1 // Package signers associates signers and their corresponding keys.
8 "github.com/vapor/crypto/ed25519/chainkd"
9 "github.com/vapor/errors"
15 AssetKeySpace keySpace = 0
16 AccountKeySpace keySpace = 1
20 //BIP0032 compatible previous derivation rule m/account/address_index
22 //BIP0032 path derivation rule m/purpose'/coin_type'/account'/change/address_index
27 // ErrBadQuorum is returned by Create when the quorum
28 // provided is less than 1 or greater than the number
30 ErrBadQuorum = errors.New("quorum must be greater than or equal to 1, and must be less than or equal to the length of xpubs")
32 // ErrBadXPub is returned by Create when the xpub
33 // provided isn't valid.
34 ErrBadXPub = errors.New("invalid xpub format")
36 // ErrNoXPubs is returned by create when the xpubs
37 // slice provided is empty.
38 ErrNoXPubs = errors.New("at least one xpub is required")
40 // ErrDupeXPub is returned by create when the same xpub
41 // appears twice in a single call.
42 ErrDupeXPub = errors.New("xpubs cannot contain the same key more than once")
43 ErrDeriveRule = errors.New("invalid key derive rule")
47 // BIP44Purpose purpose field 0x0000002c little-endian mode.
48 BIP44Purpose = []byte{0x2C, 0x00, 0x00, 0x00}
49 // BTMCoinType coin type field 0x00000099 little-endian mode.
50 BTMCoinType = []byte{0x99, 0x00, 0x00, 0x00}
53 // Signer is the abstract concept of a signer,
54 // which is composed of a set of keys as well as
55 // the amount of signatures needed for quorum.
57 Type string `json:"type"`
58 XPubs []chainkd.XPub `json:"xpubs"`
59 Quorum int `json:"quorum"`
60 KeyIndex uint64 `json:"key_index"`
61 DeriveRule uint8 `json:"derive_rule"`
64 // GetBip0032Path returns the complete path for bip0032 derived keys
65 func GetBip0032Path(s *Signer, ks keySpace, itemIndexes ...uint64) [][]byte {
67 signerPath := [9]byte{byte(ks)}
68 binary.LittleEndian.PutUint64(signerPath[1:], s.KeyIndex)
69 path = append(path, signerPath[:])
70 for _, idx := range itemIndexes {
72 binary.LittleEndian.PutUint64(idxBytes[:], idx)
73 path = append(path, idxBytes[:])
78 // getBip0044Path returns the complete path for bip0044 derived keys
79 func getBip0044Path(accountIndex uint64, change bool, addrIndex uint64) [][]byte {
81 path = append(path, BIP44Purpose[:]) //purpose
82 path = append(path, BTMCoinType[:]) //coin type
83 accIdxBytes := make([]byte, 4)
84 binary.LittleEndian.PutUint32(accIdxBytes, uint32(accountIndex))
85 path = append(path, accIdxBytes) //account index
86 branchBytes := make([]byte, 4)
88 binary.LittleEndian.PutUint32(branchBytes, uint32(1))
90 binary.LittleEndian.PutUint32(branchBytes, uint32(0))
92 path = append(path, branchBytes) //change
93 addrIdxBytes := make([]byte, 4)
94 binary.LittleEndian.PutUint32(addrIdxBytes[:], uint32(addrIndex))
95 path = append(path, addrIdxBytes[:]) //address index
99 // Path returns the complete path for derived keys
100 func Path(s *Signer, ks keySpace, change bool, addrIndex uint64) ([][]byte, error) {
101 switch s.DeriveRule {
103 return GetBip0032Path(s, ks, addrIndex), nil
105 return getBip0044Path(s.KeyIndex, change, addrIndex), nil
107 return nil, ErrDeriveRule
110 // Create creates and stores a Signer in the database
111 func Create(signerType string, xpubs []chainkd.XPub, quorum int, keyIndex uint64, deriveRule uint8) (*Signer, error) {
113 return nil, errors.Wrap(ErrNoXPubs)
116 xpubsMap := map[chainkd.XPub]bool{}
117 for _, xpub := range xpubs {
118 if _, ok := xpubsMap[xpub]; ok {
119 return nil, errors.WithDetailf(ErrDupeXPub, "duplicated key=%x", xpub)
121 xpubsMap[xpub] = true
124 if quorum == 0 || quorum > len(xpubs) {
125 return nil, errors.Wrap(ErrBadQuorum)
133 DeriveRule: deriveRule,
137 type SortKeys []chainkd.XPub
139 func (s SortKeys) Len() int { return len(s) }
140 func (s SortKeys) Less(i, j int) bool { return bytes.Compare(s[i][:], s[j][:]) < 0 }
141 func (s SortKeys) Swap(i, j int) { s[i], s[j] = s[j], s[i] }