aboutsummaryrefslogtreecommitdiffstats
path: root/ethcrypto/keypair.go
blob: ae9db3698e72cc8c2b263a4328d79c7b9caa247c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package ethcrypto

import (
    "github.com/ethereum/eth-go/ethutil"
    "github.com/obscuren/secp256k1-go"
)

type KeyPair struct {
    PrivateKey []byte
    PublicKey  []byte

    // The associated account
    // account *StateObject
}

func GenerateNewKeyPair() *KeyPair {
    _, prv := secp256k1.GenerateKeyPair()
    keyPair, _ := NewKeyPairFromSec(prv) // swallow error, this one cannot err
    return keyPair
}

func NewKeyPairFromSec(seckey []byte) (*KeyPair, error) {
    pubkey, err := secp256k1.GeneratePubKey(seckey)
    if err != nil {
        return nil, err
    }

    return &KeyPair{PrivateKey: seckey, PublicKey: pubkey}, nil
}

func (k *KeyPair) Address() []byte {
    return Sha3Bin(k.PublicKey[1:])[12:]
}

func (k *KeyPair) RlpEncode() []byte {
    return k.RlpValue().Encode()
}

func (k *KeyPair) RlpValue() *ethutil.Value {
    return ethutil.NewValue(k.PrivateKey)
}