diff --git a/chain/decred/address.go b/chain/decred/address.go new file mode 100644 index 00000000..bbefbbcf --- /dev/null +++ b/chain/decred/address.go @@ -0,0 +1,79 @@ +package decred + +import ( + "fmt" + + "github.com/decred/dcrd/chaincfg/v3" + "github.com/decred/dcrd/dcrutil/v3" + "github.com/decred/base58" + "github.com/renproject/multichain/api/address" +) + +// AddressEncodeDecoder implements the address.EncodeDecoder interface +type AddressEncodeDecoder struct { + AddressEncoder + AddressDecoder +} + +// NewAddressEncodeDecoder constructs a new AddressEncodeDecoder with the +// chain specific configurations +func NewAddressEncodeDecoder(params *chaincfg.Params) AddressEncodeDecoder { + return AddressEncodeDecoder{ + AddressEncoder: NewAddressEncoder(params), + AddressDecoder: NewAddressDecoder(params), + } +} + +// AddressEncoder encapsulates the chain specific configurations and implements +// the address.Encoder interface +type AddressEncoder struct { + params *chaincfg.Params +} + +// NewAddressEncoder constructs a new AddressEncoder with the chain specific +// configurations +func NewAddressEncoder(params *chaincfg.Params) AddressEncoder { + return AddressEncoder{params: params} +} + +// EncodeAddress implements the address.Encoder interface +func (encoder AddressEncoder) EncodeAddress(rawAddr address.RawAddress) (address.Address, error) { + + // Validate that the base58 address is in fact in correct format. + encodedAddr := base58.Encode([]byte(rawAddr)) + if _, err := dcrutil.DecodeAddress(encodedAddr, encoder.params); err != nil { + return address.Address(""), err + } + + return address.Address(encodedAddr), nil + //return address.Address(""), nil +} + + + +// AddressDecoder encapsulates the chain specific configurations and implements +// the address.Decoder interface +type AddressDecoder struct { + params *chaincfg.Params +} + +// NewAddressDecoder constructs a new AddressDecoder with the chain specific +// configurations +func NewAddressDecoder(params *chaincfg.Params) AddressDecoder { + return AddressDecoder{params: params} +} + +// DecodeAddress implements the address.Decoder interface +func (decoder AddressDecoder) DecodeAddress(addr address.Address) (address.RawAddress, error) { + decodedAddr, err := dcrutil.DecodeAddress(string(addr), decoder.params) + if err != nil { + return nil, fmt.Errorf("decode address: %v", err) + } + + switch a := decodedAddr.(type) { + case *dcrutil.AddressPubKeyHash, *dcrutil.AddressScriptHash: + return address.RawAddress(base58.Decode(string(addr))), nil + default: + return nil, fmt.Errorf("non-exhaustive pattern: address %T", a) + } +} diff --git a/chain/decred/decred.go b/chain/decred/decred.go new file mode 100644 index 00000000..de97d4ea --- /dev/null +++ b/chain/decred/decred.go @@ -0,0 +1,654 @@ +package decred + +import ( + "bytes" + "context" + "crypto/tls" + "crypto/x509" + "encoding/hex" + "encoding/json" + "fmt" + "io" + "io/ioutil" + "log" + "net/http" + "os" + "strconv" + "time" + + "github.com/decred/dcrd/chaincfg/chainhash" + "github.com/decred/dcrd/dcrjson/v3" + "github.com/decred/dcrd/dcrutil/v3" + "github.com/decred/dcrd/rpc/jsonrpc/types/v2" + "github.com/renproject/multichain/api/address" + "github.com/renproject/multichain/api/utxo" + "github.com/renproject/pack" +) + +const ( + // DefaultClientTimeout used by the Client. + DefaultClientTimeout = time.Minute + // DefaultClientTimeoutRetry used by the Client. + DefaultClientTimeoutRetry = time.Second + // DefaultClientHost used by the Client. This should only be used for local + // deployments of the multichain. + DefaultClientHost = "https://127.0.0.1:19556" + // DefaultWalletHost used by dcrwallet. This should only be used for local + // deployments of the multichain. + DefaultWalletHost = "https://127.0.0.1:19557" + // DefaultClientUser used by the Client. This is insecure, and should only + // be used for local — or publicly accessible — deployments of the + // multichain. + DefaultClientUser = "user" + // DefaultClientPassword used by the Client. This is insecure, and should + // only be used for local — or publicly accessible — deployments of the + // multichain. + DefaultClientPassword = "password" + DefaultClientNoTLS = false + // Authorization types. + DefaultClientAuthTypeBasic = "basic" + DefaultClientAuthTypeClientCert = "clientcert" + DefaultClientTLSSkipVerify = false + DefaultClientCert = "rpc.cert" +) + +// ClientOptions are used to parameterise the behaviour of the Client. +type ClientOptions struct { + Timeout time.Duration + TimeoutRetry time.Duration + NoTLS bool + Host string + WalletHost string + User string + Password string + TLSSkipVerify bool + AuthType string + ClientCert string + ClientKey string + RPCCert string + WalletRPCCert string +} + +type ClientSetting struct { + user string + password string + host string + httpClient http.Client +} + +// DefaultClientOptions returns ClientOptions with the default settings. These +// settings are valid for use with the default local deployment of the +// multichain. In production, the host, user, and password should be changed. +func DefaultClientOptions() ClientOptions { + + dir, err := os.Getwd() + if err != nil { + log.Fatal(err) + } + defaultCertFile := dir + "/../../infra/decred/" + DefaultClientCert + + return ClientOptions{ + Timeout: DefaultClientTimeout, + TimeoutRetry: DefaultClientTimeoutRetry, + NoTLS: DefaultClientNoTLS, + Host: DefaultClientHost, + WalletHost: DefaultWalletHost, + User: DefaultClientUser, + Password: DefaultClientPassword, + TLSSkipVerify: DefaultClientTLSSkipVerify, + AuthType: DefaultClientAuthTypeBasic, + RPCCert: defaultCertFile, + WalletRPCCert: defaultCertFile, + } +} + +// WithHost sets the URL of the dcrd node. +func (opts ClientOptions) WithHost(host string) ClientOptions { + opts.Host = host + return opts +} + +// WithRPCCert sets the path of the dcrd RPC cert. +func (opts ClientOptions) WithRPCCert(certPath string) ClientOptions { + opts.RPCCert = certPath + return opts +} + +// WithWalletHost sets the URL of the dcrwallet node. +func (opts ClientOptions) WithWalletHost(host string) ClientOptions { + opts.WalletHost = host + return opts +} + +// WithHost sets the path of the dcrwallet RPC cert. +func (opts ClientOptions) WithWalletRPCCert(certPath string) ClientOptions { + opts.WalletRPCCert = certPath + return opts +} + +// WithUser sets the username that will be used to authenticate with the dcrd +// node. +func (opts ClientOptions) WithUser(user string) ClientOptions { + opts.User = user + return opts +} + +// WithPassword sets the password that will be used to authenticate with the +// dcrd node. +func (opts ClientOptions) WithPassword(password string) ClientOptions { + opts.Password = password + return opts +} + +// A Client interacts with an instance of the decred network using the RPC +// interface exposed by a dcrd/dcrwallet node. +type Client interface { + utxo.Client + // UnspentOutputs spendable by the given address. + UnspentOutputs(ctx context.Context, minConf, maxConf int64, address address.Address) ([]utxo.Output, error) + // Confirmations of a transaction in the Bitcoin network. + Confirmations(ctx context.Context, txHash pack.Bytes) (int64, error) + // EstimateSmartFee + EstimateSmartFee(ctx context.Context, numBlocks int64) (float64, error) + // EstimateFeeLegacy + EstimateFeeLegacy(ctx context.Context, numBlocks int64) (float64, error) +} + +type client struct { + opts ClientOptions + httpClient http.Client + walletClient http.Client +} + +// NewClient returns a new Client. +func NewClient(opts ClientOptions) *client { + httpClient := http.Client{} + httpClient.Timeout = opts.Timeout + + // Configure TLS if needed. + var tlsConfig *tls.Config + if !opts.NoTLS { + tlsConfig = &tls.Config{ + InsecureSkipVerify: opts.TLSSkipVerify, + } + if !opts.TLSSkipVerify && opts.AuthType == DefaultClientAuthTypeClientCert { + serverCAs := x509.NewCertPool() + serverCert, err := ioutil.ReadFile(opts.RPCCert) + if err != nil { + fmt.Printf("Error: %s \n", err) + return nil + } + if !serverCAs.AppendCertsFromPEM(serverCert) { + fmt.Println("Cannot Append Sever cert") + return nil + } + keypair, err := tls.LoadX509KeyPair(opts.ClientCert, opts.ClientKey) + if err != nil { + fmt.Printf("Error: %s \n", err) + return nil + } + + tlsConfig.Certificates = []tls.Certificate{keypair} + tlsConfig.RootCAs = serverCAs + + } + if !opts.TLSSkipVerify && opts.RPCCert != "" { + pem, err := ioutil.ReadFile(opts.RPCCert) + if err != nil { + fmt.Printf("Error: %s \n", err) + return nil + } + + pool := x509.NewCertPool() + if ok := pool.AppendCertsFromPEM(pem); !ok { + return nil + } + tlsConfig.RootCAs = pool + } + } + + httpClient.Transport = &http.Transport{ + TLSClientConfig: tlsConfig, + } + + // Wallet Client. + walletClient := http.Client{} + walletClient.Timeout = opts.Timeout + + // Configure TLS if needed. + var tlsConf *tls.Config + if !opts.NoTLS { + tlsConf = &tls.Config{ + InsecureSkipVerify: opts.TLSSkipVerify, + } + if !opts.TLSSkipVerify && opts.AuthType == DefaultClientAuthTypeClientCert { + serverCAs := x509.NewCertPool() + serverCert, err := ioutil.ReadFile(opts.WalletRPCCert) + if err != nil { + fmt.Printf("Error: %s \n", err) + return nil + } + if !serverCAs.AppendCertsFromPEM(serverCert) { + return nil + } + keypair, err := tls.LoadX509KeyPair(opts.ClientCert, opts.ClientKey) + if err != nil { + fmt.Printf("Error: %s \n", err) + return nil + } + + tlsConf.Certificates = []tls.Certificate{keypair} + tlsConf.RootCAs = serverCAs + + } + if !opts.TLSSkipVerify && opts.WalletRPCCert != "" { + pem, err := ioutil.ReadFile(opts.WalletRPCCert) + if err != nil { + fmt.Printf("Error: %s \n", err) + return nil + } + + pool := x509.NewCertPool() + if ok := pool.AppendCertsFromPEM(pem); !ok { + return nil + } + tlsConf.RootCAs = pool + } + } + + walletClient.Transport = &http.Transport{ + TLSClientConfig: tlsConf, + } + + return &client{ + opts: opts, + httpClient: httpClient, + walletClient: walletClient, + } +} + +// LatestBlock returns the height of the longest blockchain. +func (client *client) LatestBlock(ctx context.Context) (pack.U64, error) { + //var resp int64 + var resp dcrjson.Response + if err := client.send(ctx, &resp, "getbestblock"); err != nil { + return pack.NewU64(0), fmt.Errorf("get block count: %v", err) + } + + result := struct { + Hash string `json:"hash"` + Height uint64 `json:"height"` + }{} + + err := json.Unmarshal(resp.Result, &result) + if err != nil { + return pack.NewU64(0), err + } + + if result.Height < 0 { + return pack.NewU64(0), fmt.Errorf("unexpected block count, expected > 0, got: %v", result.Height) + } + + return pack.NewU64(uint64(result.Height)), nil +} + +// UnspentOutputs spendable by the given address. +func (client *client) UnspentOutputs(ctx context.Context, minConf, maxConf int64, addr address.Address) ([]utxo.Output, error) { + var outputs []utxo.Output + + //var resp int64 + var resp dcrjson.Response + if err := client.send(ctx, &resp, "listunspent", minConf, maxConf, []string{string(addr)}); err != nil { + return []utxo.Output{}, fmt.Errorf("bad \"listunspent\": %v", err) + } + + //outputs := make([]utxo.Output, len(resp.Result)) + type Result struct { + TxId string `json:"txid"` + VOut uint32 `json:"vout"` + Tree int `json:"tree"` + TxType int `json:"txtype"` + Address string `json:"address"` + Account string `json:"account"` + ScriptPubKey string `json:"scriptPubKey"` + Amount float64 `json:"amount"` + Confirmations int64 `json:"confirmations"` + Spendable bool `json"spendable"` + } + + var result []Result + + err := json.Unmarshal(resp.Result, &result) + if err != nil { + return []utxo.Output{}, fmt.Errorf("bad \"listunspent\": %v", err) + } + + //fmt.Printf("Unspent Output: %+v \n", resp) + for _, v := range result { + amount, err := dcrutil.NewAmount(v.Amount) + if err != nil { + return []utxo.Output{}, fmt.Errorf("bad amount: %v", err) + } + if amount < 0 { + return []utxo.Output{}, fmt.Errorf("bad amount: %v", amount) + } + pubKeyScript, err := hex.DecodeString(v.ScriptPubKey) + if err != nil { + return []utxo.Output{}, fmt.Errorf("bad pubkey script: %v", err) + } + txid, err := chainhash.NewHashFromStr(v.TxId) + if err != nil { + return []utxo.Output{}, fmt.Errorf("bad txid: %v", err) + } + o := utxo.Output{ + Outpoint: utxo.Outpoint{ + Hash: pack.NewBytes(txid[:]), + Index: pack.NewU32(v.VOut), + }, + Value: pack.NewU256FromU64(pack.NewU64(uint64(amount))), + PubKeyScript: pack.NewBytes(pubKeyScript), + } + + outputs = append(outputs, o) + } + + return outputs, nil +} + +// Output associated with an outpoint, and its number of confirmations. +func (client *client) Output(ctx context.Context, outpoint utxo.Outpoint) (utxo.Output, pack.U64, error) { + + //var resp int64 + var resp dcrjson.Response + hash := chainhash.Hash{} + copy(hash[:], outpoint.Hash) + if err := client.send(ctx, &resp, "getrawtransaction", hash.String(), 1); err != nil { + return utxo.Output{}, pack.NewU64(0), fmt.Errorf("bad \"getrawtransaction\": %v", err) + } + + result := types.TxRawResult{} + err := json.Unmarshal(resp.Result, &result) + if err != nil { + return utxo.Output{}, pack.NewU64(0), fmt.Errorf("bad \"getrawtransaction\": %v", err) + } + + if outpoint.Index.Uint32() >= uint32(len(result.Vout)) { + return utxo.Output{}, pack.NewU64(0), fmt.Errorf("bad index: %v is out of range", outpoint.Index) + } + vout := result.Vout[outpoint.Index.Uint32()] + amount, err := dcrutil.NewAmount(vout.Value) + if err != nil { + return utxo.Output{}, pack.NewU64(0), fmt.Errorf("bad amount: %v", err) + } + if amount < 0 { + return utxo.Output{}, pack.NewU64(0), fmt.Errorf("bad amount: %v", amount) + } + pubKeyScript, err := hex.DecodeString(vout.ScriptPubKey.Hex) + if err != nil { + return utxo.Output{}, pack.NewU64(0), fmt.Errorf("bad pubkey script: %v", err) + } + output := utxo.Output{ + Outpoint: outpoint, + Value: pack.NewU256FromU64(pack.NewU64(uint64(amount))), + PubKeyScript: pack.NewBytes(pubKeyScript), + } + return output, pack.NewU64(uint64(result.Confirmations)), nil +} + +// UnspentOutput returns the unspent transaction output identified by the +// given outpoint. It also returns the number of confirmations for the +// output. If the output cannot be found before the context is done, the +// output is invalid, or the output has been spent, then an error should be +// returned. +func (client *client) UnspentOutput(ctx context.Context, outpoint utxo.Outpoint) (utxo.Output, pack.U64, error) { + var resp dcrjson.Response + hash := chainhash.Hash{} + copy(hash[:], outpoint.Hash) + if err := client.send(ctx, &resp, "gettxout", hash.String(), outpoint.Index.Uint32()); err != nil { + return utxo.Output{}, pack.NewU64(0), fmt.Errorf("bad \"gettxout\": %v", err) + } + + result := types.GetTxOutResult{} + err := json.Unmarshal(resp.Result, &result) + if err != nil { + return utxo.Output{}, pack.NewU64(0), fmt.Errorf("bad \"gettxout\": %v", err) + } + + amount, err := dcrutil.NewAmount(result.Value) + if err != nil { + return utxo.Output{}, pack.NewU64(0), fmt.Errorf("bad amount: %v", err) + } + if amount < 0 { + return utxo.Output{}, pack.NewU64(0), fmt.Errorf("bad amount: %v", amount) + } + if result.Confirmations < 0 { + return utxo.Output{}, pack.NewU64(0), fmt.Errorf("bad confirmations: %v", result.Confirmations) + } + pubKeyScript, err := hex.DecodeString(result.ScriptPubKey.Hex) + if err != nil { + return utxo.Output{}, pack.NewU64(0), fmt.Errorf("bad pubkey script: %v", err) + } + output := utxo.Output{ + Outpoint: outpoint, + Value: pack.NewU256FromU64(pack.NewU64(uint64(amount))), + PubKeyScript: pack.NewBytes(pubKeyScript), + } + return output, pack.NewU64(uint64(result.Confirmations)), nil +} + +func (client *client) send(ctx context.Context, resp *dcrjson.Response, method string, params ...interface{}) error { + // Encode the request. + data, err := encodeRequest(method, params) + if err != nil { + return err + } + + var clSetting *ClientSetting + switch method { + case "getbestblock", "getrawtransaction", "gettxout", + "sendrawtransaction", "estimatesmartfee", "estimatefee": + clSetting = &ClientSetting{ + user: client.opts.User, + password: client.opts.Password, + host: client.opts.Host, + httpClient: client.httpClient, + } + case "listunspent", "gettransaction": + clSetting = &ClientSetting{ + user: client.opts.User, + password: client.opts.Password, + host: client.opts.WalletHost, + httpClient: client.walletClient, + } + } + + return retry(ctx, client.opts.TimeoutRetry, func() error { + // Create request and add basic authentication headers. The context is + // not attached to the request, and instead we all each attempt to run + // for the timeout duration, and we keep attempting until success, or + // the context is done. + req, err := http.NewRequest("POST", clSetting.host, bytes.NewBuffer(data)) + if err != nil { + return fmt.Errorf("building http request: %v", err) + } + req.SetBasicAuth(clSetting.user, clSetting.password) + + // Send the request and decode the response. + res, err := clSetting.httpClient.Do(req) + if err != nil { + return fmt.Errorf("sending http request: %v", err) + } + // Read the raw bytes and close the response. + respBytes, err := ioutil.ReadAll(res.Body) + defer res.Body.Close() + //if err := decodeResponse(resp, res.Body); err != nil { + // return fmt.Errorf("decoding http response: %v", err) + //} + + if err != nil { + err = fmt.Errorf("error reading json reply: %s", err) + return err + } + + // Handle unsuccessful HTTP responses + if res.StatusCode < 200 || res.StatusCode >= 300 { + // Generate a standard error to return if the server body is + // empty. This should not happen very often, but it's better + // than showing nothing in case the target server has a poor + // implementation. + if len(respBytes) == 0 { + return fmt.Errorf("%d %s", res.StatusCode, + http.StatusText(res.StatusCode)) + } + return fmt.Errorf("%s", respBytes) + } + + // Unmarshal the response. + // var resp dcrjson.Response + if err := json.Unmarshal(respBytes, resp); err != nil { + return err + } + + return nil + }) +} + +func encodeRequest(method string, params []interface{}) ([]byte, error) { + rawParams, err := json.Marshal(params) + if err != nil { + return nil, fmt.Errorf("encoding params: %v", err) + } + req := struct { + Version string `json:"jsonrpc"` + ID int `json:"id"` + Method string `json:"method"` + Params json.RawMessage `json:"params"` + }{ + Version: "1.0", + ID: 1, + Method: method, + Params: rawParams, + } + rawReq, err := json.Marshal(req) + if err != nil { + return nil, fmt.Errorf("encoding request: %v", err) + } + return rawReq, nil +} + +func decodeResponse(resp interface{}, r io.Reader) error { + res := struct { + Version string `json:"version"` + ID int `json:"id"` + Result *json.RawMessage `json:"result"` + Error *json.RawMessage `json:"error"` + }{} + if err := json.NewDecoder(r).Decode(&res); err != nil { + return fmt.Errorf("decoding response: %v", err) + } + if res.Error != nil { + return fmt.Errorf("decoding response: %v", string(*res.Error)) + } + if res.Result == nil { + return fmt.Errorf("decoding result: result is nil") + } + if err := json.Unmarshal(*res.Result, resp); err != nil { + return fmt.Errorf("decoding result: %v", err) + } + return nil +} + +func retry(ctx context.Context, dur time.Duration, f func() error) error { + ticker := time.NewTicker(dur) + err := f() + for err != nil { + log.Printf("retrying: %v", err) + select { + case <-ctx.Done(): + return fmt.Errorf("%v: %v", ctx.Err(), err) + case <-ticker.C: + err = f() + } + } + return nil +} + +// SubmitTx to the Decred network. +func (client *client) SubmitTx(ctx context.Context, tx utxo.Tx) error { + serial, err := tx.Serialize() + if err != nil { + return fmt.Errorf("bad tx: %v", err) + } + var resp dcrjson.Response + if err := client.send(ctx, &resp, "sendrawtransaction", hex.EncodeToString(serial), true); err != nil { + return fmt.Errorf("bad \"sendrawtransaction\": %v", err) + } + fmt.Printf("Response: %+v \n", string(resp.Result)) + return nil +} + +func (client *client) EstimateSmartFee(ctx context.Context, numBlocks int64) (float64, error) { + resp := dcrjson.Response{} + + if err := client.send(ctx, &resp, "estimatesmartfee", numBlocks); err != nil { + return 0.0, fmt.Errorf("estimating smart fee: %v", err) + } + + if resp.Error != nil { + return 0.0, resp.Error + } + + result, err := strconv.ParseFloat(string(resp.Result), 10) + if err != nil { + return 0.0, err + } + + return result, nil +} + +func (client *client) EstimateFeeLegacy(ctx context.Context, numBlocks int64) (float64, error) { + resp := dcrjson.Response{} + + if err := client.send(ctx, &resp, "estimatefee", numBlocks); err != nil { + return 0.0, fmt.Errorf("estimating fee: %v", err) + } + + if resp.Error != nil { + return 0.0, resp.Error + } + + result, err := strconv.ParseFloat(string(resp.Result), 10) + if err != nil { + return 0.0, err + } + + return result, nil +} + +// Confirmations of a transaction in the decred network. +func (client *client) Confirmations(ctx context.Context, txHash pack.Bytes) (int64, error) { + var resp dcrjson.Response + + size := len(txHash) + txHashReversed := make([]byte, size) + copy(txHashReversed[:], txHash[:]) + for i := 0; i < size/2; i++ { + txHashReversed[i], txHashReversed[size-1-i] = txHashReversed[size-1-i], txHashReversed[i] + } + + if err := client.send(ctx, &resp, "gettransaction", hex.EncodeToString(txHashReversed)); err != nil { + return 0, fmt.Errorf("bad \"gettransaction\": %v", err) + } + + result := types.TxRawResult{} + err := json.Unmarshal(resp.Result, &result) + if err != nil { + return 0, fmt.Errorf("bad \"gettransaction\": %v", err) + } + + confirmations := result.Confirmations + if confirmations < 0 { + confirmations = 0 + } + return confirmations, nil +} diff --git a/chain/decred/decred_suite_test.go b/chain/decred/decred_suite_test.go new file mode 100644 index 00000000..0424de42 --- /dev/null +++ b/chain/decred/decred_suite_test.go @@ -0,0 +1,13 @@ +package decred_test + +import ( + "testing" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +func TestDecred(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Decred Suite") +} diff --git a/chain/decred/decred_test.go b/chain/decred/decred_test.go new file mode 100644 index 00000000..3fdd0403 --- /dev/null +++ b/chain/decred/decred_test.go @@ -0,0 +1,134 @@ +package decred_test + +import ( + "context" + "log" + "os" + "reflect" + "time" + + "github.com/decred/dcrd/chaincfg/v3" + "github.com/decred/dcrd/dcrec" + "github.com/decred/dcrd/dcrec/secp256k1/v3" + "github.com/decred/dcrd/dcrutil/v3" + "github.com/renproject/id" + "github.com/renproject/pack" + + "github.com/renproject/multichain/api/address" + "github.com/renproject/multichain/api/utxo" + "github.com/renproject/multichain/chain/decred" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("Decred", func() { + Context("when submitting transactions", func() { + Context("when sending DCR to multiple addresses", func() { + It("should work", func() { + // Load private key, and assume that the associated address has + // funds to spend. You can do this by setting DECRED_PK to the + // value specified in the `./multichaindeploy/.env` file. + pkEnv := os.Getenv("DECRED_PK") + if pkEnv == "" { + panic("DECRED_PK is undefined") + } + simNetPrivKeyID := [2]byte{0x23, 0x07} + wif, err := dcrutil.DecodeWIF(pkEnv, simNetPrivKeyID) + Expect(err).ToNot(HaveOccurred()) + + // PKH + addrPubKeyHash, err := dcrutil.NewAddressPubKeyHash(dcrutil.Hash160(wif.PubKey()), chaincfg.SimNetParams(), dcrec.STEcdsaSecp256k1) + Expect(err).ToNot(HaveOccurred()) + + log.Printf("PKH %v", addrPubKeyHash.String()) + + // Setup the client and load the unspent transaction outputs. + client := decred.NewClient(decred.DefaultClientOptions()) + outputs, err := client.UnspentOutputs(context.Background(), 0, 999999999, address.Address(addrPubKeyHash.String())) + Expect(err).ToNot(HaveOccurred()) + Expect(len(outputs)).To(BeNumerically(">", 0)) + output := outputs[0] + + // Check that we can load the output and that it is equal. + // Otherwise, something strange is happening with the RPC + // client. + output2, _, err := client.Output(context.Background(), output.Outpoint) + Expect(err).ToNot(HaveOccurred()) + Expect(reflect.DeepEqual(output, output2)).To(BeTrue()) + output2, _, err = client.UnspentOutput(context.Background(), output.Outpoint) + Expect(err).ToNot(HaveOccurred()) + Expect(reflect.DeepEqual(output, output2)).To(BeTrue()) + + // Build the transaction by consuming the outputs and spending + // them to a set of recipients. + inputs := []utxo.Input{ + {Output: utxo.Output{ + Outpoint: utxo.Outpoint{ + Hash: output.Outpoint.Hash[:], + Index: output.Outpoint.Index, + }, + PubKeyScript: output.PubKeyScript, + Value: output.Value, + }}, + } + recipients := []utxo.Recipient{ + { + To: address.Address(addrPubKeyHash.String()), + Value: pack.NewU256FromU64(pack.NewU64((output.Value.Int().Uint64() - 1000) / 3)), + }, + } + tx, err := decred.NewTxBuilder(chaincfg.SimNetParams()).BuildTx(inputs, recipients) + Expect(err).ToNot(HaveOccurred()) + + // Get the digests that need signing from the transaction, and + // sign them. In production, this would be done using the RZL + // MPC algorithm, but for the purposes of this test, using an + // explicit privkey is ok. + sighashes, err := tx.Sighashes() + signatures := make([]pack.Bytes65, len(sighashes)) + Expect(err).ToNot(HaveOccurred()) + for i := range sighashes { + hash := id.Hash(sighashes[i]) + priv := secp256k1.PrivKeyFromBytes(wif.PrivKey()) + privk := priv.ToECDSA() + privKey := (*id.PrivKey)(privk) + signature, err := privKey.Sign(&hash) + Expect(err).ToNot(HaveOccurred()) + signatures[i] = pack.NewBytes65(signature) + } + Expect(tx.Sign(signatures, pack.NewBytes(wif.PubKey()))).To(Succeed()) + + // Submit the transaction to the dcrd node. Again, this + // should be running a la `./multichaindeploy`. + txHash, err := tx.Hash() + Expect(err).ToNot(HaveOccurred()) + err = client.SubmitTx(context.Background(), tx) + Expect(err).ToNot(HaveOccurred()) + log.Printf("TXID %v", txHash) + + for { + // Loop until the transaction has at least a few + // confirmations. This implies that the transaction is + // definitely valid, and the test has passed. We were + // successfully able to use the multichain to construct and + // submit a Bitcoin transaction! + confs, err := client.Confirmations(context.Background(), txHash) + Expect(err).ToNot(HaveOccurred()) + log.Printf(" %v/3 confirmations", confs) + if confs >= 1 { + break + } + time.Sleep(10 * time.Second) + } + + // Check that we can load the output and that it is equal. + // Otherwise, something strange is happening with the RPC + // client. + output2, _, err = client.Output(context.Background(), output.Outpoint) + Expect(err).ToNot(HaveOccurred()) + Expect(reflect.DeepEqual(output, output2)).To(BeTrue()) + }) + }) + }) +}) diff --git a/chain/decred/gas.go b/chain/decred/gas.go new file mode 100644 index 00000000..3b652755 --- /dev/null +++ b/chain/decred/gas.go @@ -0,0 +1,57 @@ +package decred + +import ( + "context" + "fmt" + "math" + + "github.com/renproject/pack" +) + +const ( + dcrToAtom = 1e8 + kilobyteToByte = 1024 +) + +// A GasEstimator returns the SATs-per-byte that is needed in order to confirm +// transactions with an estimated maximum delay of one block. In distributed +// networks that collectively build, sign, and submit transactions, it is +// important that all nodes in the network have reached consensus on the +// SATs-per-byte. +type GasEstimator struct { + client Client + numBlocks int64 + fallbackGas pack.U256 +} + +// NewGasEstimator returns a simple gas estimator that always returns the given +// number of SATs-per-byte. +func NewGasEstimator(client Client, numBlocks int64, fallbackGas pack.U256) GasEstimator { + return GasEstimator{ + client: client, + numBlocks: numBlocks, + fallbackGas: fallbackGas, + } +} + +// EstimateGas returns the number of SATs-per-byte (for both price and cap) that +// is needed in order to confirm transactions with an estimated maximum delay of +// `numBlocks` block. It is the responsibility of the caller to know the number +// of bytes in their transaction. This method calls the `estimatesmartfee` RPC +// call to the node, which based on a conservative (considering longer history) +// strategy returns the estimated DCR per kilobyte of data in the transaction. +// An error will be returned if the node hasn't observed enough blocks +// to make an estimate for the provided target `numBlocks`. +func (gasEstimator GasEstimator) EstimateGas(ctx context.Context) (pack.U256, pack.U256, error) { + feeRate, err := gasEstimator.client.EstimateFeeLegacy(ctx, gasEstimator.numBlocks) + if err != nil { + return gasEstimator.fallbackGas, gasEstimator.fallbackGas, err + } + + if feeRate <= 0.0 { + return gasEstimator.fallbackGas, gasEstimator.fallbackGas, fmt.Errorf("invalid fee rate: %v", feeRate) + } + + satsPerByte := uint64(math.Ceil(feeRate * dcrToAtom / kilobyteToByte)) + return pack.NewU256FromUint64(satsPerByte), pack.NewU256FromUint64(satsPerByte), nil +} diff --git a/chain/decred/gas_test.go b/chain/decred/gas_test.go new file mode 100644 index 00000000..8d5983c6 --- /dev/null +++ b/chain/decred/gas_test.go @@ -0,0 +1,52 @@ +package decred_test + +import ( + "context" + + "github.com/renproject/multichain/chain/decred" + "github.com/renproject/pack" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("Gas", func() { + Context("when estimating decred network fee", func() { + It("should work", func() { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + client := decred.NewClient(decred.DefaultClientOptions()) + + // estimate fee to include tx within 1 block. + fallback1 := uint64(123) + gasEstimator1 := decred.NewGasEstimator(client, 1, pack.NewU256FromUint64(fallback1)) + gasPrice1, _, err := gasEstimator1.EstimateGas(ctx) + if err != nil { + Expect(gasPrice1).To(Equal(pack.NewU256FromUint64(fallback1))) + } + + // estimate fee to include tx within 10 blocks. + fallback2 := uint64(234) + gasEstimator2 := decred.NewGasEstimator(client, 10, pack.NewU256FromUint64(fallback2)) + gasPrice2, _, err := gasEstimator2.EstimateGas(ctx) + if err != nil { + Expect(gasPrice2).To(Equal(pack.NewU256FromUint64(fallback2))) + } + + // estimate fee to include tx within 100 blocks. + fallback3 := uint64(345) + gasEstimator3 := decred.NewGasEstimator(client, 100, pack.NewU256FromUint64(fallback3)) + gasPrice3, _, err := gasEstimator3.EstimateGas(ctx) + if err != nil { + Expect(gasPrice3).To(Equal(pack.NewU256FromUint64(fallback3))) + } + + // expect fees in this order at the very least. + if err == nil { + Expect(gasPrice1.GreaterThanEqual(gasPrice2)).To(BeTrue()) + Expect(gasPrice2.GreaterThanEqual(gasPrice3)).To(BeTrue()) + } + }) + }) +}) diff --git a/chain/decred/utxo.go b/chain/decred/utxo.go new file mode 100644 index 00000000..b1196b53 --- /dev/null +++ b/chain/decred/utxo.go @@ -0,0 +1,204 @@ +package decred + +import ( + "bytes" + "errors" + "fmt" + "math/big" + + "github.com/btcsuite/btcd/btcec" + "github.com/decred/dcrd/chaincfg/chainhash" + "github.com/decred/dcrd/chaincfg/v3" + "github.com/decred/dcrd/dcrutil/v3" + "github.com/decred/dcrd/txscript/v3" + "github.com/decred/dcrd/wire" + "github.com/renproject/multichain/api/utxo" + "github.com/renproject/pack" +) + +// The TxBuilder is an implementation of a UTXO-compatible transaction builder +// for Bitcoin. +type TxBuilder struct { + params *chaincfg.Params +} + +// NewTxBuilder returns a transaction builder that builds UTXO-compatible +// Bitcoin transactions for the given chain configuration (this means that it +// can be used for regnet, testnet, and mainnet, but also for networks that are +// minimally modified forks of the Bitcoin network). +func NewTxBuilder(params *chaincfg.Params) TxBuilder { + return TxBuilder{params: params} +} + +// BuildTx returns a Decred transaction that consumes funds from the given +// inputs, and sends them to the given recipients. The difference in the sum +// value of the inputs and the sum value of the recipients is paid as a fee to +// the Bitcoin network. This fee must be calculated independently of this +// function. Outputs produced for recipients will use P2PKH, P2SH, P2WPKH, or +// P2WSH scripts as the pubkey script, based on the format of the recipient +// address. +func (txBuilder TxBuilder) BuildTx(inputs []utxo.Input, recipients []utxo.Recipient) (utxo.Tx, error) { + msgTx := wire.NewMsgTx() + + // Inputs + for _, input := range inputs { + hash := chainhash.Hash{} + copy(hash[:], input.Hash) + index := input.Index.Uint32() + amt, err := dcrutil.NewAmount(1) + if err != nil { + return nil, err + } + prevOutV := int64(amt) + msgTx.AddTxIn(wire.NewTxIn(wire.NewOutPoint(&hash, index, wire.TxTreeRegular), prevOutV, []byte{})) + } + + // Outputs + for _, recipient := range recipients { + addr, err := dcrutil.DecodeAddress(string(recipient.To), txBuilder.params) + if err != nil { + return nil, err + } + // Ensure the address is one of the supported types. + switch addr.(type) { + case *dcrutil.AddressPubKeyHash: + case *dcrutil.AddressScriptHash: + default: + return nil, errors.New("Invalid address type") + } + + script, err := txscript.PayToAddrScript(addr) + if err != nil { + return nil, err + } + + value := recipient.Value.Int().Int64() + if value < 0 { + return nil, fmt.Errorf("expected value >= 0, got value %v", value) + } + msgTx.AddTxOut(wire.NewTxOut(value, script)) + } + + return &Tx{inputs: inputs, recipients: recipients, msgTx: msgTx, signed: false}, nil +} + +// Tx represents a simple Decred transaction +type Tx struct { + inputs []utxo.Input + recipients []utxo.Recipient + + msgTx *wire.MsgTx + + signed bool +} + +// Hash returns the transaction hash of the given underlying transaction. +func (tx *Tx) Hash() (pack.Bytes, error) { + txhash := tx.msgTx.TxHash() + return pack.NewBytes(txhash[:]), nil +} + +// Inputs returns the UTXO inputs in the underlying transaction. +func (tx *Tx) Inputs() ([]utxo.Input, error) { + return tx.inputs, nil +} + +// Outputs returns the UTXO outputs in the underlying transaction. +func (tx *Tx) Outputs() ([]utxo.Output, error) { + hash, err := tx.Hash() + if err != nil { + return nil, fmt.Errorf("bad hash: %v", err) + } + outputs := make([]utxo.Output, len(tx.msgTx.TxOut)) + for i := range outputs { + outputs[i].Outpoint = utxo.Outpoint{ + Hash: hash, + Index: pack.NewU32(uint32(i)), + } + outputs[i].PubKeyScript = pack.Bytes(tx.msgTx.TxOut[i].PkScript) + if tx.msgTx.TxOut[i].Value < 0 { + return nil, fmt.Errorf("bad output %v: value is less than zero", i) + } + outputs[i].Value = pack.NewU256FromU64(pack.NewU64(uint64(tx.msgTx.TxOut[i].Value))) + } + return outputs, nil +} + +// Sighashes returns the digests that must be signed before the transaction +// can be submitted by the client. +func (tx *Tx) Sighashes() ([]pack.Bytes32, error) { + sighashes := make([]pack.Bytes32, len(tx.inputs)) + + for i, txin := range tx.inputs { + pubKeyScript := txin.PubKeyScript + sigScript := txin.SigScript + value := txin.Value.Int().Int64() + if value < 0 { + return []pack.Bytes32{}, fmt.Errorf("expected value >= 0, got value %v", value) + } + + var hash []byte + var err error + if sigScript == nil { + hash, err = txscript.CalcSignatureHash(pubKeyScript, txscript.SigHashAll, tx.msgTx, i, nil) + } else { + hash, err = txscript.CalcSignatureHash(sigScript, txscript.SigHashAll, tx.msgTx, i, nil) + } + if err != nil { + return []pack.Bytes32{}, err + } + + sighash := [32]byte{} + copy(sighash[:], hash) + sighashes[i] = pack.NewBytes32(sighash) + } + + return sighashes, nil +} + +// Sign consumes a list of signatures, and adds them to the list of UTXOs in +// the underlying transactions. +func (tx *Tx) Sign(signatures []pack.Bytes65, pubKey pack.Bytes) error { + if tx.signed { + return fmt.Errorf("already signed") + } + if len(signatures) != len(tx.msgTx.TxIn) { + return fmt.Errorf("expected %v signatures, got %v signatures", len(tx.msgTx.TxIn), len(signatures)) + } + + for i, rsv := range signatures { + var err error + + // Decode the signature and the pubkey script. + r := new(big.Int).SetBytes(rsv[:32]) + s := new(big.Int).SetBytes(rsv[32:64]) + signature := btcec.Signature{ + R: r, + S: s, + } + sigScript := tx.inputs[i].SigScript + + builder := txscript.NewScriptBuilder() + builder.AddData(append(signature.Serialize(), byte(txscript.SigHashAll))) + builder.AddData(pubKey) + if sigScript != nil { + builder.AddData(sigScript) + } + tx.msgTx.TxIn[i].SignatureScript, err = builder.Script() + if err != nil { + return err + } + } + + tx.signed = true + return nil +} + +// Serialize serializes the UTXO transaction to bytes +func (tx *Tx) Serialize() (pack.Bytes, error) { + buf := new(bytes.Buffer) + if err := tx.msgTx.Serialize(buf); err != nil { + return pack.Bytes{}, err + } + return pack.NewBytes(buf.Bytes()), nil +} diff --git a/chain/decred/utxo_test.go b/chain/decred/utxo_test.go new file mode 100644 index 00000000..3d06df08 --- /dev/null +++ b/chain/decred/utxo_test.go @@ -0,0 +1 @@ +package decred_test \ No newline at end of file diff --git a/infra/.env b/infra/.env index 7d32d6e5..d92c70c6 100644 --- a/infra/.env +++ b/infra/.env @@ -25,6 +25,16 @@ export BITCOIN_ADDRESS=mwjUmhAW68zCtgZpW5b1xD5g7MZew6xPV4 export BITCOINCASH_PK=cSEohZFQLKuemNeBVrzwxniouUJJxdcx7Tm6HpspYuxraVjytieW export BITCOINCASH_ADDRESS=bchreg:qp6tejc0ghtjeejcxa97amzvxvzacjt4qczpy2n3gf +# +# Decred +# + +# Address that will receive mining rewards. Generally, this is set to an address +# for which the private key is known by a test suite. This allows the test suite +# access to plenty of testing funds. +export DECRED_PK=PsUQEpYDXVwphd9xNXUMj63LyxSWPTor3RDgfw9DMdH9tDkJaosyp +export DECRED_ADDRESS=SsaGEEZu2L8x93qvKzzahtzQ7yzkec3i8wL + # # DigiByte # diff --git a/infra/decred/Dockerfile b/infra/decred/Dockerfile new file mode 100644 index 00000000..31414f0c --- /dev/null +++ b/infra/decred/Dockerfile @@ -0,0 +1,39 @@ +FROM ubuntu:xenial + +ENV VERSION v1.6.2 +ENV RELEASE_NAME decred-linux-amd64-${VERSION} +ENV RELEASE_FILE ${RELEASE_NAME}.tar.gz +ENV DCR_URL https://github.com/decred/decred-binaries/releases/download/${VERSION}/${RELEASE_FILE} + + +RUN apt-get update && apt-get install --yes software-properties-common +Run apt-get install --yes screen +RUN apt-get install --yes curl + +RUN cd /tmp +RUN curl -SLO ${DCR_URL} +Run tar xvzf ${RELEASE_FILE} +Run mkdir /decred +RUN mv ${RELEASE_NAME}/dcrd /decred +RUN mv ${RELEASE_NAME}/dcrwallet /decred +RUN mv ${RELEASE_NAME}/dcrctl /decred +RUN chmod +x /decred/dcrd +RUN chmod +x /decred/dcrwallet +RUN chmod +x /decred/dcrctl + +COPY dcrd.conf /root/.dcrd/ +COPY dcrwallet.conf /root/.dcrwallet/ +COPY rpc.key /root/.dcrd/ +COPY rpc.cert /root/.dcrd/ +COPY rpc.key /root/.dcrwallet/ +COPY rpc.cert /root/.dcrwallet/ +COPY run.sh /root/ +RUN chmod +x /root/run.sh + +# Cleanup +RUN cd /root/ && rm -rf /tmp/* + +# PEER & RPC PORTS +EXPOSE 19556 18555 19557 + +ENTRYPOINT ["./root/run.sh"] diff --git a/infra/decred/dcrd.conf b/infra/decred/dcrd.conf new file mode 100644 index 00000000..5660f42a --- /dev/null +++ b/infra/decred/dcrd.conf @@ -0,0 +1,5 @@ +simnet=1 +rpcuser=user +rpcpass=password +rpclisten=0.0.0.0 +txindex=1 diff --git a/infra/decred/dcrwallet.conf b/infra/decred/dcrwallet.conf new file mode 100644 index 00000000..0de3dcae --- /dev/null +++ b/infra/decred/dcrwallet.conf @@ -0,0 +1,9 @@ +simnet=1 +pass=password +cafile=~/.dcrd/rpc.cert +rpccert=~/.dcrwallet/rpc.cert +rpckey=~/.dcrwallet/rpc.key +rpclisten=0.0.0.0 +username=user +password=password +appdata=~/.dcrwallet \ No newline at end of file diff --git a/infra/decred/keygen.go b/infra/decred/keygen.go new file mode 100644 index 00000000..f74f57ec --- /dev/null +++ b/infra/decred/keygen.go @@ -0,0 +1,28 @@ +package main + +import ( + "fmt" + + "github.com/decred/dcrd/dcrec" + "github.com/decred/dcrd/dcrec/secp256k1/v3" + "github.com/decred/dcrd/dcrutil/v3" + "github.com/decred/dcrd/chaincfg/v3" +) + +func main() { + simNetPrivKeyID := [2]byte{0x23, 0x07} + privKey, err := secp256k1.GeneratePrivateKey() + if err != nil { + panic(err) + } + wif, err := dcrutil.NewWIF(privKey.Serialize(), simNetPrivKeyID, dcrec.STEcdsaSecp256k1) + if err != nil { + panic(err) + } + addrPubKeyHash, err := dcrutil.NewAddressPubKeyHash(dcrutil.Hash160(wif.PubKey()), chaincfg.SimNetParams(), dcrec.STEcdsaSecp256k1) + if err != nil { + panic(err) + } + fmt.Printf("DECRED_PK=%v\n", wif) + fmt.Printf("DECRED_ADDRESS=%v\n", addrPubKeyHash) +} diff --git a/infra/decred/rpc.cert b/infra/decred/rpc.cert new file mode 100644 index 00000000..1241debd --- /dev/null +++ b/infra/decred/rpc.cert @@ -0,0 +1,12 @@ +-----BEGIN CERTIFICATE----- +MIIBvDCCAWKgAwIBAgIQFapkfcx1xsuJayzCsAb5ejAKBggqhkjOPQQDAjAnMREw +DwYDVQQKEwhnZW5jZXJ0czESMBAGA1UEAxMJbG9jYWxob3N0MB4XDTIxMDUxNTE0 +MjQ1MFoXDTMxMDUxNDE0MjQ1MFowJzERMA8GA1UEChMIZ2VuY2VydHMxEjAQBgNV +BAMTCWxvY2FsaG9zdDBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABF7wYx4foGY8 +9ucRNuYtciqzsq53L8SNZUqoB3rovTLJv/r/bujzMZaLhWD5QTQ88+y7CJ7RsUfr +gNDLyKahFpyjcDBuMA4GA1UdDwEB/wQEAwIChDAPBgNVHRMBAf8EBTADAQH/MB0G +A1UdDgQWBBTFJqSTEBFsGmGSza2kB1XwnARDBzAsBgNVHREEJTAjgglsb2NhbGhv +c3SHBH8AAAGHEAAAAAAAAAAAAAAAAAAAAAEwCgYIKoZIzj0EAwIDSAAwRQIhAKsW +aMDXTIYIYEH06AHiPneWPVdufNbn3INGwA5cPEoDAiAFGYejnqUGsB6tih6lKaZZ +4CX3QAo92ZnRy+qg90wX6w== +-----END CERTIFICATE----- diff --git a/infra/decred/rpc.key b/infra/decred/rpc.key new file mode 100644 index 00000000..1e1d0bc5 --- /dev/null +++ b/infra/decred/rpc.key @@ -0,0 +1,5 @@ +-----BEGIN PRIVATE KEY----- +MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgiiXTS03qdz2Zf9zg +xqi0vSra5R9HVFGMuS13t0IQS6ihRANCAARe8GMeH6BmPPbnETbmLXIqs7Kudy/E +jWVKqAd66L0yyb/6/27o8zGWi4Vg+UE0PPPsuwie0bFH64DQy8imoRac +-----END PRIVATE KEY----- diff --git a/infra/decred/run.sh b/infra/decred/run.sh new file mode 100644 index 00000000..d36b9519 --- /dev/null +++ b/infra/decred/run.sh @@ -0,0 +1,23 @@ +#!/bin/bash +ADDRESS=$1 +PRIV_KEY=$2 + +# Start +screen -dm /decred/dcrd --simnet --miningaddr=$ADDRESS +screen -dm /decred/dcrwallet --simnet --createtemp +sleep 10 + +# Print setup +echo "DECRED_ADDRESS=$ADDRESS" + +/decred/dcrctl --simnet --wallet importprivkey $PRIV_KEY + +/decred/dcrctl --simnet generate 101 + +# Simulate mining +while : +do + /decred/dcrctl --simnet generate 10 + sleep 5 +done + diff --git a/infra/docker-compose.yaml b/infra/docker-compose.yaml index 402fe7ec..73e2a5a8 100644 --- a/infra/docker-compose.yaml +++ b/infra/docker-compose.yaml @@ -50,7 +50,20 @@ services: entrypoint: - "./root/run.sh" - "${BITCOINCASH_ADDRESS}" - + # + # Decred + # + decred: + build: + context: ./decred + ports: + - "0.0.0.0:19556:19556" + - "0.0.0.0:19557:19557" + entrypoint: + - "./root/run.sh" + - "${DECRED_ADDRESS}" + - "${DECRED_PK}" + # # DigiByte # diff --git a/multichain.go b/multichain.go index ef2eee7e..22c0a04b 100644 --- a/multichain.go +++ b/multichain.go @@ -103,6 +103,7 @@ const ( BCH = Asset("BCH") // Bitcoin Cash BNB = Asset("BNB") // Binance Coin BTC = Asset("BTC") // Bitcoin + DCR = Asset("DCR") // Decred DGB = Asset("DGB") // DigiByte DOGE = Asset("DOGE") // Dogecoin ETH = Asset("ETH") // Ether @@ -134,6 +135,8 @@ func (asset Asset) OriginChain() Chain { return BinanceSmartChain case BTC: return Bitcoin + case DCR: + return Decred case DGB: return DigiByte case DOGE: @@ -173,7 +176,7 @@ func (asset Asset) OriginChain() Chain { // ChainType returns the chain-type (Account or UTXO) for the given asset func (asset Asset) ChainType() ChainType { switch asset { - case BCH, BTC, DGB, DOGE, ZEC: + case BCH, BTC, DCR, DGB, DOGE, ZEC: return ChainTypeUTXOBased case AVAX, BNB, ETH, FIL, GLMR, LUNA, MATIC: return ChainTypeAccountBased @@ -218,6 +221,7 @@ const ( BinanceSmartChain = Chain("BinanceSmartChain") Bitcoin = Chain("Bitcoin") BitcoinCash = Chain("BitcoinCash") + Decred = Chain("Decred") DigiByte = Chain("DigiByte") Dogecoin = Chain("Dogecoin") Ethereum = Chain("Ethereum") @@ -259,7 +263,7 @@ func (chain *Chain) Unmarshal(buf []byte, rem int) ([]byte, int, error) { // for the chain. func (chain Chain) ChainType() ChainType { switch chain { - case Bitcoin, BitcoinCash, DigiByte, Dogecoin, Zcash: + case Bitcoin, BitcoinCash, Decred, DigiByte, Dogecoin, Zcash: return ChainTypeUTXOBased case Avalanche, BinanceSmartChain, Ethereum, Fantom, Filecoin, Moonbeam, Polygon, Solana, Terra: return ChainTypeAccountBased @@ -301,6 +305,8 @@ func (chain Chain) NativeAsset() Asset { return BCH case Bitcoin: return BTC + case Decred: + return DCR case DigiByte: return DGB case Dogecoin: