您正在查看: 2021年3月

由Golang实现的多币种HD钱包

https://github.com/foxnut/go-hdwallet

supported coins

  • BTC
  • LTC
  • DOGE
  • DASH
  • ETH
  • ETC
  • BCH
  • QTUM
  • USDT
  • IOST
  • USDC

install

go get -v -u github.com/foxnut/go-hdwallet

example

package main

import (
    "fmt"

    "github.com/foxnut/go-hdwallet"
)

var (
    mnemonic = "range sheriff try enroll deer over ten level bring display stamp recycle"
)

func main() {
    master, err := hdwallet.NewKey(
        hdwallet.Mnemonic(mnemonic),
    )
    if err != nil {
        panic(err)
    }

    // BTC: 1AwEPfoojHnKrhgt1vfuZAhrvPrmz7Rh4
    wallet, _ := master.GetWallet(hdwallet.CoinType(hdwallet.BTC), hdwallet.AddressIndex(1))
    address, _ := wallet.GetAddress()
    addressP2WPKH, _ := wallet.GetKey().AddressP2WPKH()
    addressP2WPKHInP2SH, _ := wallet.GetKey().AddressP2WPKHInP2SH()
    fmt.Println("BTC: ", address, addressP2WPKH, addressP2WPKHInP2SH)

    // BCH: 1CSBT18sjcCwLCpmnnyN5iqLc46Qx7CC91
    wallet, _ = master.GetWallet(hdwallet.CoinType(hdwallet.BCH))
    address, _ = wallet.GetAddress()
    addressBCH, _ := wallet.GetKey().AddressBCH()
    fmt.Println("BCH: ", address, addressBCH)

    // LTC: LLCaMFT8AKjDTvz1Ju8JoyYXxuug4PZZmS
    wallet, _ = master.GetWallet(hdwallet.CoinType(hdwallet.LTC))
    address, _ = wallet.GetAddress()
    fmt.Println("LTC: ", address)

    // DOGE: DHLA3rJcCjG2tQwvnmoJzD5Ej7dBTQqhHK
    wallet, _ = master.GetWallet(hdwallet.CoinType(hdwallet.DOGE))
    address, _ = wallet.GetAddress()
    fmt.Println("DOGE:", address)

    // ETH: 0x37039021cBA199663cBCb8e86bB63576991A28C1
    wallet, _ = master.GetWallet(hdwallet.CoinType(hdwallet.ETH))
    address, _ = wallet.GetAddress()
    fmt.Println("ETH: ", address)

    // ETC: 0x480C69E014C7f018dAbF17A98273e90f0b0680cf
    wallet, _ = master.GetWallet(hdwallet.CoinType(hdwallet.ETC))
    address, _ = wallet.GetAddress()
    fmt.Println("ETC: ", address)
}

以太坊观察者

https://github.com/HydroProtocol/ethereum-watcher

ethereum-watcher是用Golang编写的以太坊区块链的事件监听器。使用以太坊观察器,您可以监视和跟踪以太坊区块链上发生的当前或历史事件。

背景

与以太坊区块链交互的许多应用程序需要知道链上何时发生特定动作,但无法直接访问链上数据。ethereum-watcher充当应用程序和链之间的接口:从区块链收集指定的数据,以便应用程序可以与链上事件进行更无缝的交互。

特征

插件友好。您可以轻松地向以太坊观察者添加一个插件,以侦听任何类型的链上事件。
叉公差。如果发生派生,则将一条还原消息发送给订户。

简单的以太坊API以获取您的ERC20代币余额以及有用的信息

https://github.com/hunterlong/tokenbalance

import (
    "github.com/hunterlong/tokenbalance"
)

func main() {
    // connect to your Geth Server
    configs = &tokenbalance.Config{
         GethLocation: "https://eth.coinapp.io",
         Logs:         true,
    }
    configs.Connect()

    // insert a Token Contract address and Wallet address
    contract := "0x86fa049857e0209aa7d9e616f7eb3b3b78ecfdb0"
    wallet := "0xbfaa1a1ea534d35199e84859975648b59880f639"

    // query the blockchain and wallet details
    token, err := tokenbalance.New(contract, wallet)

    // Token Balance will respond back useful things
    token.BalanceString()  // "600000.0"
    token.ETHString()      // "1.020095885777777767"
    token.Name             // "OmiseGO"
    token.Symbol           // "OMG"
    token.Decimals         // 18
    token.Balance          // big.Int() (token balance)
    token.ETH              // big.Int() (ether balance)
}

Ethereum HD Wallet derivations in Go

https://github.com/miguelmota/go-ethereum-hdwallet.git

Install

go get -u github.com/miguelmota/go-ethereum-hdwallet

Documenation

https://godoc.org/github.com/miguelmota/go-ethereum-hdwallet

Getting started

package main

import (
    "fmt"
    "log"

    "github.com/miguelmota/go-ethereum-hdwallet"
)

func main() {
    mnemonic := "tag volcano eight thank tide danger coast health above argue embrace heavy"
    wallet, err := hdwallet.NewFromMnemonic(mnemonic)
    if err != nil {
        log.Fatal(err)
    }

    path := hdwallet.MustParseDerivationPath("m/44'/60'/0'/0/0")
    account, err := wallet.Derive(path, false)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(account.Address.Hex()) // 0xC49926C4124cEe1cbA0Ea94Ea31a6c12318df947

    path = hdwallet.MustParseDerivationPath("m/44'/60'/0'/0/1")
    account, err = wallet.Derive(path, false)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(account.Address.Hex()) // 0x8230645aC28A4EdD1b0B53E7Cd8019744E9dD559
}

Signing transaction

package main

import (
    "log"
    "math/big"

    "github.com/davecgh/go-spew/spew"
    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/core/types"
    "github.com/miguelmota/go-ethereum-hdwallet"
)

func main() {
    mnemonic := "tag volcano eight thank tide danger coast health above argue embrace heavy"
    wallet, err := hdwallet.NewFromMnemonic(mnemonic)
    if err != nil {
        log.Fatal(err)
    }

    path := hdwallet.MustParseDerivationPath("m/44'/60'/0'/0/0")
    account, err := wallet.Derive(path, true)
    if err != nil {
        log.Fatal(err)
    }

    nonce := uint64(0)
    value := big.NewInt(1000000000000000000)
    toAddress := common.HexToAddress("0x0")
    gasLimit := uint64(21000)
    gasPrice := big.NewInt(21000000000)
    var data []byte

    tx := types.NewTransaction(nonce, toAddress, value, gasLimit, gasPrice, data)
    signedTx, err := wallet.SignTx(account, tx, nil)
    if err != nil {
        log.Fatal(err)
    }

    spew.Dump(signedTx)
}

CLI

go get -u github.com/miguelmota/go-ethereum-hdwallet/cmd/geth-hdwallet
$ geth-hdwallet -mnemonic "tag volcano eight thank tide danger coast health above argue embrace heavy" -path "m/44'/60'/0'/0/0"

public address: 0xC49926C4124cEe1cbA0Ea94Ea31a6c12318df947
private key: 63e21d10fd50155dbba0e7d3f7431a400b84b4c2ac1ee38872f82448fe3ecfb9

Test

make test