-
Notifications
You must be signed in to change notification settings - Fork 39
/
identity.go
99 lines (85 loc) · 2.91 KB
/
identity.go
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package account
import (
"bytes"
"crypto/ecdsa"
"crypto/rand"
"encoding/binary"
"fmt"
"github.com/ethereum/go-ethereum/crypto"
"math/big"
)
// Identity include recipient and key
type Identity struct {
recipient Recipient
key Key
}
// NewIdentity new identity include recipient and key
func NewIdentity(recipient Recipient, key Key) Identity {
return Identity{
recipient: recipient,
key: key,
}
}
// CreatRandomIdentity create a random identity
func CreatRandomIdentity() (Identity, error) {
sk, err := ecdsa.GenerateKey(crypto.S256(), rand.Reader)
if err != nil {
return Identity{}, ErrGenIdentityKey
}
key := crypto.FromECDSA(sk)
if len(key) != KeyLength {
return Identity{}, fmt.Errorf("privateKey To Bytes falied: unexceptd %d ,excepted 32", len(key))
}
if len(crypto.FromECDSAPub(&sk.PublicKey)) != 2*KeyLength+1 {
return Identity{}, fmt.Errorf("fromECDSAPub len is not match :unexcepted %d,excepted 65", len(crypto.FromECDSAPub(&sk.PublicKey)))
}
recipient := crypto.Keccak256(crypto.FromECDSAPub(&sk.PublicKey)[1:])
if len(recipient) != KeyLength {
return Identity{}, fmt.Errorf("recipient len is not match:unexceptd %d,exceptd 32", len(recipient))
}
return newIdentity(recipient, key)
}
// CreatIdentityFromKey creat identity from key
func CreatIdentityFromKey(key Key) (Identity, error) {
keyValue := big.NewInt(0)
keyValue.SetBytes(key.Bytes())
sk := new(ecdsa.PrivateKey)
sk.PublicKey.Curve = crypto.S256()
sk.D = keyValue
sk.PublicKey.X, sk.PublicKey.Y = crypto.S256().ScalarBaseMult(keyValue.Bytes())
if len(crypto.FromECDSAPub(&sk.PublicKey)) != 2*KeyLength+1 {
return Identity{}, fmt.Errorf("fromECDSAPub len is not match :unexcepted %d,excepted %d", len(crypto.FromECDSAPub(&sk.PublicKey)), 2*KeyLength+1)
}
recipient := crypto.Keccak256(crypto.FromECDSAPub(&sk.PublicKey)[1:]) //"0x04"+64
if len(recipient) != KeyLength {
return Identity{}, fmt.Errorf("recipient len is not match:unexceptd %d,exceptd 32", len(recipient))
}
return newIdentity(recipient, key.Bytes())
}
func newIdentity(recipient []byte, key []byte) (Identity, error) {
recipientType := BytesToIdentityRecipient(recipient[(len(recipient) - RecipientLength):])
keyType := BytesToIdentityKey(key)
return NewIdentity(recipientType, keyType), nil
}
// GetDefaultFullShardKey get identity's default fullShardKey
func (Self *Identity) GetDefaultFullShardKey() (uint32, error) {
var fullShardKey uint32
r := Self.recipient
realShardKey := []byte{0x00, 0x00}
realShardKey = append(realShardKey, r[0:1]...)
realShardKey = append(realShardKey, r[10:11]...)
buffer := bytes.NewBuffer(realShardKey)
err := binary.Read(buffer, binary.BigEndian, &fullShardKey)
if err != nil {
return fullShardKey, err
}
return fullShardKey, nil
}
// GetRecipient Get it's recipient
func (Self *Identity) GetRecipient() Recipient {
return Self.recipient
}
// GetKey get it's key
func (Self *Identity) GetKey() Key {
return Self.key
}