• Stars
    star
    107
  • Rank 323,587 (Top 7 %)
  • Language
    Go
  • License
    MIT License
  • Created over 8 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

P2P SPV Wallet/Library in Go used in OpenBazaar 2.0

Build Status Coverage Status Go Report Card

spvwallet

Lightweight p2p SPV wallet and library in Go. It connects directly to the bitcoin p2p network to fetch headers, merkle blocks, and transactions.

It uses a number of utilities from btcsuite but natively handles blockchain and wallet.

Library Usage:

// Create a new config
config := spvwallet.NewDefaultConfig()

// Select network
config.Params = &chaincfg.TestNet3Params

// Select wallet datastore
sqliteDatastore, _ := db.Create(config.RepoPath)
config.DB = sqliteDatastore

// Create the wallet
wallet, _ := spvwallet.NewSPVWallet(config)

// Start it!
go wallet.Start()

Easy peasy

The wallet implements the following interface:

type BitcoinWallet interface {

	// Start the wallet
	Start()

	// Return the network parameters
	Params() *chaincfg.Params

	// Returns the type of crytocurrency this wallet implements
	CurrencyCode() string

	// Check if this amount is considered dust
	IsDust(amount int64) bool

	// Get the master private key
	MasterPrivateKey() *hd.ExtendedKey

	// Get the master public key
	MasterPublicKey() *hd.ExtendedKey

	// Get the current address for the given purpose
	CurrentAddress(purpose spvwallet.KeyPurpose) btc.Address

	// Returns a fresh address that has never been returned by this function
	NewAddress(purpose spvwallet.KeyPurpose) btc.Address

	// Parse the address string and return an address interface
	DecodeAddress(addr string) (btc.Address, error)

	// Turn the given output script into an address
	ScriptToAddress(script []byte) (btc.Address, error)

	// Turn the given address into an output script
	AddressToScript(addr btc.Address) ([]byte, error)

	// Returns if the wallet has the key for the given address
	HasKey(addr btc.Address) bool

	// Get the confirmed and unconfirmed balances
	Balance() (confirmed, unconfirmed int64)

	// Returns a list of transactions for this wallet
	Transactions() ([]spvwallet.Txn, error)

	// Get info on a specific transaction
	GetTransaction(txid chainhash.Hash) (spvwallet.Txn, error)

	// Get the height of the blockchain
	ChainTip() uint32

	// Get the current fee per byte
	GetFeePerByte(feeLevel spvwallet.FeeLevel) uint64

	// Send bitcoins to an external wallet
	Spend(amount int64, addr btc.Address, feeLevel spvwallet.FeeLevel) (*chainhash.Hash, error)

	// Bump the fee for the given transaction
	BumpFee(txid chainhash.Hash) (*chainhash.Hash, error)

	// Calculates the estimated size of the transaction and returns the total fee for the given feePerByte
	EstimateFee(ins []spvwallet.TransactionInput, outs []spvwallet.TransactionOutput, feePerByte uint64) uint64

	// Build and broadcast a transaction that sweeps all coins from an address. If it is a p2sh multisig, the redeemScript must be included
	SweepAddress(utxos []spvwallet.Utxo, address *btc.Address, key *hd.ExtendedKey, redeemScript *[]byte, feeLevel spvwallet.FeeLevel) (*chainhash.Hash, error)

	// Create a signature for a multisig transaction
	CreateMultisigSignature(ins []spvwallet.TransactionInput, outs []spvwallet.TransactionOutput, key *hd.ExtendedKey, redeemScript []byte, feePerByte uint64) ([]spvwallet.Signature, error)

	// Combine signatures and optionally broadcast
	Multisign(ins []spvwallet.TransactionInput, outs []spvwallet.TransactionOutput, sigs1 []spvwallet.Signature, sigs2 []spvwallet.Signature, redeemScript []byte, feePerByte uint64, broadcast bool) ([]byte, error)

	// Generate a multisig script from public keys. If a timeout is included the returned script should be a timelocked escrow which releases using the timeoutKey.
	GenerateMultisigScript(keys []hd.ExtendedKey, threshold int, timeout time.Duration, timeoutKey *hd.ExtendedKey) (addr btc.Address, redeemScript []byte, err error)

	// Add a script to the wallet and get notifications back when coins are received or spent from it
	AddWatchedScript(script []byte) error

	// Add a callback for incoming transactions
	AddTransactionListener(func(spvwallet.TransactionCallback))

	// Use this to re-download merkle blocks in case of missed transactions
	ReSyncBlockchain(fromHeight int32)

	// Return the number of confirmations and the height for a transaction
	GetConfirmations(txid chainhash.Hash) (confirms, atHeight uint32, err error)

	// Cleanly disconnect from the wallet
	Close()
}

To create a wallet binary:

make install

Usage:

Usage:
  spvwallet [OPTIONS] <command>

Help Options:
  -h, --help  Show this help message

Available commands:
  addwatchedscript         add a script to watch
  balance                  get the wallet balance
  bumpfee                  bump the tx fee
  chaintip                 return the height of the chain
  createmultisigsignature  create a p2sh multisig signature
  currentaddress           get the current bitcoin address
  dumpheaders              print the header database
  estimatefee              estimate the fee for a tx
  getconfirmations         get the number of confirmations for a tx
  getfeeperbyte            get the current bitcoin fee
  gettransaction           get a specific transaction
  haskey                   does key exist
  masterprivatekey         get the wallet's master private key
  masterpublickey          get the wallet's master public key
  multisign                combine multisig signatures
  newaddress               get a new bitcoin address
  peers                    get info about peers
  resyncblockchain         re-download the chain of headers
  spend                    send bitcoins
  start                    start the wallet
  stop                     stop the wallet
  sweepaddress             sweep all coins from an address
  transactions             get a list of transactions
  version                  print the version number

Finally a gRPC API is available on port 8234. The same interface is exposed via the API plus a streaming wallet notifier which fires when a new transaction (either incoming or outgoing) is recorded then again when it gains its first confirmation.

This library began it's life as a library called uspv written by Thaddeus Dryja for the lnd. Several files still contain the original code.

More Repositories

1

openbazaar-go

OpenBazaar 2.0 Server Daemon in Go
Go
994
star
2

openbazaar-desktop

OpenBazaar 2.0 Desktop Client (talks to openbazaar-go server daemon)
JavaScript
647
star
3

OpenBazaar-Server

(Deprecated) OpenBazaar 1.0 Server daemon for communication with OpenBazaar-Client
Python
608
star
4

OpenBazaar-Client

(DEPRECATED) Front-end Electron application for talking with the OpenBazaar-Server daemon
JavaScript
414
star
5

OpenBazaar-Installer

Process for building the OpenBazaar 1.0 executables on OSX, Linux and Windows
Python
124
star
6

go-onion-transport

Tor onion transport for IPFS
Go
110
star
7

multiwallet

API based multi-cryptocurrency wallet
Go
73
star
8

smart-contracts

Collection of OpenBazaar smart contracts
JavaScript
65
star
9

openbazaar3-rust

Rust implementation of OpenBazaar 3.0
Rust
61
star
10

haven

JavaScript
31
star
11

libsignal

Custom implementation of the signal messaging protocol in Go
Go
27
star
12

openbazaar-awesome

Useful resources for using OpenBazaar and building things on top of it
20
star
13

docs

OpenBazaar documentation
19
star
14

wallet-interface

Interfaces for the openbazaar-go wallet
Go
18
star
15

go-ethwallet

Ethereum wallet used in OpenBazaar
Go
18
star
16

PressKit

This repository contains information for the media about the OpenBazaar project.
PostScript
12
star
17

official_site

OpenBazaar.org official site code
HTML
12
star
18

openbazaar-web

OpenBazaar 2.0 browser-based client.
JavaScript
10
star
19

obips

OpenBazaar Improvement Proposals
9
star
20

openbazaar.com

Browse only UI
HTML
9
star
21

wns

Worldwide OpenBazaar resource finder naming service (WorfNS)
JavaScript
6
star
22

tickerproxy

Price data feed for the OpenBazaar network
Go
6
star
23

webrelay

Relay node to bridge the browser and desktop networks
Go
5
star
24

mobile-server

Repo to store iOS framework and Android libraries for development; official releases are in the openbazaar-go repo.
4
star
25

feeproxy

Serve Bitcoin fee via standardized API
Go
3
star
26

ob2-importer

CSS
3
star
27

leveldb-repair

Repairs a corrupted leveldb instance.
Go
3
star
28

bitcoind-wallet

OpenBazaar plugin for a bitcoind based wallet
Go
2
star
29

go-blockstackclient

Resolve blockchainIDs to OpenBazaar IDs
Go
2
star
30

Teflon

p2p marketplace on Optimism Layer 2 leverage stablecoin and NFTs
1
star
31

official_docs

HTML
1
star
32

downloadcounter

It counts downloads
Go
1
star
33

jsonpb

Modified Protobuf to JSON serializer in Go
Go
1
star