Go client for BitGo.com API v2
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
cmd
testdata
workdir
.gitignore
.travis.yml
LICENSE
README.md
bitgo.go
bitgo_test.go
error.go
error_test.go
fuzz.go
fuzz_test.go
log.go
wallet.go
wallet_test.go

README.md

Go client for BitGo.com API v2

Documentation Go Report Card Travis CI

This is unofficial API client. There are no plans to implement all resources.

Consolidate Wallet Unspents

This API call will consolidate Bitcoin Cash of 585951a5df8380e0e3063e9f wallet using max 0.001 BCH unspents with 5000 satoshis/kb fee rate. You can stop consolidation by cancelling a ctx context.

c := bitgo.NewClient(
	bitgo.WithCoin("bch"),
	bitgo.WithAccesToken("swordfish"),
)
tx, err := c.Wallet.Consolidate(ctx, "585951a5df8380e0e3063e9f", &bitgo.WalletConsolidateParams{
	WalletPassphrase: "root",
	MaxValue:         100000,
	FeeRate:          5000,
})
if err != nil {
	log.Fatalf("Failed to coalesce unspents: %v", err)
}
fmt.Printf("Consolidated transaction ID: %s", tx.TxID)

There is a CLI program to consolidate unspensts of a wallet.

$ go build ./cmd/consolidate/
$ ./consolidate -token=swordfish -coin=bch -wallet=585951a5df8380e0e3063e9f -passphrase=root -max-value=0.001 -fee-rate=5000
5885a7e6c7802206f69655ed763d14f101cf46501aef38e275c67c72cfcedb75

Also you can perform periodic consolidation with consolidated just like you would do with consolidate program. In this example consolidation runs every 12 hours.

$ go build ./cmd/consolidated/
$ ./consolidated -schedule=12h -token=swordfish -coin=bch -wallet=585951a5df8380e0e3063e9f -passphrase=root -max-value=0.001 -fee-rate=5000
5885a7e6c7802206f69655ed763d14f101cf46501aef38e275c67c72cfcedb75

List Wallet Unspents

This API call will retrieve the unspent transaction outputs (UTXOs) within a wallet. For example, we want to request 58ae81a5df8380e0e307e876 wallet's confirmed unspents and print amounts in BTC. You can stop pagination by cancelling a ctx context.

c := bitgo.NewClient(
	bitgo.WithCoin("bch"),
	bitgo.WithAccesToken("swordfish"),
)
params := url.Values{}
params.Set("minConfirms", "1")
err := c.Wallet.Unspents(ctx, "58ae81a5df8380e0e307e876", params, func(list *bitgo.UnspentList) {
	for _, utxo := range list.Unspents {
		fmt.Printf("%0.8f\n", toBitcoins(utxo.Value))
	}
})

There is a CLI program to list all unspensts of a wallet.

$ go build ./cmd/utxo/
$ ./utxo -token=swordfish -coin=bch -wallet=58ae81a5df8380e0e307e876 -min-confirms=1
0.00000117
0.00000001
0.00000001
0.00000562
0.00000001
0.00000562

You can use it to get a rough idea about unspents available in the wallet.

$ ./utxo -token=swordfish -coin=bch -wallet=58ae81a5df8380e0e307e876 > unspents.txt
$ sort unspents.txt | uniq -c | sort -n -r
   3 0.00000001
   2 0.00000562
   1 0.00000117

Error Handling

Dave Cheney recommends asserting errors for behaviour, not type.

package main

import (
	"fmt"

	"github.com/marselester/bitgo-v2"
	"github.com/pkg/errors"
)

// IsUnauthorized returns true if err caused by authentication problem.
func IsUnauthorized(err error) bool {
	e, ok := errors.Cause(err).(interface {
		IsUnauthorized() bool
	})
	return ok && e.IsUnauthorized()
}

func main() {
	err := bitgo.Error{Type: bitgo.ErrorTypeAuthentication}
	fmt.Println(IsUnauthorized(err))
	fmt.Println(IsUnauthorized(fmt.Errorf("")))
	fmt.Println(IsUnauthorized(nil))
	// Output: true
	// false
	// false
}

Testing

Quick tutorial on how to fuzz by Damian Gryski. Copy JSON files from testdata into workdir/corpus as sample inputs.

$ go-fuzz-build github.com/marselester/bitgo-v2
$ go-fuzz -bin=bitgo-fuzz.zip -workdir=workdir