-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathaccount_common.go
82 lines (71 loc) · 2.19 KB
/
account_common.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
package incclient
import (
"encoding/json"
"fmt"
"github.com/incognitochain/go-incognito-sdk-v2/common"
"github.com/incognitochain/go-incognito-sdk-v2/common/base58"
"github.com/incognitochain/go-incognito-sdk-v2/key"
"github.com/incognitochain/go-incognito-sdk-v2/wallet"
)
// KeyInfo contains all key-related information of an account.
type KeyInfo struct {
PrivateKey string
PublicKey string
PaymentAddressV1 string
PaymentAddress string
ReadOnlyKey string
OTAPrivateKey string
MiningKey string
MiningPublicKey string
ValidatorPublicKey string
ShardID byte
}
func (k KeyInfo) String() string {
jsb, err := json.MarshalIndent(k, "", "\t")
if err != nil {
return ""
}
return string(jsb)
}
// GetAccountInfoFromPrivateKey returns all fields related to a private key.
func GetAccountInfoFromPrivateKey(privateKey string) (*KeyInfo, error) {
w, err := wallet.Base58CheckDeserialize(privateKey)
if err != nil {
return nil, err
}
if len(w.KeySet.PrivateKey) != 32 {
return nil, fmt.Errorf("privateKey is invalid")
}
pubKey := PrivateKeyToPublicKey(privateKey)
addr := PrivateKeyToPaymentAddress(privateKey, -1)
addrV1 := PrivateKeyToPaymentAddress(privateKey, 0)
readonlyKey := PrivateKeyToReadonlyKey(privateKey)
otaKey := PrivateKeyToPrivateOTAKey(privateKey)
miningKey := PrivateKeyToMiningKey(privateKey)
shardID := GetShardIDFromPrivateKey(privateKey)
miningKeyBytes, _, err := base58.Base58Check{}.Decode(miningKey)
if err != nil {
return nil, err
}
committeeKey, err := key.NewCommitteeKeyFromSeed(miningKeyBytes, pubKey)
if err != nil {
return nil, err
}
miningPubKeyStr, err := committeeKey.ToBase58()
if err != nil {
return nil, err
}
validatorPublicKey := committeeKey.GetMiningKeyBase58(common.BlsConsensus)
return &KeyInfo{
PrivateKey: privateKey,
PublicKey: base58.Base58Check{}.Encode(pubKey, common.ZeroByte),
PaymentAddressV1: addrV1,
PaymentAddress: addr,
ReadOnlyKey: readonlyKey,
OTAPrivateKey: otaKey,
MiningKey: miningKey,
MiningPublicKey: miningPubKeyStr,
ValidatorPublicKey: validatorPublicKey,
ShardID: shardID,
}, nil
}