Code Monkey home page Code Monkey logo

go-spvchannels's Introduction

go-spvchannels

Release Go Go

Report codecov Go Sponsor Donate Mergify Status


Go SPV Channels is a golang implementation of the SPV Channels Server.

The library implement all rest api endpoints served in SPV Channels Server and the websocket client to listen new message notifications in real time.

Table of Contents

Installation

go get github.com/libsv/go-spvchannels

Run tests

Run unit test

go clean -testcache && go test -v ./...

To run integration tests, make sure you have docker-compose up -d on your local machine, then run

go clean -testcache && go test  -race -v -tags=integration ./...

Setup Local SPV Channels server

Creating SSL key for secure connection

Following the tutorial in the SPV Channels Server, we first create the certificate using openssl:

terminal $> openssl req -x509 -out localhost.crt -keyout localhost.key -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost' -extensions EXT -config <( printf "[dn]\nCN=localhost\n[req]\ndistinguished_name = dn\n[EXT]\nsubjectAltName=DNS:localhost\nkeyUsage=digitalSignature\nextendedKeyUsage=serverAuth")
terminal $> openssl pkcs12 -export -out devkey.pfx -inkey localhost.key -in localhost.crt # use devkey as password

That will create the devkey.pfx with password devkey. We then write a docker-compose.yml file following the tutorial.

Launch local SPV Channels Server :

docker-compose up -d

Create an account

We then need to create a SPV Channels account on the server

docker exec spvchannels ./SPVChannels.API.Rest -createaccount spvchannels_dev dev dev

Usage with swagger

The SPV Channels Server run by docker-compose.yml listen on localhost:5010. We can start playing with the endpoints using swagger, i.e in browser, open https://localhost:5010/swagger/index.html

From this page, there are a link /swagger/v1/swagger.json to export swagger file

Usage with Postman

Interacting with browser might have some difficulty related to adding certificate to the system. It might be easier to use Postman to interact as Postman has a easy possibility to disable SSL certificate check to ease development propose.

From Postman, import the file devconfig/postman.json and set the environment config as follow

VARIABLE INITIAL VALUE
URL_PORT localhost:5010
ACCOUNT 1
USERNAME dev
PASSWORD dev

These environment variable are used as template to populate values in the postman.json file. There are a few more environment variable to define (look into the json file) that will depend to the endpoint and value created during the experience:

VARIABLE INITIAL VALUE
CHANNEL_ID .. to define ..
TOKEN_ID .. to define ..
TOKEN_VALUE .. to define ..
MSG_SEQUENCE .. to define ..
NOTIFY_TOKEN .. to define ..

License

License

go-spvchannels's People

Contributors

ctnguyen avatar mergify[bot] avatar roppa avatar tigh-latte avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

go-spvchannels's Issues

Functional Options for Rest Client setup

Can you change the Client setups to use Functional Options instead of a config stuct. They are nicer to user and suers only need to set only what they need. I find option structs are confusing as you don't know if you need to set everything if you ony want to change 1 thing

func NewClient(c ClientConfig) *Client {

https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis

Another example can be found https://github.com/tonicpow/go-paymail/blob/master/client.go#L19

A convention we're trying to use for these functions is With... so here we'd have WithInsecure, WithBaseURL etc. Setting any of these would update the default config.

Rename Id to ID

The linter will probably catch this, but adding in case it doesn't, rename all Acronyms in structs to be Uppercased, such as ID, JSON etc.

Add close method to Socket client

At the moment there is a maxMessages variable to trigger when the socket run ends. I don't think this is overly user friendly, how would I know what to set this to and how would I gracefully shut down the socket if I need to exit early.

Instead we should add a Close method to the socket client. This can be called to shutdown and end the socket.

I'm not 100% familiar with the gorilla websocket, but the close method would need to shutdown the gorilla socket and end the run loop and clear our any processing messages. You may need to employ a notify channel to exit the Run loop, I have an example of this here https://github.com/libsv/bitcoin-hc/blob/master/transports/zmq/headers.go#L60

Doing this means the caller can control when the Run loop ends and removes the need to a user to know in advance how many messages they want to process.

Functional Options for Socket Client

Can you change the Client setups to use Functional Options instead of a config stuct. They are nicer to user and suers only need to set only what they need. I find option structs are confusing as you don't know if you need to set everything if you ony want to change 1 thing

func NewWSClient(c WSConfig, p ProcessMessage, m ...uint64) *WSClient {

https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis

Another example can be found https://github.com/tonicpow/go-paymail/blob/master/client.go#L19

A convention we're trying to use for these functions is With... so here we'd have WithInsecure, WithBaseURL etc. Setting any of these would update the default config.

Rename file

There's a file at the root called svchannels.go, should this be spvchannels.go?

Fully document library with examples and verbose documentation

We have public comments on all methods but these could be improved with more detailed explanations and code examples, I find a handy tool to use when writing these is https://bradleyjkemp.dev/godoc-playground/ which lets you write and interact with go docs in real time.

A handy guide for writing godocs is https://elliotchance.medium.com/godoc-tips-tricks-cda6571549b and also https://go.dev/blog/godoc.

When this repo is published the docs will appear here https://pkg.go.dev/github.com/libsv/go-spvchannels so we want it to be as friendly to users as possible. Yep, we also need to update our other repos but lets make this one the example to follow.

[FEATURE] Remove empty data structure

There are some endpoints that have empty reply, we actually use an empty struct to define them. It is recommended to not use them and does not return anything if the reply is empty by definition

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.