Documentation ¶
Index ¶
- Constants
- func CompressPubkey(pubkey *ecdsa.PublicKey) []byte
- func DecompressPubkey(pubkey []byte) (*ecdsa.PublicKey, error)
- func FromECDSA(priv *ecdsa.PrivateKey) []byte
- func FromECDSAPub(pub *ecdsa.PublicKey) []bytedeprecated
- func FromWIF(wif string) (*ecdsa.PrivateKey, error)
- func GenerateKey() (*ecdsa.PrivateKey, error)
- func GetRandomBytes(len int) ([]byte, error)
- func GetRandomNonce() ([]byte, error)
- func Hash160(buf []byte) []byte
- func HexToECDSA(hexkey string) (*ecdsa.PrivateKey, error)
- func Keccak256(data ...[]byte) []byte
- func Keccak256Hash(data ...[]byte) (h common.Hash)
- func LoadECDSA(file string) (*ecdsa.PrivateKey, error)
- func P256FromECDSAPub(pub *ecdsa.PublicKey) []byte
- func P256ToECDSA(d []byte) (*ecdsa.PrivateKey, error)
- func P256ToECDSAPub(pub []byte) *ecdsa.PublicKey
- func PrvKeyToWIF(prvKey *ecdsa.PrivateKey) string
- func PubkeyBytesToAddress(pubKeyCompressBytes []byte) common.Address
- func RequestIdToContractAddress(deployRequestId common.Hash) common.Address
- func S256() elliptic.Curve
- func SaveECDSA(file string, key *ecdsa.PrivateKey) error
- func ScriptHashToAddress(scriptHash []byte) common.Address
- func ScriptToAddress(redeemScript []byte) common.Address
- func Sign(hash []byte, prv *ecdsa.PrivateKey) ([]byte, error)
- func ToECDSA(d []byte) (*ecdsa.PrivateKey, error)
- func ToECDSAPub(pub []byte) *ecdsa.PublicKey
- func ToECDSAUnsafe(d []byte) *ecdsa.PrivateKey
- func ToWIF(prvKey []byte) string
- func ValidateSignatureValues(v byte, r, s *big.Int, homestead bool) bool
- func VerifySignature(pubkey, hash, signature []byte) bool
- type CryptoGm
- func (c *CryptoGm) Decrypt(key, ciphertext []byte) (plaintext []byte, err error)
- func (c *CryptoGm) Encrypt(key []byte, plaintext []byte) (ciphertext []byte, err error)
- func (c *CryptoGm) GetHash() (h hash.Hash, err error)
- func (c *CryptoGm) Hash(msg []byte) (hash []byte, err error)
- func (c *CryptoGm) KeyGen() ([]byte, error)
- func (c *CryptoGm) PrivateKeyToInstance(privKey []byte) (interface{}, error)
- func (c *CryptoGm) PrivateKeyToPubKey(privKey []byte) ([]byte, error)
- func (c *CryptoGm) Sign(privKey, message []byte) (signature []byte, err error)
- func (c *CryptoGm) Verify(pubKey, signature, message []byte) (valid bool, err error)
- type CryptoP256
- func (c *CryptoP256) Decrypt(key, ciphertext []byte) (plaintext []byte, err error)
- func (c *CryptoP256) Encrypt(key []byte, plaintext []byte) (ciphertext []byte, err error)
- func (c *CryptoP256) GetHash() (h hash.Hash, err error)
- func (c *CryptoP256) Hash(msg []byte) (hash []byte, err error)
- func (c *CryptoP256) KeyGen() (privKey []byte, err error)
- func (c *CryptoP256) PrivateKeyToInstance(privKey []byte) (interface{}, error)
- func (c *CryptoP256) PrivateKeyToPubKey(privKey []byte) ([]byte, error)
- func (c *CryptoP256) Sign(privKey, message []byte) (signature []byte, err error)
- func (c *CryptoP256) Verify(pubKey, signature []byte, message []byte) (valid bool, err error)
- type CryptoS256
- func (c *CryptoS256) Decrypt(key, ciphertext []byte) (plaintext []byte, err error)
- func (c *CryptoS256) Encrypt(key []byte, plaintext []byte) (ciphertext []byte, err error)
- func (c *CryptoS256) GetHash() (h hash.Hash, err error)
- func (c *CryptoS256) Hash(msg []byte) (hash []byte, err error)
- func (c *CryptoS256) KeyGen() (privKey []byte, err error)
- func (c *CryptoS256) PrivateKeyToInstance(privKey []byte) (interface{}, error)
- func (c *CryptoS256) PrivateKeyToPubKey(privKey []byte) ([]byte, error)
- func (c *CryptoS256) Sign(privKey, message []byte) (signature []byte, err error)
- func (c *CryptoS256) Verify(pubKey, signature, message []byte) (valid bool, err error)
- type ECDSASignature
- type ICrypto
Constants ¶
const (
// NonceSize is the default NonceSize
NonceSize = 24
)
Variables ¶
This section is empty.
Functions ¶
func CompressPubkey ¶
CompressPubkey encodes a public key to the 33-byte compressed format.
func DecompressPubkey ¶
DecompressPubkey parses a public key in the 33-byte compressed format.
func FromECDSA ¶
func FromECDSA(priv *ecdsa.PrivateKey) []byte
FromECDSA exports a private key into a binary dump.
func FromECDSAPub
deprecated
func FromWIF ¶
func FromWIF(wif string) (*ecdsa.PrivateKey, error)
Import a WIF string to get a private key
func GenerateKey ¶
func GenerateKey() (*ecdsa.PrivateKey, error)
func GetRandomBytes ¶
GetRandomBytes returns len random looking bytes
func GetRandomNonce ¶
GetRandomNonce returns a random byte array of length NonceSize
func HexToECDSA ¶
func HexToECDSA(hexkey string) (*ecdsa.PrivateKey, error)
HexToECDSA parses a secp256k1 private key.
func Keccak256Hash ¶
Keccak256Hash calculates and returns the Keccak256 hash of the input data, converting it to an internal Hash data structure.
func LoadECDSA ¶
func LoadECDSA(file string) (*ecdsa.PrivateKey, error)
LoadECDSA loads a secp256k1 private key from the given file.
func P256FromECDSAPub ¶ added in v1.0.1
func P256ToECDSA ¶ added in v1.0.1
func P256ToECDSA(d []byte) (*ecdsa.PrivateKey, error)
func P256ToECDSAPub ¶ added in v1.0.1
func PrvKeyToWIF ¶
func PrvKeyToWIF(prvKey *ecdsa.PrivateKey) string
func PubkeyBytesToAddress ¶
This is only for P2PKH account address.
func PubkeyToAddress(p *ecdsa.PublicKey) common.Address { pubBytes := CompressPubkey(p) return PubkeyBytesToAddress(pubBytes) }
func SaveECDSA ¶
func SaveECDSA(file string, key *ecdsa.PrivateKey) error
SaveECDSA saves a secp256k1 private key to the given file with restrictive permissions. The key data is saved hex-encoded.
func ScriptHashToAddress ¶
func ScriptToAddress ¶
This is for P2SH address, start with P3
func Sign ¶
func Sign(hash []byte, prv *ecdsa.PrivateKey) ([]byte, error)
// Ecrecover returns the uncompressed public key that created the given signature.
func Ecrecover(hash, sig []byte) ([]byte, error) { pub, err := SigToPub(hash, sig) if err != nil { return nil, err } bytes := (*btcec.PublicKey)(pub).SerializeUncompressed() return bytes, err }
// SigToPub returns the public key that created the given signature.
func SigToPub(hash, sig []byte) (*ecdsa.PublicKey, error) { // Convert to btcec input format with 'recovery id' v at the beginning. btcsig := make([]byte, 65) btcsig[0] = sig[64] + 27 copy(btcsig[1:], sig) pub, _, err := btcec.RecoverCompact(btcec.S256(), btcsig, hash) return (*ecdsa.PublicKey)(pub), err }
Sign calculates an ECDSA signature.
This function is susceptible to chosen plaintext attacks that can leak information about the private key that is used for signing. Callers must be aware that the given hash cannot be chosen by an adversery. Common solution is to hash any input before calculating the signature.
The produced signature is in the [R || S ] .
func ToECDSA ¶
func ToECDSA(d []byte) (*ecdsa.PrivateKey, error)
ToECDSA creates a private key with the given D value.
func ToECDSAPub ¶
func ToECDSAUnsafe ¶
func ToECDSAUnsafe(d []byte) *ecdsa.PrivateKey
ToECDSAUnsafe blindly converts a binary blob to a private key. It should almost never be used unless you are sure the input is valid and want to avoid hitting errors due to bad origin encoding (0 prefixes cut off).
func ValidateSignatureValues ¶
ValidateSignatureValues verifies whether the signature values are valid with the given chain rules. The v value is assumed to be either 0 or 1.
func VerifySignature ¶
VerifySignature checks that the given public key created signature over hash. The public key should be in compressed (33 bytes) or uncompressed (65 bytes) format. The signature should have the 64 byte [R || S] format.
Types ¶
type CryptoGm ¶ added in v1.0.1
type CryptoGm struct { }
func (*CryptoGm) PrivateKeyToInstance ¶ added in v1.0.1
func (*CryptoGm) PrivateKeyToPubKey ¶ added in v1.0.1
type CryptoP256 ¶ added in v1.0.1
type CryptoP256 struct { }
func (*CryptoP256) Decrypt ¶ added in v1.0.1
func (c *CryptoP256) Decrypt(key, ciphertext []byte) (plaintext []byte, err error)
func (*CryptoP256) Encrypt ¶ added in v1.0.1
func (c *CryptoP256) Encrypt(key []byte, plaintext []byte) (ciphertext []byte, err error)
func (*CryptoP256) GetHash ¶ added in v1.0.1
func (c *CryptoP256) GetHash() (h hash.Hash, err error)
func (*CryptoP256) Hash ¶ added in v1.0.1
func (c *CryptoP256) Hash(msg []byte) (hash []byte, err error)
func (*CryptoP256) KeyGen ¶ added in v1.0.1
func (c *CryptoP256) KeyGen() (privKey []byte, err error)
func (*CryptoP256) PrivateKeyToInstance ¶ added in v1.0.1
func (c *CryptoP256) PrivateKeyToInstance(privKey []byte) (interface{}, error)
func (*CryptoP256) PrivateKeyToPubKey ¶ added in v1.0.1
func (c *CryptoP256) PrivateKeyToPubKey(privKey []byte) ([]byte, error)
type CryptoS256 ¶ added in v1.0.1
type CryptoS256 struct { }
func (*CryptoS256) Decrypt ¶ added in v1.0.1
func (c *CryptoS256) Decrypt(key, ciphertext []byte) (plaintext []byte, err error)
func (*CryptoS256) Encrypt ¶ added in v1.0.1
func (c *CryptoS256) Encrypt(key []byte, plaintext []byte) (ciphertext []byte, err error)
func (*CryptoS256) GetHash ¶ added in v1.0.1
func (c *CryptoS256) GetHash() (h hash.Hash, err error)
func (*CryptoS256) Hash ¶ added in v1.0.1
func (c *CryptoS256) Hash(msg []byte) (hash []byte, err error)
func (*CryptoS256) KeyGen ¶ added in v1.0.1
func (c *CryptoS256) KeyGen() (privKey []byte, err error)
func (*CryptoS256) PrivateKeyToInstance ¶ added in v1.0.1
func (c *CryptoS256) PrivateKeyToInstance(privKey []byte) (interface{}, error)
func (*CryptoS256) PrivateKeyToPubKey ¶ added in v1.0.1
func (c *CryptoS256) PrivateKeyToPubKey(privKey []byte) ([]byte, error)
type ECDSASignature ¶ added in v1.0.1
type ICrypto ¶ added in v1.0.1
type ICrypto interface { // KeyGen generates a key using opts. KeyGen() (privKey []byte, err error) PrivateKeyToPubKey(privKey []byte) (pubKey []byte, err error) PrivateKeyToInstance(privKey []byte) (interface{}, error) // Hash hashes messages msg using options opts. // If opts is nil, the default hash function will be used. Hash(msg []byte) (hash []byte, err error) // GetHash returns and instance of hash.Hash using options opts. // If opts is nil, the default hash function will be returned. GetHash() (h hash.Hash, err error) // Sign signs digest using key k. // The opts argument should be appropriate for the algorithm used. // // Note that when a signature of a hash of a larger message is needed, // the caller is responsible for hashing the larger message and passing // the hash (as digest). Sign(privKey, message []byte) (signature []byte, err error) // Verify verifies signature against key k and digest // The opts argument should be appropriate for the algorithm used. Verify(pubKey, signature, message []byte) (valid bool, err error) // Encrypt encrypts plaintext using key k. // The opts argument should be appropriate for the algorithm used. Encrypt(key []byte, plaintext []byte) (ciphertext []byte, err error) // Decrypt decrypts ciphertext using key k. // The opts argument should be appropriate for the algorithm used. Decrypt(key, ciphertext []byte) (plaintext []byte, err error) }
var MyCryptoLib ICrypto = &CryptoS256{}