mirror of
https://github.com/fluencelabs/tendermint
synced 2025-05-28 13:41:21 +00:00
commit
0ca2c6fdb0
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
*.swp
|
||||
*.swo
|
837
README.md
Normal file
837
README.md
Normal file
@ -0,0 +1,837 @@
|
||||
|
||||
|
||||
# crypto
|
||||
`import "github.com/tendermint/go-crypto"`
|
||||
|
||||
* [Overview](#pkg-overview)
|
||||
* [Index](#pkg-index)
|
||||
* [Subdirectories](#pkg-subdirectories)
|
||||
|
||||
## <a name="pkg-overview">Overview</a>
|
||||
|
||||
|
||||
|
||||
## <a name="pkg-index">Index</a>
|
||||
* [Constants](#pkg-constants)
|
||||
* [func CRandBytes(numBytes int) []byte](#CRandBytes)
|
||||
* [func CRandHex(numDigits int) string](#CRandHex)
|
||||
* [func CReader() io.Reader](#CReader)
|
||||
* [func DecodeArmor(armorStr string) (blockType string, headers map[string]string, data []byte, err error)](#DecodeArmor)
|
||||
* [func DecryptSymmetric(ciphertext []byte, secret []byte) (plaintext []byte, err error)](#DecryptSymmetric)
|
||||
* [func EncodeArmor(blockType string, headers map[string]string, data []byte) string](#EncodeArmor)
|
||||
* [func EncryptSymmetric(plaintext []byte, secret []byte) (ciphertext []byte)](#EncryptSymmetric)
|
||||
* [func MixEntropy(seedBytes []byte)](#MixEntropy)
|
||||
* [func Ripemd160(bytes []byte) []byte](#Ripemd160)
|
||||
* [func Sha256(bytes []byte) []byte](#Sha256)
|
||||
* [type PrivKey](#PrivKey)
|
||||
* [func PrivKeyFromBytes(privKeyBytes []byte) (privKey PrivKey, err error)](#PrivKeyFromBytes)
|
||||
* [type PrivKeyEd25519](#PrivKeyEd25519)
|
||||
* [func GenPrivKeyEd25519() PrivKeyEd25519](#GenPrivKeyEd25519)
|
||||
* [func GenPrivKeyEd25519FromSecret(secret []byte) PrivKeyEd25519](#GenPrivKeyEd25519FromSecret)
|
||||
* [func (privKey PrivKeyEd25519) Bytes() []byte](#PrivKeyEd25519.Bytes)
|
||||
* [func (privKey PrivKeyEd25519) Equals(other PrivKey) bool](#PrivKeyEd25519.Equals)
|
||||
* [func (privKey PrivKeyEd25519) Generate(index int) PrivKeyEd25519](#PrivKeyEd25519.Generate)
|
||||
* [func (p PrivKeyEd25519) MarshalJSON() ([]byte, error)](#PrivKeyEd25519.MarshalJSON)
|
||||
* [func (privKey PrivKeyEd25519) PubKey() PubKey](#PrivKeyEd25519.PubKey)
|
||||
* [func (privKey PrivKeyEd25519) Sign(msg []byte) Signature](#PrivKeyEd25519.Sign)
|
||||
* [func (privKey PrivKeyEd25519) String() string](#PrivKeyEd25519.String)
|
||||
* [func (privKey PrivKeyEd25519) ToCurve25519() *[32]byte](#PrivKeyEd25519.ToCurve25519)
|
||||
* [func (p *PrivKeyEd25519) UnmarshalJSON(enc []byte) error](#PrivKeyEd25519.UnmarshalJSON)
|
||||
* [type PrivKeyS](#PrivKeyS)
|
||||
* [func (p PrivKeyS) Empty() bool](#PrivKeyS.Empty)
|
||||
* [func (p PrivKeyS) MarshalJSON() ([]byte, error)](#PrivKeyS.MarshalJSON)
|
||||
* [func (p *PrivKeyS) UnmarshalJSON(data []byte) (err error)](#PrivKeyS.UnmarshalJSON)
|
||||
* [type PrivKeySecp256k1](#PrivKeySecp256k1)
|
||||
* [func GenPrivKeySecp256k1() PrivKeySecp256k1](#GenPrivKeySecp256k1)
|
||||
* [func GenPrivKeySecp256k1FromSecret(secret []byte) PrivKeySecp256k1](#GenPrivKeySecp256k1FromSecret)
|
||||
* [func (privKey PrivKeySecp256k1) Bytes() []byte](#PrivKeySecp256k1.Bytes)
|
||||
* [func (privKey PrivKeySecp256k1) Equals(other PrivKey) bool](#PrivKeySecp256k1.Equals)
|
||||
* [func (p PrivKeySecp256k1) MarshalJSON() ([]byte, error)](#PrivKeySecp256k1.MarshalJSON)
|
||||
* [func (privKey PrivKeySecp256k1) PubKey() PubKey](#PrivKeySecp256k1.PubKey)
|
||||
* [func (privKey PrivKeySecp256k1) Sign(msg []byte) Signature](#PrivKeySecp256k1.Sign)
|
||||
* [func (privKey PrivKeySecp256k1) String() string](#PrivKeySecp256k1.String)
|
||||
* [func (p *PrivKeySecp256k1) UnmarshalJSON(enc []byte) error](#PrivKeySecp256k1.UnmarshalJSON)
|
||||
* [type PubKey](#PubKey)
|
||||
* [func PubKeyFromBytes(pubKeyBytes []byte) (pubKey PubKey, err error)](#PubKeyFromBytes)
|
||||
* [type PubKeyEd25519](#PubKeyEd25519)
|
||||
* [func (pubKey PubKeyEd25519) Address() []byte](#PubKeyEd25519.Address)
|
||||
* [func (pubKey PubKeyEd25519) Bytes() []byte](#PubKeyEd25519.Bytes)
|
||||
* [func (pubKey PubKeyEd25519) Equals(other PubKey) bool](#PubKeyEd25519.Equals)
|
||||
* [func (pubKey PubKeyEd25519) KeyString() string](#PubKeyEd25519.KeyString)
|
||||
* [func (p PubKeyEd25519) MarshalJSON() ([]byte, error)](#PubKeyEd25519.MarshalJSON)
|
||||
* [func (pubKey PubKeyEd25519) String() string](#PubKeyEd25519.String)
|
||||
* [func (pubKey PubKeyEd25519) ToCurve25519() *[32]byte](#PubKeyEd25519.ToCurve25519)
|
||||
* [func (p *PubKeyEd25519) UnmarshalJSON(enc []byte) error](#PubKeyEd25519.UnmarshalJSON)
|
||||
* [func (pubKey PubKeyEd25519) VerifyBytes(msg []byte, sig_ Signature) bool](#PubKeyEd25519.VerifyBytes)
|
||||
* [type PubKeyS](#PubKeyS)
|
||||
* [func (p PubKeyS) Empty() bool](#PubKeyS.Empty)
|
||||
* [func (p PubKeyS) MarshalJSON() ([]byte, error)](#PubKeyS.MarshalJSON)
|
||||
* [func (p *PubKeyS) UnmarshalJSON(data []byte) (err error)](#PubKeyS.UnmarshalJSON)
|
||||
* [type PubKeySecp256k1](#PubKeySecp256k1)
|
||||
* [func (pubKey PubKeySecp256k1) Address() []byte](#PubKeySecp256k1.Address)
|
||||
* [func (pubKey PubKeySecp256k1) Bytes() []byte](#PubKeySecp256k1.Bytes)
|
||||
* [func (pubKey PubKeySecp256k1) Equals(other PubKey) bool](#PubKeySecp256k1.Equals)
|
||||
* [func (pubKey PubKeySecp256k1) KeyString() string](#PubKeySecp256k1.KeyString)
|
||||
* [func (p PubKeySecp256k1) MarshalJSON() ([]byte, error)](#PubKeySecp256k1.MarshalJSON)
|
||||
* [func (pubKey PubKeySecp256k1) String() string](#PubKeySecp256k1.String)
|
||||
* [func (p *PubKeySecp256k1) UnmarshalJSON(enc []byte) error](#PubKeySecp256k1.UnmarshalJSON)
|
||||
* [func (pubKey PubKeySecp256k1) VerifyBytes(msg []byte, sig_ Signature) bool](#PubKeySecp256k1.VerifyBytes)
|
||||
* [type Signature](#Signature)
|
||||
* [func SignatureFromBytes(sigBytes []byte) (sig Signature, err error)](#SignatureFromBytes)
|
||||
* [type SignatureEd25519](#SignatureEd25519)
|
||||
* [func (sig SignatureEd25519) Bytes() []byte](#SignatureEd25519.Bytes)
|
||||
* [func (sig SignatureEd25519) Equals(other Signature) bool](#SignatureEd25519.Equals)
|
||||
* [func (sig SignatureEd25519) IsZero() bool](#SignatureEd25519.IsZero)
|
||||
* [func (p SignatureEd25519) MarshalJSON() ([]byte, error)](#SignatureEd25519.MarshalJSON)
|
||||
* [func (sig SignatureEd25519) String() string](#SignatureEd25519.String)
|
||||
* [func (p *SignatureEd25519) UnmarshalJSON(enc []byte) error](#SignatureEd25519.UnmarshalJSON)
|
||||
* [type SignatureS](#SignatureS)
|
||||
* [func (p SignatureS) Empty() bool](#SignatureS.Empty)
|
||||
* [func (p SignatureS) MarshalJSON() ([]byte, error)](#SignatureS.MarshalJSON)
|
||||
* [func (p *SignatureS) UnmarshalJSON(data []byte) (err error)](#SignatureS.UnmarshalJSON)
|
||||
* [type SignatureSecp256k1](#SignatureSecp256k1)
|
||||
* [func (sig SignatureSecp256k1) Bytes() []byte](#SignatureSecp256k1.Bytes)
|
||||
* [func (sig SignatureSecp256k1) Equals(other Signature) bool](#SignatureSecp256k1.Equals)
|
||||
* [func (sig SignatureSecp256k1) IsZero() bool](#SignatureSecp256k1.IsZero)
|
||||
* [func (p SignatureSecp256k1) MarshalJSON() ([]byte, error)](#SignatureSecp256k1.MarshalJSON)
|
||||
* [func (sig SignatureSecp256k1) String() string](#SignatureSecp256k1.String)
|
||||
* [func (p *SignatureSecp256k1) UnmarshalJSON(enc []byte) error](#SignatureSecp256k1.UnmarshalJSON)
|
||||
|
||||
|
||||
#### <a name="pkg-files">Package files</a>
|
||||
[armor.go](/src/github.com/tendermint/go-crypto/armor.go) [hash.go](/src/github.com/tendermint/go-crypto/hash.go) [priv_key.go](/src/github.com/tendermint/go-crypto/priv_key.go) [pub_key.go](/src/github.com/tendermint/go-crypto/pub_key.go) [random.go](/src/github.com/tendermint/go-crypto/random.go) [signature.go](/src/github.com/tendermint/go-crypto/signature.go) [symmetric.go](/src/github.com/tendermint/go-crypto/symmetric.go)
|
||||
|
||||
|
||||
## <a name="pkg-constants">Constants</a>
|
||||
``` go
|
||||
const (
|
||||
TypeEd25519 = byte(0x01)
|
||||
TypeSecp256k1 = byte(0x02)
|
||||
NameEd25519 = "ed25519"
|
||||
NameSecp256k1 = "secp256k1"
|
||||
)
|
||||
```
|
||||
Types of implementations
|
||||
|
||||
|
||||
|
||||
|
||||
## <a name="CRandBytes">func</a> [CRandBytes](/src/target/random.go?s=698:734#L28)
|
||||
``` go
|
||||
func CRandBytes(numBytes int) []byte
|
||||
```
|
||||
This uses the OS and the Seed(s).
|
||||
|
||||
|
||||
|
||||
## <a name="CRandHex">func</a> [CRandHex](/src/target/random.go?s=924:959#L38)
|
||||
``` go
|
||||
func CRandHex(numDigits int) string
|
||||
```
|
||||
RandHex(24) gives 96 bits of randomness, strong enough for most purposes.
|
||||
|
||||
|
||||
|
||||
## <a name="CReader">func</a> [CReader](/src/target/random.go?s=1078:1102#L43)
|
||||
``` go
|
||||
func CReader() io.Reader
|
||||
```
|
||||
Returns a crand.Reader mixed with user-supplied entropy
|
||||
|
||||
|
||||
|
||||
## <a name="DecodeArmor">func</a> [DecodeArmor](/src/target/armor.go?s=596:699#L18)
|
||||
``` go
|
||||
func DecodeArmor(armorStr string) (blockType string, headers map[string]string, data []byte, err error)
|
||||
```
|
||||
|
||||
|
||||
## <a name="DecryptSymmetric">func</a> [DecryptSymmetric](/src/target/symmetric.go?s=1048:1133#L23)
|
||||
``` go
|
||||
func DecryptSymmetric(ciphertext []byte, secret []byte) (plaintext []byte, err error)
|
||||
```
|
||||
secret must be 32 bytes long. Use something like Sha256(Bcrypt(passphrase))
|
||||
The ciphertext is (secretbox.Overhead + 24) bytes longer than the plaintext.
|
||||
|
||||
|
||||
|
||||
## <a name="EncodeArmor">func</a> [EncodeArmor](/src/target/armor.go?s=125:206#L1)
|
||||
``` go
|
||||
func EncodeArmor(blockType string, headers map[string]string, data []byte) string
|
||||
```
|
||||
|
||||
|
||||
## <a name="EncryptSymmetric">func</a> [EncryptSymmetric](/src/target/symmetric.go?s=356:430#L6)
|
||||
``` go
|
||||
func EncryptSymmetric(plaintext []byte, secret []byte) (ciphertext []byte)
|
||||
```
|
||||
secret must be 32 bytes long. Use something like Sha256(Bcrypt(passphrase))
|
||||
The ciphertext is (secretbox.Overhead + 24) bytes longer than the plaintext.
|
||||
NOTE: call crypto.MixEntropy() first.
|
||||
|
||||
|
||||
|
||||
## <a name="MixEntropy">func</a> [MixEntropy](/src/target/random.go?s=407:440#L13)
|
||||
``` go
|
||||
func MixEntropy(seedBytes []byte)
|
||||
```
|
||||
Mix additional bytes of randomness, e.g. from hardware, user-input, etc.
|
||||
It is OK to call it multiple times. It does not diminish security.
|
||||
|
||||
|
||||
|
||||
## <a name="Ripemd160">func</a> [Ripemd160](/src/target/hash.go?s=185:220#L4)
|
||||
``` go
|
||||
func Ripemd160(bytes []byte) []byte
|
||||
```
|
||||
|
||||
|
||||
## <a name="Sha256">func</a> [Sha256](/src/target/hash.go?s=78:110#L1)
|
||||
``` go
|
||||
func Sha256(bytes []byte) []byte
|
||||
```
|
||||
|
||||
|
||||
|
||||
## <a name="PrivKey">type</a> [PrivKey](/src/target/priv_key.go?s=326:435#L5)
|
||||
``` go
|
||||
type PrivKey interface {
|
||||
Bytes() []byte
|
||||
Sign(msg []byte) Signature
|
||||
PubKey() PubKey
|
||||
Equals(PrivKey) bool
|
||||
}
|
||||
```
|
||||
PrivKey is part of PrivAccount and state.PrivValidator.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
### <a name="PrivKeyFromBytes">func</a> [PrivKeyFromBytes](/src/target/priv_key.go?s=1302:1373#L50)
|
||||
``` go
|
||||
func PrivKeyFromBytes(privKeyBytes []byte) (privKey PrivKey, err error)
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
## <a name="PrivKeyEd25519">type</a> [PrivKeyEd25519](/src/target/priv_key.go?s=1502:1530#L58)
|
||||
``` go
|
||||
type PrivKeyEd25519 [64]byte
|
||||
```
|
||||
Implements PrivKey
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
### <a name="GenPrivKeyEd25519">func</a> [GenPrivKeyEd25519](/src/target/priv_key.go?s=3003:3042#L116)
|
||||
``` go
|
||||
func GenPrivKeyEd25519() PrivKeyEd25519
|
||||
```
|
||||
|
||||
### <a name="GenPrivKeyEd25519FromSecret">func</a> [GenPrivKeyEd25519FromSecret](/src/target/priv_key.go?s=3290:3352#L125)
|
||||
``` go
|
||||
func GenPrivKeyEd25519FromSecret(secret []byte) PrivKeyEd25519
|
||||
```
|
||||
NOTE: secret should be the output of a KDF like bcrypt,
|
||||
if it's derived from user input.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
### <a name="PrivKeyEd25519.Bytes">func</a> (PrivKeyEd25519) [Bytes](/src/target/priv_key.go?s=1532:1576#L60)
|
||||
``` go
|
||||
func (privKey PrivKeyEd25519) Bytes() []byte
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <a name="PrivKeyEd25519.Equals">func</a> (PrivKeyEd25519) [Equals](/src/target/priv_key.go?s=1973:2029#L75)
|
||||
``` go
|
||||
func (privKey PrivKeyEd25519) Equals(other PrivKey) bool
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <a name="PrivKeyEd25519.Generate">func</a> (PrivKeyEd25519) [Generate](/src/target/priv_key.go?s=2761:2825#L106)
|
||||
``` go
|
||||
func (privKey PrivKeyEd25519) Generate(index int) PrivKeyEd25519
|
||||
```
|
||||
Deterministically generates new priv-key bytes from key.
|
||||
|
||||
|
||||
|
||||
|
||||
### <a name="PrivKeyEd25519.MarshalJSON">func</a> (PrivKeyEd25519) [MarshalJSON](/src/target/priv_key.go?s=2156:2209#L83)
|
||||
``` go
|
||||
func (p PrivKeyEd25519) MarshalJSON() ([]byte, error)
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <a name="PrivKeyEd25519.PubKey">func</a> (PrivKeyEd25519) [PubKey](/src/target/priv_key.go?s=1826:1871#L70)
|
||||
``` go
|
||||
func (privKey PrivKeyEd25519) PubKey() PubKey
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <a name="PrivKeyEd25519.Sign">func</a> (PrivKeyEd25519) [Sign](/src/target/priv_key.go?s=1635:1691#L64)
|
||||
``` go
|
||||
func (privKey PrivKeyEd25519) Sign(msg []byte) Signature
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <a name="PrivKeyEd25519.String">func</a> (PrivKeyEd25519) [String](/src/target/priv_key.go?s=2613:2658#L101)
|
||||
``` go
|
||||
func (privKey PrivKeyEd25519) String() string
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <a name="PrivKeyEd25519.ToCurve25519">func</a> (PrivKeyEd25519) [ToCurve25519](/src/target/priv_key.go?s=2399:2453#L94)
|
||||
``` go
|
||||
func (privKey PrivKeyEd25519) ToCurve25519() *[32]byte
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <a name="PrivKeyEd25519.UnmarshalJSON">func</a> (\*PrivKeyEd25519) [UnmarshalJSON](/src/target/priv_key.go?s=2250:2306#L87)
|
||||
``` go
|
||||
func (p *PrivKeyEd25519) UnmarshalJSON(enc []byte) error
|
||||
```
|
||||
|
||||
|
||||
|
||||
## <a name="PrivKeyS">type</a> [PrivKeyS](/src/target/priv_key.go?s=929:962#L30)
|
||||
``` go
|
||||
type PrivKeyS struct {
|
||||
PrivKey
|
||||
}
|
||||
```
|
||||
PrivKeyS add json serialization to PrivKey
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
### <a name="PrivKeyS.Empty">func</a> (PrivKeyS) [Empty](/src/target/priv_key.go?s=1241:1271#L46)
|
||||
``` go
|
||||
func (p PrivKeyS) Empty() bool
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <a name="PrivKeyS.MarshalJSON">func</a> (PrivKeyS) [MarshalJSON](/src/target/priv_key.go?s=964:1011#L34)
|
||||
``` go
|
||||
func (p PrivKeyS) MarshalJSON() ([]byte, error)
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <a name="PrivKeyS.UnmarshalJSON">func</a> (\*PrivKeyS) [UnmarshalJSON](/src/target/priv_key.go?s=1057:1114#L38)
|
||||
``` go
|
||||
func (p *PrivKeyS) UnmarshalJSON(data []byte) (err error)
|
||||
```
|
||||
|
||||
|
||||
|
||||
## <a name="PrivKeySecp256k1">type</a> [PrivKeySecp256k1](/src/target/priv_key.go?s=3635:3665#L136)
|
||||
``` go
|
||||
type PrivKeySecp256k1 [32]byte
|
||||
```
|
||||
Implements PrivKey
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
### <a name="GenPrivKeySecp256k1">func</a> [GenPrivKeySecp256k1](/src/target/priv_key.go?s=5071:5114#L194)
|
||||
``` go
|
||||
func GenPrivKeySecp256k1() PrivKeySecp256k1
|
||||
```
|
||||
|
||||
### <a name="GenPrivKeySecp256k1FromSecret">func</a> [GenPrivKeySecp256k1FromSecret](/src/target/priv_key.go?s=5436:5502#L204)
|
||||
``` go
|
||||
func GenPrivKeySecp256k1FromSecret(secret []byte) PrivKeySecp256k1
|
||||
```
|
||||
NOTE: secret should be the output of a KDF like bcrypt,
|
||||
if it's derived from user input.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
### <a name="PrivKeySecp256k1.Bytes">func</a> (PrivKeySecp256k1) [Bytes](/src/target/priv_key.go?s=3667:3713#L138)
|
||||
``` go
|
||||
func (privKey PrivKeySecp256k1) Bytes() []byte
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <a name="PrivKeySecp256k1.Equals">func</a> (PrivKeySecp256k1) [Equals](/src/target/priv_key.go?s=4235:4293#L158)
|
||||
``` go
|
||||
func (privKey PrivKeySecp256k1) Equals(other PrivKey) bool
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <a name="PrivKeySecp256k1.MarshalJSON">func</a> (PrivKeySecp256k1) [MarshalJSON](/src/target/priv_key.go?s=4426:4481#L166)
|
||||
``` go
|
||||
func (p PrivKeySecp256k1) MarshalJSON() ([]byte, error)
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <a name="PrivKeySecp256k1.PubKey">func</a> (PrivKeySecp256k1) [PubKey](/src/target/priv_key.go?s=4032:4079#L151)
|
||||
``` go
|
||||
func (privKey PrivKeySecp256k1) PubKey() PubKey
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <a name="PrivKeySecp256k1.Sign">func</a> (PrivKeySecp256k1) [Sign](/src/target/priv_key.go?s=3772:3830#L142)
|
||||
``` go
|
||||
func (privKey PrivKeySecp256k1) Sign(msg []byte) Signature
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <a name="PrivKeySecp256k1.String">func</a> (PrivKeySecp256k1) [String](/src/target/priv_key.go?s=4673:4720#L177)
|
||||
``` go
|
||||
func (privKey PrivKeySecp256k1) String() string
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <a name="PrivKeySecp256k1.UnmarshalJSON">func</a> (\*PrivKeySecp256k1) [UnmarshalJSON](/src/target/priv_key.go?s=4522:4580#L170)
|
||||
``` go
|
||||
func (p *PrivKeySecp256k1) UnmarshalJSON(enc []byte) error
|
||||
```
|
||||
|
||||
|
||||
|
||||
## <a name="PubKey">type</a> [PubKey](/src/target/pub_key.go?s=361:506#L7)
|
||||
``` go
|
||||
type PubKey interface {
|
||||
Address() []byte
|
||||
Bytes() []byte
|
||||
KeyString() string
|
||||
VerifyBytes(msg []byte, sig Signature) bool
|
||||
Equals(PubKey) bool
|
||||
}
|
||||
```
|
||||
PubKey is part of Account and Validator.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
### <a name="PubKeyFromBytes">func</a> [PubKeyFromBytes](/src/target/pub_key.go?s=1203:1270#L45)
|
||||
``` go
|
||||
func PubKeyFromBytes(pubKeyBytes []byte) (pubKey PubKey, err error)
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
## <a name="PubKeyEd25519">type</a> [PubKeyEd25519](/src/target/pub_key.go?s=1396:1423#L53)
|
||||
``` go
|
||||
type PubKeyEd25519 [32]byte
|
||||
```
|
||||
Implements PubKey
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
### <a name="PubKeyEd25519.Address">func</a> (PubKeyEd25519) [Address](/src/target/pub_key.go?s=1425:1469#L55)
|
||||
``` go
|
||||
func (pubKey PubKeyEd25519) Address() []byte
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <a name="PubKeyEd25519.Bytes">func</a> (PubKeyEd25519) [Bytes](/src/target/pub_key.go?s=1789:1831#L68)
|
||||
``` go
|
||||
func (pubKey PubKeyEd25519) Bytes() []byte
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <a name="PubKeyEd25519.Equals">func</a> (PubKeyEd25519) [Equals](/src/target/pub_key.go?s=3064:3117#L119)
|
||||
``` go
|
||||
func (pubKey PubKeyEd25519) Equals(other PubKey) bool
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <a name="PubKeyEd25519.KeyString">func</a> (PubKeyEd25519) [KeyString](/src/target/pub_key.go?s=2983:3029#L115)
|
||||
``` go
|
||||
func (pubKey PubKeyEd25519) KeyString() string
|
||||
```
|
||||
Must return the full bytes in hex.
|
||||
Used for map keying, etc.
|
||||
|
||||
|
||||
|
||||
|
||||
### <a name="PubKeyEd25519.MarshalJSON">func</a> (PubKeyEd25519) [MarshalJSON](/src/target/pub_key.go?s=2279:2331#L87)
|
||||
``` go
|
||||
func (p PubKeyEd25519) MarshalJSON() ([]byte, error)
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <a name="PubKeyEd25519.String">func</a> (PubKeyEd25519) [String](/src/target/pub_key.go?s=2823:2866#L109)
|
||||
``` go
|
||||
func (pubKey PubKeyEd25519) String() string
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <a name="PubKeyEd25519.ToCurve25519">func</a> (PubKeyEd25519) [ToCurve25519](/src/target/pub_key.go?s=2585:2637#L100)
|
||||
``` go
|
||||
func (pubKey PubKeyEd25519) ToCurve25519() *[32]byte
|
||||
```
|
||||
For use with golang/crypto/nacl/box
|
||||
If error, returns nil.
|
||||
|
||||
|
||||
|
||||
|
||||
### <a name="PubKeyEd25519.UnmarshalJSON">func</a> (\*PubKeyEd25519) [UnmarshalJSON](/src/target/pub_key.go?s=2372:2427#L91)
|
||||
``` go
|
||||
func (p *PubKeyEd25519) UnmarshalJSON(enc []byte) error
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <a name="PubKeyEd25519.VerifyBytes">func</a> (PubKeyEd25519) [VerifyBytes](/src/target/pub_key.go?s=1888:1960#L72)
|
||||
``` go
|
||||
func (pubKey PubKeyEd25519) VerifyBytes(msg []byte, sig_ Signature) bool
|
||||
```
|
||||
|
||||
|
||||
|
||||
## <a name="PubKeyS">type</a> [PubKeyS](/src/target/pub_key.go?s=841:872#L25)
|
||||
``` go
|
||||
type PubKeyS struct {
|
||||
PubKey
|
||||
}
|
||||
```
|
||||
PubKeyS add json serialization to PubKey
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
### <a name="PubKeyS.Empty">func</a> (PubKeyS) [Empty](/src/target/pub_key.go?s=1144:1173#L41)
|
||||
``` go
|
||||
func (p PubKeyS) Empty() bool
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <a name="PubKeyS.MarshalJSON">func</a> (PubKeyS) [MarshalJSON](/src/target/pub_key.go?s=874:920#L29)
|
||||
``` go
|
||||
func (p PubKeyS) MarshalJSON() ([]byte, error)
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <a name="PubKeyS.UnmarshalJSON">func</a> (\*PubKeyS) [UnmarshalJSON](/src/target/pub_key.go?s=964:1020#L33)
|
||||
``` go
|
||||
func (p *PubKeyS) UnmarshalJSON(data []byte) (err error)
|
||||
```
|
||||
|
||||
|
||||
|
||||
## <a name="PubKeySecp256k1">type</a> [PubKeySecp256k1](/src/target/pub_key.go?s=3401:3430#L132)
|
||||
``` go
|
||||
type PubKeySecp256k1 [33]byte
|
||||
```
|
||||
Implements PubKey.
|
||||
Compressed pubkey (just the x-cord),
|
||||
prefixed with 0x02 or 0x03, depending on the y-cord.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
### <a name="PubKeySecp256k1.Address">func</a> (PubKeySecp256k1) [Address](/src/target/pub_key.go?s=3497:3543#L135)
|
||||
``` go
|
||||
func (pubKey PubKeySecp256k1) Address() []byte
|
||||
```
|
||||
Implements Bitcoin style addresses: RIPEMD160(SHA256(pubkey))
|
||||
|
||||
|
||||
|
||||
|
||||
### <a name="PubKeySecp256k1.Bytes">func</a> (PubKeySecp256k1) [Bytes](/src/target/pub_key.go?s=3774:3818#L145)
|
||||
``` go
|
||||
func (pubKey PubKeySecp256k1) Bytes() []byte
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <a name="PubKeySecp256k1.Equals">func</a> (PubKeySecp256k1) [Equals](/src/target/pub_key.go?s=4897:4952#L192)
|
||||
``` go
|
||||
func (pubKey PubKeySecp256k1) Equals(other PubKey) bool
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <a name="PubKeySecp256k1.KeyString">func</a> (PubKeySecp256k1) [KeyString](/src/target/pub_key.go?s=4814:4862#L188)
|
||||
``` go
|
||||
func (pubKey PubKeySecp256k1) KeyString() string
|
||||
```
|
||||
Must return the full bytes in hex.
|
||||
Used for map keying, etc.
|
||||
|
||||
|
||||
|
||||
|
||||
### <a name="PubKeySecp256k1.MarshalJSON">func</a> (PubKeySecp256k1) [MarshalJSON](/src/target/pub_key.go?s=4405:4459#L171)
|
||||
``` go
|
||||
func (p PubKeySecp256k1) MarshalJSON() ([]byte, error)
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <a name="PubKeySecp256k1.String">func</a> (PubKeySecp256k1) [String](/src/target/pub_key.go?s=4650:4695#L182)
|
||||
``` go
|
||||
func (pubKey PubKeySecp256k1) String() string
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <a name="PubKeySecp256k1.UnmarshalJSON">func</a> (\*PubKeySecp256k1) [UnmarshalJSON](/src/target/pub_key.go?s=4500:4557#L175)
|
||||
``` go
|
||||
func (p *PubKeySecp256k1) UnmarshalJSON(enc []byte) error
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <a name="PubKeySecp256k1.VerifyBytes">func</a> (PubKeySecp256k1) [VerifyBytes](/src/target/pub_key.go?s=3875:3949#L149)
|
||||
``` go
|
||||
func (pubKey PubKeySecp256k1) VerifyBytes(msg []byte, sig_ Signature) bool
|
||||
```
|
||||
|
||||
|
||||
|
||||
## <a name="Signature">type</a> [Signature](/src/target/signature.go?s=204:304#L3)
|
||||
``` go
|
||||
type Signature interface {
|
||||
Bytes() []byte
|
||||
IsZero() bool
|
||||
String() string
|
||||
Equals(Signature) bool
|
||||
}
|
||||
```
|
||||
Signature is a part of Txs and consensus Votes.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
### <a name="SignatureFromBytes">func</a> [SignatureFromBytes](/src/target/signature.go?s=1031:1098#L40)
|
||||
``` go
|
||||
func SignatureFromBytes(sigBytes []byte) (sig Signature, err error)
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
## <a name="SignatureEd25519">type</a> [SignatureEd25519](/src/target/signature.go?s=1221:1251#L48)
|
||||
``` go
|
||||
type SignatureEd25519 [64]byte
|
||||
```
|
||||
Implements Signature
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
### <a name="SignatureEd25519.Bytes">func</a> (SignatureEd25519) [Bytes](/src/target/signature.go?s=1253:1295#L50)
|
||||
``` go
|
||||
func (sig SignatureEd25519) Bytes() []byte
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <a name="SignatureEd25519.Equals">func</a> (SignatureEd25519) [Equals](/src/target/signature.go?s=1520:1576#L58)
|
||||
``` go
|
||||
func (sig SignatureEd25519) Equals(other Signature) bool
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <a name="SignatureEd25519.IsZero">func</a> (SignatureEd25519) [IsZero](/src/target/signature.go?s=1352:1393#L54)
|
||||
``` go
|
||||
func (sig SignatureEd25519) IsZero() bool
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <a name="SignatureEd25519.MarshalJSON">func</a> (SignatureEd25519) [MarshalJSON](/src/target/signature.go?s=1701:1756#L66)
|
||||
``` go
|
||||
func (p SignatureEd25519) MarshalJSON() ([]byte, error)
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <a name="SignatureEd25519.String">func</a> (SignatureEd25519) [String](/src/target/signature.go?s=1420:1463#L56)
|
||||
``` go
|
||||
func (sig SignatureEd25519) String() string
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <a name="SignatureEd25519.UnmarshalJSON">func</a> (\*SignatureEd25519) [UnmarshalJSON](/src/target/signature.go?s=1797:1855#L70)
|
||||
``` go
|
||||
func (p *SignatureEd25519) UnmarshalJSON(enc []byte) error
|
||||
```
|
||||
|
||||
|
||||
|
||||
## <a name="SignatureS">type</a> [SignatureS](/src/target/signature.go?s=648:685#L20)
|
||||
``` go
|
||||
type SignatureS struct {
|
||||
Signature
|
||||
}
|
||||
```
|
||||
SignatureS add json serialization to Signature
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
### <a name="SignatureS.Empty">func</a> (SignatureS) [Empty](/src/target/signature.go?s=966:998#L36)
|
||||
``` go
|
||||
func (p SignatureS) Empty() bool
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <a name="SignatureS.MarshalJSON">func</a> (SignatureS) [MarshalJSON](/src/target/signature.go?s=687:736#L24)
|
||||
``` go
|
||||
func (p SignatureS) MarshalJSON() ([]byte, error)
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <a name="SignatureS.UnmarshalJSON">func</a> (\*SignatureS) [UnmarshalJSON](/src/target/signature.go?s=780:839#L28)
|
||||
``` go
|
||||
func (p *SignatureS) UnmarshalJSON(data []byte) (err error)
|
||||
```
|
||||
|
||||
|
||||
|
||||
## <a name="SignatureSecp256k1">type</a> [SignatureSecp256k1](/src/target/signature.go?s=2013:2043#L80)
|
||||
``` go
|
||||
type SignatureSecp256k1 []byte
|
||||
```
|
||||
Implements Signature
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
### <a name="SignatureSecp256k1.Bytes">func</a> (SignatureSecp256k1) [Bytes](/src/target/signature.go?s=2045:2089#L82)
|
||||
``` go
|
||||
func (sig SignatureSecp256k1) Bytes() []byte
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <a name="SignatureSecp256k1.Equals">func</a> (SignatureSecp256k1) [Equals](/src/target/signature.go?s=2318:2376#L90)
|
||||
``` go
|
||||
func (sig SignatureSecp256k1) Equals(other Signature) bool
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <a name="SignatureSecp256k1.IsZero">func</a> (SignatureSecp256k1) [IsZero](/src/target/signature.go?s=2146:2189#L86)
|
||||
``` go
|
||||
func (sig SignatureSecp256k1) IsZero() bool
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <a name="SignatureSecp256k1.MarshalJSON">func</a> (SignatureSecp256k1) [MarshalJSON](/src/target/signature.go?s=2502:2559#L97)
|
||||
``` go
|
||||
func (p SignatureSecp256k1) MarshalJSON() ([]byte, error)
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <a name="SignatureSecp256k1.String">func</a> (SignatureSecp256k1) [String](/src/target/signature.go?s=2216:2261#L88)
|
||||
``` go
|
||||
func (sig SignatureSecp256k1) String() string
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <a name="SignatureSecp256k1.UnmarshalJSON">func</a> (\*SignatureSecp256k1) [UnmarshalJSON](/src/target/signature.go?s=2597:2657#L101)
|
||||
``` go
|
||||
func (p *SignatureSecp256k1) UnmarshalJSON(enc []byte) error
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
- - -
|
||||
Generated by [godoc2md](http://godoc.org/github.com/davecheney/godoc2md)
|
126
embed_test.go
Normal file
126
embed_test.go
Normal file
@ -0,0 +1,126 @@
|
||||
package crypto_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
data "github.com/tendermint/go-data"
|
||||
)
|
||||
|
||||
type Foo struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
func (f Foo) Greet() string {
|
||||
return "Foo: " + f.Name
|
||||
}
|
||||
|
||||
type Bar struct {
|
||||
Age int
|
||||
}
|
||||
|
||||
func (b Bar) Greet() string {
|
||||
return fmt.Sprintf("Bar #%d", b.Age)
|
||||
}
|
||||
|
||||
type PubNameInner interface {
|
||||
Greet() string
|
||||
}
|
||||
|
||||
type privNameInner interface {
|
||||
Greet() string
|
||||
}
|
||||
|
||||
type Greeter interface {
|
||||
Greet() string
|
||||
}
|
||||
|
||||
var (
|
||||
pubNameMapper, privNameMapper data.Mapper
|
||||
)
|
||||
|
||||
// register both public key types with go-data (and thus go-wire)
|
||||
func init() {
|
||||
pubNameMapper = data.NewMapper(PubName{}).
|
||||
RegisterImplementation(Foo{}, "foo", 1).
|
||||
RegisterImplementation(Bar{}, "bar", 2)
|
||||
privNameMapper = data.NewMapper(PrivName{}).
|
||||
RegisterImplementation(Foo{}, "foo", 1).
|
||||
RegisterImplementation(Bar{}, "bar", 2)
|
||||
}
|
||||
|
||||
type PubName struct {
|
||||
PubNameInner
|
||||
}
|
||||
|
||||
func (p PubName) MarshalJSON() ([]byte, error) {
|
||||
return pubNameMapper.ToJSON(p.PubNameInner)
|
||||
}
|
||||
|
||||
func (p *PubName) UnmarshalJSON(data []byte) error {
|
||||
parsed, err := pubNameMapper.FromJSON(data)
|
||||
if err == nil && parsed != nil {
|
||||
p.PubNameInner = parsed.(PubNameInner)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
type PrivName struct {
|
||||
privNameInner
|
||||
}
|
||||
|
||||
func (p PrivName) MarshalJSON() ([]byte, error) {
|
||||
return privNameMapper.ToJSON(p.privNameInner)
|
||||
}
|
||||
|
||||
func (p *PrivName) UnmarshalJSON(data []byte) error {
|
||||
parsed, err := privNameMapper.FromJSON(data)
|
||||
if err == nil && parsed != nil {
|
||||
p.privNameInner = parsed.(privNameInner)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// TestEncodeDemo tries the various strategies to encode the objects
|
||||
func TestEncodeDemo(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
// assert := assert.New(t)
|
||||
// require := require.New(t)
|
||||
|
||||
cases := []struct {
|
||||
in, out Greeter
|
||||
expected string
|
||||
}{
|
||||
{PubName{Foo{"pub-foo"}}, &PubName{}, "Foo: pub-foo"},
|
||||
{PubName{Bar{7}}, &PubName{}, "Bar #7"},
|
||||
// Note these fail - if you can figure a solution here, I'll buy you a beer :)
|
||||
// {PrivName{Foo{"priv-foo"}}, &PrivName{}, "Foo: priv-foo"},
|
||||
// {PrivName{Bar{9}}, &PrivName{}, "Bar #9"},
|
||||
}
|
||||
|
||||
for i, tc := range cases {
|
||||
// make sure it is proper to start
|
||||
require.Equal(tc.expected, tc.in.Greet())
|
||||
fmt.Println(tc.expected)
|
||||
|
||||
// now, try to encode as binary
|
||||
b, err := data.ToWire(tc.in)
|
||||
if assert.Nil(err, "%d: %#v", i, tc.in) {
|
||||
err := data.FromWire(b, tc.out)
|
||||
if assert.Nil(err) {
|
||||
assert.Equal(tc.expected, tc.out.Greet())
|
||||
}
|
||||
}
|
||||
|
||||
// try to encode it as json
|
||||
j, err := data.ToJSON(tc.in)
|
||||
if assert.Nil(err, "%d: %#v", i, tc.in) {
|
||||
err := data.FromJSON(j, tc.out)
|
||||
if assert.Nil(err) {
|
||||
assert.Equal(tc.expected, tc.out.Greet())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
289
hd/address.go
Normal file
289
hd/address.go
Normal file
@ -0,0 +1,289 @@
|
||||
package hd
|
||||
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"crypto/sha512"
|
||||
"encoding/base64"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"hash"
|
||||
"log"
|
||||
"math/big"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec"
|
||||
"github.com/btcsuite/btcutil/base58"
|
||||
"github.com/tendermint/go-crypto"
|
||||
"golang.org/x/crypto/ripemd160"
|
||||
)
|
||||
|
||||
const (
|
||||
// BIP32 chainpath prefix
|
||||
CHAINPATH_PREFIX_DEPOSIT = 0
|
||||
CHAINPATH_PREFIX_CHANGE = 1
|
||||
CHAINPATH_PREFIX_SWEEP = 2
|
||||
CHAINPATH_PREFIX_SWEEP_DRY = 102
|
||||
)
|
||||
|
||||
func ComputeAddress(coin string, pubKeyHex string, chainHex string, path string, index int32) string {
|
||||
pubKeyBytes := DerivePublicKeyForPath(
|
||||
HexDecode(pubKeyHex),
|
||||
HexDecode(chainHex),
|
||||
fmt.Sprintf("%v/%v", path, index),
|
||||
)
|
||||
return AddrFromPubKeyBytes(coin, pubKeyBytes)
|
||||
}
|
||||
|
||||
func ComputePrivateKey(mprivHex string, chainHex string, path string, index int32) string {
|
||||
privKeyBytes := DerivePrivateKeyForPath(
|
||||
HexDecode(mprivHex),
|
||||
HexDecode(chainHex),
|
||||
fmt.Sprintf("%v/%v", path, index),
|
||||
)
|
||||
return HexEncode(privKeyBytes)
|
||||
}
|
||||
|
||||
func ComputeAddressForPrivKey(coin string, privKey string) string {
|
||||
pubKeyBytes := PubKeyBytesFromPrivKeyBytes(HexDecode(privKey), true)
|
||||
return AddrFromPubKeyBytes(coin, pubKeyBytes)
|
||||
}
|
||||
|
||||
func SignMessage(privKey string, message string, compress bool) string {
|
||||
prefixBytes := []byte("Bitcoin Signed Message:\n")
|
||||
messageBytes := []byte(message)
|
||||
bytes := []byte{}
|
||||
bytes = append(bytes, byte(len(prefixBytes)))
|
||||
bytes = append(bytes, prefixBytes...)
|
||||
bytes = append(bytes, byte(len(messageBytes)))
|
||||
bytes = append(bytes, messageBytes...)
|
||||
privKeyBytes := HexDecode(privKey)
|
||||
x, y := btcec.S256().ScalarBaseMult(privKeyBytes)
|
||||
ecdsaPubKey := ecdsa.PublicKey{
|
||||
Curve: btcec.S256(),
|
||||
X: x,
|
||||
Y: y,
|
||||
}
|
||||
ecdsaPrivKey := &btcec.PrivateKey{
|
||||
PublicKey: ecdsaPubKey,
|
||||
D: new(big.Int).SetBytes(privKeyBytes),
|
||||
}
|
||||
sigbytes, err := btcec.SignCompact(btcec.S256(), ecdsaPrivKey, crypto.Sha256(crypto.Sha256(bytes)), compress)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return base64.StdEncoding.EncodeToString(sigbytes)
|
||||
}
|
||||
|
||||
// returns MPK, Chain, and master secret in hex.
|
||||
func ComputeMastersFromSeed(seed string) (string, string, string, string) {
|
||||
secret, chain := I64([]byte("Bitcoin seed"), []byte(seed))
|
||||
pubKeyBytes := PubKeyBytesFromPrivKeyBytes(secret, true)
|
||||
return HexEncode(pubKeyBytes), HexEncode(secret), HexEncode(chain), HexEncode(secret)
|
||||
}
|
||||
|
||||
func ComputeWIF(coin string, privKey string, compress bool) string {
|
||||
return WIFFromPrivKeyBytes(coin, HexDecode(privKey), compress)
|
||||
}
|
||||
|
||||
func ComputeTxId(rawTxHex string) string {
|
||||
return HexEncode(ReverseBytes(CalcHash256(HexDecode(rawTxHex))))
|
||||
}
|
||||
|
||||
// Private methods...
|
||||
|
||||
func printKeyInfo(privKeyBytes []byte, pubKeyBytes []byte, chain []byte) {
|
||||
if pubKeyBytes == nil {
|
||||
pubKeyBytes = PubKeyBytesFromPrivKeyBytes(privKeyBytes, true)
|
||||
}
|
||||
addr := AddrFromPubKeyBytes("BTC", pubKeyBytes)
|
||||
log.Println("\nprikey:\t%v\npubKeyBytes:\t%v\naddr:\t%v\nchain:\t%v",
|
||||
HexEncode(privKeyBytes),
|
||||
HexEncode(pubKeyBytes),
|
||||
addr,
|
||||
HexEncode(chain))
|
||||
}
|
||||
|
||||
func DerivePrivateKeyForPath(privKeyBytes []byte, chain []byte, path string) []byte {
|
||||
data := privKeyBytes
|
||||
parts := strings.Split(path, "/")
|
||||
for _, part := range parts {
|
||||
prime := part[len(part)-1:] == "'"
|
||||
// prime == private derivation. Otherwise public.
|
||||
if prime {
|
||||
part = part[:len(part)-1]
|
||||
}
|
||||
i, err := strconv.Atoi(part)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if i < 0 {
|
||||
panic(errors.New("index too large."))
|
||||
}
|
||||
data, chain = DerivePrivateKey(data, chain, uint32(i), prime)
|
||||
//printKeyInfo(data, nil, chain)
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
func DerivePublicKeyForPath(pubKeyBytes []byte, chain []byte, path string) []byte {
|
||||
data := pubKeyBytes
|
||||
parts := strings.Split(path, "/")
|
||||
for _, part := range parts {
|
||||
prime := part[len(part)-1:] == "'"
|
||||
if prime {
|
||||
panic(errors.New("cannot do a prime derivation from public key"))
|
||||
}
|
||||
i, err := strconv.Atoi(part)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if i < 0 {
|
||||
panic(errors.New("index too large."))
|
||||
}
|
||||
data, chain = DerivePublicKey(data, chain, uint32(i))
|
||||
//printKeyInfo(nil, data, chain)
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
func DerivePrivateKey(privKeyBytes []byte, chain []byte, i uint32, prime bool) ([]byte, []byte) {
|
||||
data := []byte{}
|
||||
if prime {
|
||||
i = i | 0x80000000
|
||||
data = append([]byte{byte(0)}, privKeyBytes...)
|
||||
} else {
|
||||
public := PubKeyBytesFromPrivKeyBytes(privKeyBytes, true)
|
||||
data = public
|
||||
}
|
||||
data = append(data, uint32ToBytes(i)...)
|
||||
data2, chain2 := I64(chain, data)
|
||||
x := addScalars(privKeyBytes, data2)
|
||||
return x, chain2
|
||||
}
|
||||
|
||||
func DerivePublicKey(pubKeyBytes []byte, chain []byte, i uint32) ([]byte, []byte) {
|
||||
data := []byte{}
|
||||
data = append(data, pubKeyBytes...)
|
||||
data = append(data, uint32ToBytes(i)...)
|
||||
data2, chain2 := I64(chain, data)
|
||||
data2p := PubKeyBytesFromPrivKeyBytes(data2, true)
|
||||
return addPoints(pubKeyBytes, data2p), chain2
|
||||
}
|
||||
|
||||
func addPoints(a []byte, b []byte) []byte {
|
||||
ap, err := btcec.ParsePubKey(a, btcec.S256())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
bp, err := btcec.ParsePubKey(b, btcec.S256())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
sumX, sumY := btcec.S256().Add(ap.X, ap.Y, bp.X, bp.Y)
|
||||
sum := (*btcec.PublicKey)(&btcec.PublicKey{
|
||||
Curve: btcec.S256(),
|
||||
X: sumX,
|
||||
Y: sumY,
|
||||
})
|
||||
return sum.SerializeCompressed()
|
||||
}
|
||||
|
||||
func addScalars(a []byte, b []byte) []byte {
|
||||
aInt := new(big.Int).SetBytes(a)
|
||||
bInt := new(big.Int).SetBytes(b)
|
||||
sInt := new(big.Int).Add(aInt, bInt)
|
||||
x := sInt.Mod(sInt, btcec.S256().N).Bytes()
|
||||
x2 := [32]byte{}
|
||||
copy(x2[32-len(x):], x)
|
||||
return x2[:]
|
||||
}
|
||||
|
||||
func uint32ToBytes(i uint32) []byte {
|
||||
b := [4]byte{}
|
||||
binary.BigEndian.PutUint32(b[:], i)
|
||||
return b[:]
|
||||
}
|
||||
|
||||
func HexEncode(b []byte) string {
|
||||
return hex.EncodeToString(b)
|
||||
}
|
||||
|
||||
func HexDecode(str string) []byte {
|
||||
b, _ := hex.DecodeString(str)
|
||||
return b
|
||||
}
|
||||
|
||||
func I64(key []byte, data []byte) ([]byte, []byte) {
|
||||
mac := hmac.New(sha512.New, key)
|
||||
mac.Write(data)
|
||||
I := mac.Sum(nil)
|
||||
return I[:32], I[32:]
|
||||
}
|
||||
|
||||
func AddrFromPubKeyBytes(coin string, pubKeyBytes []byte) string {
|
||||
prefix := byte(0x00) // TODO Make const or configurable
|
||||
h160 := CalcHash160(pubKeyBytes)
|
||||
h160 = append([]byte{prefix}, h160...)
|
||||
checksum := CalcHash256(h160)
|
||||
b := append(h160, checksum[:4]...)
|
||||
return base58.Encode(b)
|
||||
}
|
||||
|
||||
func WIFFromPrivKeyBytes(coin string, privKeyBytes []byte, compress bool) string {
|
||||
prefix := byte(0x80) // TODO Make const or configurable
|
||||
bytes := append([]byte{prefix}, privKeyBytes...)
|
||||
if compress {
|
||||
bytes = append(bytes, byte(1))
|
||||
}
|
||||
checksum := CalcHash256(bytes)
|
||||
bytes = append(bytes, checksum[:4]...)
|
||||
return base58.Encode(bytes)
|
||||
}
|
||||
|
||||
func PubKeyBytesFromPrivKeyBytes(privKeyBytes []byte, compress bool) (pubKeyBytes []byte) {
|
||||
x, y := btcec.S256().ScalarBaseMult(privKeyBytes)
|
||||
pub := (*btcec.PublicKey)(&btcec.PublicKey{
|
||||
Curve: btcec.S256(),
|
||||
X: x,
|
||||
Y: y,
|
||||
})
|
||||
|
||||
if compress {
|
||||
return pub.SerializeCompressed()
|
||||
}
|
||||
return pub.SerializeUncompressed()
|
||||
}
|
||||
|
||||
// Calculate the hash of hasher over buf.
|
||||
func CalcHash(buf []byte, hasher hash.Hash) []byte {
|
||||
hasher.Write(buf)
|
||||
return hasher.Sum(nil)
|
||||
}
|
||||
|
||||
// calculate hash160 which is ripemd160(sha256(data))
|
||||
func CalcHash160(buf []byte) []byte {
|
||||
return CalcHash(CalcHash(buf, sha256.New()), ripemd160.New())
|
||||
}
|
||||
|
||||
// calculate hash256 which is sha256(sha256(data))
|
||||
func CalcHash256(buf []byte) []byte {
|
||||
return CalcHash(CalcHash(buf, sha256.New()), sha256.New())
|
||||
}
|
||||
|
||||
// calculate sha512(data)
|
||||
func CalcSha512(buf []byte) []byte {
|
||||
return CalcHash(buf, sha512.New())
|
||||
}
|
||||
|
||||
func ReverseBytes(buf []byte) []byte {
|
||||
res := []byte{}
|
||||
for i := len(buf) - 1; i >= 0; i-- {
|
||||
res = append(res, buf[i])
|
||||
}
|
||||
return res
|
||||
}
|
37
hd/address_test.go
Normal file
37
hd/address_test.go
Normal file
@ -0,0 +1,37 @@
|
||||
package hd
|
||||
|
||||
/*
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestManual(t *testing.T) {
|
||||
bytes, _ := hex.DecodeString("dfac699f1618c9be4df2befe94dc5f313946ebafa386756bd4926a1ecfd7cf2438426ede521d1ee6512391bc200b7910bcbea593e68d52b874c29bdc5a308ed1")
|
||||
fmt.Println(bytes)
|
||||
puk, prk, ch, se := ComputeMastersFromSeed(string(bytes))
|
||||
fmt.Println(puk, ch, se)
|
||||
|
||||
pubBytes2 := DerivePublicKeyForPath(
|
||||
HexDecode(puk),
|
||||
HexDecode(ch),
|
||||
//"44'/118'/0'/0/0",
|
||||
"0/0",
|
||||
)
|
||||
fmt.Printf("PUB2 %X\n", pubBytes2)
|
||||
|
||||
privBytes := DerivePrivateKeyForPath(
|
||||
HexDecode(prk),
|
||||
HexDecode(ch),
|
||||
//"44'/118'/0'/0/0",
|
||||
//"0/0",
|
||||
"44'/118'/0'/0/0",
|
||||
)
|
||||
fmt.Printf("PRIV %X\n", privBytes)
|
||||
pubBytes := PubKeyBytesFromPrivKeyBytes(privBytes, true)
|
||||
fmt.Printf("PUB %X\n", pubBytes)
|
||||
}
|
||||
|
||||
*/
|
189
hd/hd_test.go
Normal file
189
hd/hd_test.go
Normal file
@ -0,0 +1,189 @@
|
||||
package hd
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/hmac"
|
||||
"crypto/sha512"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/tyler-smith/go-bip39"
|
||||
|
||||
"github.com/btcsuite/btcd/chaincfg"
|
||||
"github.com/btcsuite/btcutil/hdkeychain"
|
||||
"github.com/mndrix/btcutil"
|
||||
"github.com/tyler-smith/go-bip32"
|
||||
|
||||
"github.com/tendermint/go-crypto"
|
||||
)
|
||||
|
||||
type addrData struct {
|
||||
Mnemonic string
|
||||
Master string
|
||||
Seed string
|
||||
Priv string
|
||||
Pub string
|
||||
Addr string
|
||||
}
|
||||
|
||||
// NOTE: atom fundraiser address
|
||||
var hdPath string = "m/44'/118'/0'/0/0"
|
||||
var hdToAddrTable []addrData
|
||||
|
||||
func init() {
|
||||
|
||||
b, err := ioutil.ReadFile("test.json")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
err = json.Unmarshal(b, &hdToAddrTable)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHDToAddr(t *testing.T) {
|
||||
|
||||
for i, d := range hdToAddrTable {
|
||||
privB, _ := hex.DecodeString(d.Priv)
|
||||
pubB, _ := hex.DecodeString(d.Pub)
|
||||
addrB, _ := hex.DecodeString(d.Addr)
|
||||
seedB, _ := hex.DecodeString(d.Seed)
|
||||
masterB, _ := hex.DecodeString(d.Master)
|
||||
|
||||
seed := bip39.NewSeed(d.Mnemonic, "")
|
||||
|
||||
fmt.Println("================================")
|
||||
fmt.Println("ROUND:", i, "MNEMONIC:", d.Mnemonic)
|
||||
|
||||
// master, priv, pub := tylerSmith(seed)
|
||||
// master, priv, pub := btcsuite(seed)
|
||||
master, priv, pub := gocrypto(seed)
|
||||
|
||||
fmt.Printf("\tNODEJS GOLANG\n")
|
||||
fmt.Printf("SEED \t%X %X\n", seedB, seed)
|
||||
fmt.Printf("MSTR \t%X %X\n", masterB, master)
|
||||
fmt.Printf("PRIV \t%X %X\n", privB, priv)
|
||||
fmt.Printf("PUB \t%X %X\n", pubB, pub)
|
||||
_, _ = priv, privB
|
||||
|
||||
assert.Equal(t, master, masterB, fmt.Sprintf("Expected masters to match for %d", i))
|
||||
assert.Equal(t, priv, privB, "Expected priv keys to match")
|
||||
assert.Equal(t, pub, pubB, fmt.Sprintf("Expected pub keys to match for %d", i))
|
||||
|
||||
var pubT crypto.PubKeySecp256k1
|
||||
copy(pubT[:], pub)
|
||||
addr := pubT.Address()
|
||||
fmt.Printf("ADDR \t%X %X\n", addrB, addr)
|
||||
assert.Equal(t, addr, addrB, fmt.Sprintf("Expected addresses to match %d", i))
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func ifExit(err error, n int) {
|
||||
if err != nil {
|
||||
fmt.Println(n, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func gocrypto(seed []byte) ([]byte, []byte, []byte) {
|
||||
|
||||
_, priv, ch, _ := ComputeMastersFromSeed(string(seed))
|
||||
|
||||
privBytes := DerivePrivateKeyForPath(
|
||||
HexDecode(priv),
|
||||
HexDecode(ch),
|
||||
"44'/118'/0'/0/0",
|
||||
)
|
||||
|
||||
pubBytes := PubKeyBytesFromPrivKeyBytes(privBytes, true)
|
||||
|
||||
return HexDecode(priv), privBytes, pubBytes
|
||||
}
|
||||
|
||||
func btcsuite(seed []byte) ([]byte, []byte, []byte) {
|
||||
fmt.Println("HD")
|
||||
masterKey, err := hdkeychain.NewMaster(seed, &chaincfg.MainNetParams)
|
||||
if err != nil {
|
||||
hmac := hmac.New(sha512.New, []byte("Bitcoin seed"))
|
||||
hmac.Write([]byte(seed))
|
||||
intermediary := hmac.Sum(nil)
|
||||
|
||||
curve := btcutil.Secp256k1()
|
||||
curveParams := curve.Params()
|
||||
|
||||
// Split it into our key and chain code
|
||||
keyBytes := intermediary[:32]
|
||||
fmt.Printf("\t%X\n", keyBytes)
|
||||
fmt.Printf("\t%X\n", curveParams.N.Bytes())
|
||||
keyInt, _ := binary.ReadVarint(bytes.NewBuffer(keyBytes))
|
||||
fmt.Printf("\t%d\n", keyInt)
|
||||
}
|
||||
fh := hdkeychain.HardenedKeyStart
|
||||
k, err := masterKey.Child(uint32(fh + 44))
|
||||
ifExit(err, 44)
|
||||
k, err = k.Child(uint32(fh + 118))
|
||||
ifExit(err, 118)
|
||||
k, err = k.Child(uint32(fh + 0))
|
||||
ifExit(err, 1)
|
||||
k, err = k.Child(uint32(0))
|
||||
ifExit(err, 2)
|
||||
k, err = k.Child(uint32(0))
|
||||
ifExit(err, 3)
|
||||
ecpriv, err := k.ECPrivKey()
|
||||
ifExit(err, 10)
|
||||
ecpub, err := k.ECPubKey()
|
||||
ifExit(err, 11)
|
||||
|
||||
priv := ecpriv.Serialize()
|
||||
pub := ecpub.SerializeCompressed()
|
||||
mkey, _ := masterKey.ECPrivKey()
|
||||
return mkey.Serialize(), priv, pub
|
||||
}
|
||||
|
||||
// return priv and pub
|
||||
func tylerSmith(seed []byte) ([]byte, []byte, []byte) {
|
||||
masterKey, err := bip32.NewMasterKey(seed)
|
||||
if err != nil {
|
||||
hmac := hmac.New(sha512.New, []byte("Bitcoin seed"))
|
||||
hmac.Write([]byte(seed))
|
||||
intermediary := hmac.Sum(nil)
|
||||
|
||||
curve := btcutil.Secp256k1()
|
||||
curveParams := curve.Params()
|
||||
|
||||
// Split it into our key and chain code
|
||||
keyBytes := intermediary[:32]
|
||||
fmt.Printf("\t%X\n", keyBytes)
|
||||
fmt.Printf("\t%X\n", curveParams.N.Bytes())
|
||||
keyInt, _ := binary.ReadVarint(bytes.NewBuffer(keyBytes))
|
||||
fmt.Printf("\t%d\n", keyInt)
|
||||
|
||||
}
|
||||
ifExit(err, 0)
|
||||
fh := bip32.FirstHardenedChild
|
||||
k, err := masterKey.NewChildKey(fh + 44)
|
||||
ifExit(err, 44)
|
||||
k, err = k.NewChildKey(fh + 118)
|
||||
ifExit(err, 118)
|
||||
k, err = k.NewChildKey(fh + 0)
|
||||
ifExit(err, 1)
|
||||
k, err = k.NewChildKey(0)
|
||||
ifExit(err, 2)
|
||||
k, err = k.NewChildKey(0)
|
||||
ifExit(err, 3)
|
||||
|
||||
priv := k.Key
|
||||
pub := k.PublicKey().Key
|
||||
return masterKey.Key, priv, pub
|
||||
}
|
1
hd/test.json
Normal file
1
hd/test.json
Normal file
File diff suppressed because one or more lines are too long
17
priv_key.go
17
priv_key.go
@ -32,8 +32,8 @@ var privKeyMapper data.Mapper
|
||||
// register both private key types with go-data (and thus go-wire)
|
||||
func init() {
|
||||
privKeyMapper = data.NewMapper(PrivKeyS{}).
|
||||
RegisterInterface(PrivKeyEd25519{}, NameEd25519, TypeEd25519).
|
||||
RegisterInterface(PrivKeySecp256k1{}, NameSecp256k1, TypeSecp256k1)
|
||||
RegisterImplementation(PrivKeyEd25519{}, NameEd25519, TypeEd25519).
|
||||
RegisterImplementation(PrivKeySecp256k1{}, NameSecp256k1, TypeSecp256k1)
|
||||
}
|
||||
|
||||
// PrivKeyS add json serialization to PrivKey
|
||||
@ -41,6 +41,13 @@ type PrivKeyS struct {
|
||||
PrivKey
|
||||
}
|
||||
|
||||
func WrapPrivKey(pk PrivKey) PrivKeyS {
|
||||
for ppk, ok := pk.(PrivKeyS); ok; ppk, ok = pk.(PrivKeyS) {
|
||||
pk = ppk.PrivKey
|
||||
}
|
||||
return PrivKeyS{pk}
|
||||
}
|
||||
|
||||
func (p PrivKeyS) MarshalJSON() ([]byte, error) {
|
||||
return privKeyMapper.ToJSON(p.PrivKey)
|
||||
}
|
||||
@ -160,9 +167,9 @@ func (privKey PrivKeySecp256k1) Sign(msg []byte) Signature {
|
||||
|
||||
func (privKey PrivKeySecp256k1) PubKey() PubKey {
|
||||
_, pub__ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKey[:])
|
||||
pub := [64]byte{}
|
||||
copy(pub[:], pub__.SerializeUncompressed()[1:])
|
||||
return PubKeySecp256k1(pub)
|
||||
var pub PubKeySecp256k1
|
||||
copy(pub[:], pub__.SerializeCompressed())
|
||||
return pub
|
||||
}
|
||||
|
||||
func (privKey PrivKeySecp256k1) Equals(other PrivKey) bool {
|
||||
|
38
pub_key.go
38
pub_key.go
@ -2,6 +2,7 @@ package crypto
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
|
||||
secp256k1 "github.com/btcsuite/btcd/btcec"
|
||||
"github.com/tendermint/ed25519"
|
||||
@ -26,8 +27,8 @@ var pubKeyMapper data.Mapper
|
||||
// register both public key types with go-data (and thus go-wire)
|
||||
func init() {
|
||||
pubKeyMapper = data.NewMapper(PubKeyS{}).
|
||||
RegisterInterface(PubKeyEd25519{}, NameEd25519, TypeEd25519).
|
||||
RegisterInterface(PubKeySecp256k1{}, NameSecp256k1, TypeSecp256k1)
|
||||
RegisterImplementation(PubKeyEd25519{}, NameEd25519, TypeEd25519).
|
||||
RegisterImplementation(PubKeySecp256k1{}, NameSecp256k1, TypeSecp256k1)
|
||||
}
|
||||
|
||||
// PubKeyS add json serialization to PubKey
|
||||
@ -35,6 +36,13 @@ type PubKeyS struct {
|
||||
PubKey
|
||||
}
|
||||
|
||||
func WrapPubKey(pk PubKey) PubKeyS {
|
||||
for ppk, ok := pk.(PubKeyS); ok; ppk, ok = pk.(PubKeyS) {
|
||||
pk = ppk.PubKey
|
||||
}
|
||||
return PubKeyS{pk}
|
||||
}
|
||||
|
||||
func (p PubKeyS) MarshalJSON() ([]byte, error) {
|
||||
return pubKeyMapper.ToJSON(p.PubKey)
|
||||
}
|
||||
@ -135,20 +143,20 @@ func (pubKey PubKeyEd25519) Equals(other PubKey) bool {
|
||||
|
||||
//-------------------------------------
|
||||
|
||||
// Implements PubKey
|
||||
type PubKeySecp256k1 [64]byte
|
||||
// Implements PubKey.
|
||||
// Compressed pubkey (just the x-cord),
|
||||
// prefixed with 0x02 or 0x03, depending on the y-cord.
|
||||
type PubKeySecp256k1 [33]byte
|
||||
|
||||
// Implements Bitcoin style addresses: RIPEMD160(SHA256(pubkey))
|
||||
func (pubKey PubKeySecp256k1) Address() []byte {
|
||||
w, n, err := new(bytes.Buffer), new(int), new(error)
|
||||
wire.WriteBinary(pubKey[:], w, n, err)
|
||||
if *err != nil {
|
||||
PanicCrisis(*err)
|
||||
}
|
||||
// append type byte
|
||||
encodedPubkey := append([]byte{TypeSecp256k1}, w.Bytes()...)
|
||||
hasher := ripemd160.New()
|
||||
hasher.Write(encodedPubkey) // does not error
|
||||
return hasher.Sum(nil)
|
||||
hasherSHA256 := sha256.New()
|
||||
hasherSHA256.Write(pubKey[:]) // does not error
|
||||
sha := hasherSHA256.Sum(nil)
|
||||
|
||||
hasherRIPEMD160 := ripemd160.New()
|
||||
hasherRIPEMD160.Write(sha) // does not error
|
||||
return hasherRIPEMD160.Sum(nil)
|
||||
}
|
||||
|
||||
func (pubKey PubKeySecp256k1) Bytes() []byte {
|
||||
@ -166,7 +174,7 @@ func (pubKey PubKeySecp256k1) VerifyBytes(msg []byte, sig_ Signature) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
pub__, err := secp256k1.ParsePubKey(append([]byte{0x04}, pubKey[:]...), secp256k1.S256())
|
||||
pub__, err := secp256k1.ParsePubKey(pubKey[:], secp256k1.S256())
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
41
pub_key_test.go
Normal file
41
pub_key_test.go
Normal file
@ -0,0 +1,41 @@
|
||||
package crypto
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"testing"
|
||||
|
||||
"github.com/btcsuite/btcutil/base58"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type keyData struct {
|
||||
priv string
|
||||
pub string
|
||||
addr string
|
||||
}
|
||||
|
||||
var secpDataTable = []keyData{
|
||||
{
|
||||
priv: "a96e62ed3955e65be32703f12d87b6b5cf26039ecfa948dc5107a495418e5330",
|
||||
pub: "02950e1cdfcb133d6024109fd489f734eeb4502418e538c28481f22bce276f248c",
|
||||
addr: "1CKZ9Nx4zgds8tU7nJHotKSDr4a9bYJCa3",
|
||||
},
|
||||
}
|
||||
|
||||
func TestPubKeySecp256k1Address(t *testing.T) {
|
||||
for _, d := range secpDataTable {
|
||||
privB, _ := hex.DecodeString(d.priv)
|
||||
pubB, _ := hex.DecodeString(d.pub)
|
||||
addrB, _, _ := base58.CheckDecode(d.addr)
|
||||
|
||||
var priv PrivKeySecp256k1
|
||||
copy(priv[:], privB)
|
||||
|
||||
pubT := priv.PubKey().(PubKeySecp256k1)
|
||||
pub := pubT[:]
|
||||
addr := priv.PubKey().Address()
|
||||
|
||||
assert.Equal(t, pub, pubB, "Expected pub keys to match")
|
||||
assert.Equal(t, addr, addrB, "Expected addresses to match")
|
||||
}
|
||||
}
|
11
signature.go
11
signature.go
@ -22,8 +22,8 @@ var sigMapper data.Mapper
|
||||
// register both public key types with go-data (and thus go-wire)
|
||||
func init() {
|
||||
sigMapper = data.NewMapper(SignatureS{}).
|
||||
RegisterInterface(SignatureEd25519{}, NameEd25519, TypeEd25519).
|
||||
RegisterInterface(SignatureSecp256k1{}, NameSecp256k1, TypeSecp256k1)
|
||||
RegisterImplementation(SignatureEd25519{}, NameEd25519, TypeEd25519).
|
||||
RegisterImplementation(SignatureSecp256k1{}, NameSecp256k1, TypeSecp256k1)
|
||||
}
|
||||
|
||||
// SignatureS add json serialization to Signature
|
||||
@ -31,6 +31,13 @@ type SignatureS struct {
|
||||
Signature
|
||||
}
|
||||
|
||||
func WrapSignature(sig Signature) SignatureS {
|
||||
for ssig, ok := sig.(SignatureS); ok; ssig, ok = sig.(SignatureS) {
|
||||
sig = ssig.Signature
|
||||
}
|
||||
return SignatureS{sig}
|
||||
}
|
||||
|
||||
func (p SignatureS) MarshalJSON() ([]byte, error) {
|
||||
return sigMapper.ToJSON(p.Signature)
|
||||
}
|
||||
|
@ -107,3 +107,37 @@ func TestSignatureEncodings(t *testing.T) {
|
||||
assert.True(t, strings.HasPrefix(text, tc.sigName))
|
||||
}
|
||||
}
|
||||
|
||||
func TestWrapping(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
// construct some basic constructs
|
||||
msg := CRandBytes(128)
|
||||
priv := GenPrivKeyEd25519()
|
||||
pub := priv.PubKey()
|
||||
sig := priv.Sign(msg)
|
||||
|
||||
// do some wrapping
|
||||
pubs := []PubKeyS{
|
||||
WrapPubKey(nil),
|
||||
WrapPubKey(pub),
|
||||
WrapPubKey(WrapPubKey(WrapPubKey(WrapPubKey(pub)))),
|
||||
WrapPubKey(PubKeyS{PubKeyS{PubKeyS{pub}}}),
|
||||
}
|
||||
for _, p := range pubs {
|
||||
_, ok := p.PubKey.(PubKeyS)
|
||||
assert.False(ok)
|
||||
}
|
||||
|
||||
sigs := []SignatureS{
|
||||
WrapSignature(nil),
|
||||
WrapSignature(sig),
|
||||
WrapSignature(WrapSignature(WrapSignature(WrapSignature(sig)))),
|
||||
WrapSignature(SignatureS{SignatureS{SignatureS{sig}}}),
|
||||
}
|
||||
for _, s := range sigs {
|
||||
_, ok := s.Signature.(SignatureS)
|
||||
assert.False(ok)
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user