wallet

package module
v0.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 15, 2022 License: MIT Imports: 18 Imported by: 2

README

go wallet

golang blockchain wallet wrapper

  • generate address, phrase, private key, public key
  • sign and verify message

example

import (
	"github.com/civet148/go-wallet"
	"github.com/civet148/log"
)

func main() {

    //create a new wallet
    var wc = wallet.NewWalletEthereum(wallet.OpType_Create)
    log.Infof("[CREATE] address [%s] private key [%s] public key [%s] phrase [%s]", wc.GetAddress(), wc.GetPrivateKey(), wc.GetPublicKey(), wc.GetPhrase())

    //recover a wallet from phrase
    var wr = wallet.NewWalletEthereum(wallet.OpType_Recover, wc.GetPhrase())
    log.Infof("[RECOVER] address [%s] private key [%s] public key [%s] phrase [%s]", wr.GetAddress(), wr.GetPrivateKey(), wr.GetPublicKey(), wr.GetPhrase())
    
    //load a wallet from full info
    var wl = wallet.NewWalletEthereum(wallet.OpType_Load, wc.GetAddress(), wc.GetPrivateKey(), wc.GetPublicKey(), wc.GetPhrase())
    log.Infof("[LOAD] address [%s] private key [%s] public key [%s] phrase [%s]", wl.GetAddress(), wl.GetPrivateKey(), wl.GetPublicKey(), wl.GetPhrase())
    
    //new a wallet from public key, you just can verify signature
    var wv = wallet.NewWalletEthereum(wallet.OpType_Verify, wc.GetPublicKey())
    log.Infof("[VERIFY] public key [%s]", wv.GetPublicKey())
    
    var strHelloWorld = "hello world"
    strSigned, err := wc.SignText([]byte(strHelloWorld))
    if err != nil {
        log.Errorf("sign failed [%s]", err.Error())
        panic(err.Error())
    }
    log.Infof("signature [%s]", strSigned)
    if !wv.VerifyText([]byte(strHelloWorld), strSigned) {
        log.Errorf("verify signature failed")
        panic("verify signature failed")
    } else {
        log.Infof("verify signature ok")
    }
}

Documentation

Index

Constants

View Source
const (
	TAG_VALUE_IGNORE = "-"
	TAG_NAME_JSON    = "json"
)
View Source
const (
	//Number of bytes in a pubkey.
	PublicKeyLength = 32
	//Maximum length of derived pubkey seed.
	MaxSeedLength = 32
	//Maximum number of seeds.
	MaxSeeds = 16
	//Number of bytes in a signature.
	SignatureLength = 64
	//Maximum string length of a base58 encoded pubkey.
	MaxBase58Length = 44
)
View Source
const (
	DefaultBitSize = 128
)

Variables

View Source
var (
	DerivationPath0 = "m/44'/60'/0'/0/0"
	DerivationPath1 = "m/44'/60'/0'/0/1"
)

Functions

func DecodeHexString added in v0.5.0

func DecodeHexString(s string) (b []byte, err error)

func LegacyKeccak256Hash added in v0.8.0

func LegacyKeccak256Hash(data []byte) string

func MakeHashKeccak256 added in v0.7.0

func MakeHashKeccak256(obj interface{}, excepts ...string) []byte

func MakeHashKeccak256Hex added in v0.7.0

func MakeHashKeccak256Hex(obj interface{}, excepts ...string) string

func MakeHashSHA256 added in v0.7.0

func MakeHashSHA256(obj interface{}, excepts ...string) []byte

func MakeHashSHA256Hex added in v0.7.0

func MakeHashSHA256Hex(obj interface{}, excepts ...string) string

func MakeSignString

func MakeSignString(obj interface{}, excepts ...string) string

MakeSignString make a sign string sort by alpha character

obj can be url.Values, struct with json tag or map[string]interface{}

func MustSignatureFromBase58 added in v0.9.0

func MustSignatureFromBase58(in string) solana.Signature

func PersonalSignHash added in v0.8.0

func PersonalSignHash(data []byte) string

func PublicKey2Address added in v0.2.0

func PublicKey2Address(strPublicKey string) (string, error)

func RecoverKeccak256Hash added in v0.8.0

func RecoverKeccak256Hash(strHash, strSignature string) (string, error)

func RecoverKeccak256Msg added in v0.8.0

func RecoverKeccak256Msg(strMsg, strSignature string) (string, error)

RecoverKeccak256Msg for metamask eth_sign signature

func RecoverLegacyKeccak256Hash added in v0.8.0

func RecoverLegacyKeccak256Hash(strHash, strSignature string) (strPubKey string, err error)

func RecoverLegacyKeccak256Msg added in v0.8.0

func RecoverLegacyKeccak256Msg(strMsg, strSignature string) (string, error)

func VerifyKeccak256 added in v0.8.0

func VerifyKeccak256(strAddress, strMsg, strSignature string) (bool, error)

VerifyKeccak256 for metamask eth_sign signature verification

func VerifyLegacyKeccak256 added in v0.8.0

func VerifyLegacyKeccak256(strAddress, strMsg, strSignature string) (bool, error)

VerifyLegacyKeccak256 for BCOS signature verification

func VerifySignatureSolana added in v0.9.0

func VerifySignatureSolana(strAddress, strMsg, strSignature string) (bool, error)

VerifySignatureSolana for Solana phantom signature verification strAddress base58 encoded public key strSignature base58 encoded signature strMsg base64 encoded message

Types

type OpType

type OpType int
const (
	OpType_Load    OpType = 1
	OpType_Create  OpType = 2
	OpType_Recover OpType = 3
	OpType_Verify  OpType = 4
)

func (OpType) GoString

func (t OpType) GoString() string

func (OpType) String

func (t OpType) String() string

type Wallet

type Wallet interface {
	Create() Wallet                                                    //create a new wallet
	Recover(strPhrase string) Wallet                                   //recover wallet from phrase
	SignHash(digestHash []byte) (strSignature string, err error)       //sign hash bytes (32bytes) and return a hex signature string
	VerifyHash(digestHash []byte, strSignature string) bool            //verify hash bytes (32bytes) signature from a hex encoded signature
	MakeHash(data []byte) []byte                                       //make hash (ethereum: legacy keccak256)
	SignText(text []byte) (strMsgHash, strSignature string, err error) //sign text bytes and return a hex signature string
	VerifyText(text []byte, strSignature string) bool                  //verify text bytes signature from a hex encoded signature
	GetPrivateKey() string                                             //retrieve private key (hex string)
	GetPublicKey() string                                              //retrieve public key (hex string)
	GetAddress() string                                                //retrieve address key (hex string)
	GetPhrase() string                                                 //retrieve phrase key (hex string)
}

func NewWalletEthereum

func NewWalletEthereum(op OpType, args ...string) Wallet

type WalletEthereum

type WalletEthereum struct {
	Address    string
	PrivateKey string
	PublicKey  string
	Phrase     string
	Seed       string
}

func (*WalletEthereum) Create

func (m *WalletEthereum) Create() Wallet

func (*WalletEthereum) GetAddress

func (m *WalletEthereum) GetAddress() string

func (*WalletEthereum) GetPhrase

func (m *WalletEthereum) GetPhrase() string

func (*WalletEthereum) GetPrivateKey

func (m *WalletEthereum) GetPrivateKey() string

func (*WalletEthereum) GetPublicKey

func (m *WalletEthereum) GetPublicKey() string

func (*WalletEthereum) MakeHash added in v0.8.0

func (m *WalletEthereum) MakeHash(data []byte) []byte

MakeHash legacy keccak256 hash algorithm, return [32]byte

func (*WalletEthereum) MakeHashString added in v0.8.0

func (m *WalletEthereum) MakeHashString(data []byte) string

MakeHashString legacy keccak256 hash algorithm return hex string

func (*WalletEthereum) Recover

func (m *WalletEthereum) Recover(strPhrase string) Wallet

func (*WalletEthereum) SignHash

func (m *WalletEthereum) SignHash(digestHash []byte) (strSignature string, err error)

func (*WalletEthereum) SignText

func (m *WalletEthereum) SignText(text []byte) (strMsgHash, strSignature string, err error)

func (*WalletEthereum) VerifyHash

func (m *WalletEthereum) VerifyHash(digestHash []byte, strSignature string) bool

func (*WalletEthereum) VerifyText

func (m *WalletEthereum) VerifyText(text []byte, strSignature string) bool

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL