-
Notifications
You must be signed in to change notification settings - Fork 90
/
account.go
352 lines (306 loc) · 10 KB
/
account.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package crypto
import (
"crypto/sha512"
"errors"
"fmt"
"golang.org/x/crypto/ed25519"
"github.com/algorand/go-algorand-sdk/types"
)
// prefix for multisig transaction signing
const msigAddrPrefix = "MultisigAddr"
// Account holds both the public and private information associated with an
// Algorand address
type Account struct {
PublicKey ed25519.PublicKey
PrivateKey ed25519.PrivateKey
Address types.Address
}
func init() {
addrLen := len(types.Address{})
pkLen := ed25519.PublicKeySize
if addrLen != pkLen {
panic("address and public key are different sizes")
}
}
// GenerateAccount generates a random Account
func GenerateAccount() (kp Account) {
// Generate an ed25519 keypair. This should never fail
pk, sk, err := ed25519.GenerateKey(nil)
if err != nil {
panic(err)
}
// Convert the public key to an address
var a types.Address
n := copy(a[:], pk)
if n != ed25519.PublicKeySize {
panic("generated public key is the wrong size")
}
// Build the account
kp.PublicKey = pk
kp.PrivateKey = sk
kp.Address = a
return
}
// AccountFromPrivateKey derives the remaining Account fields from only a
// private key. The argument sk must have a length equal to
// ed25519.PrivateKeySize.
func AccountFromPrivateKey(sk ed25519.PrivateKey) (account Account, err error) {
if len(sk) != ed25519.PrivateKeySize {
err = errInvalidPrivateKey
return
}
// copy sk
account.PrivateKey = make(ed25519.PrivateKey, len(sk))
copy(account.PrivateKey, sk)
account.PublicKey = sk.Public().(ed25519.PublicKey)
if len(account.PublicKey) != ed25519.PublicKeySize {
err = errors.New("generated public key is the wrong size")
return
}
copy(account.Address[:], account.PublicKey)
return
}
/* Multisig Support */
// MultisigAccount is a convenience type for holding multisig preimage data
type MultisigAccount struct {
// Version is the version of this multisig
Version uint8
// Threshold is how many signatures are needed to fully sign as this address
Threshold uint8
// Pks is an ordered list of public keys that could potentially sign a message
Pks []ed25519.PublicKey
}
// MultisigAccountWithParams creates a MultisigAccount with the given parameters
func MultisigAccountWithParams(version uint8, threshold uint8, addrs []types.Address) (ma MultisigAccount, err error) {
ma.Version = version
ma.Threshold = threshold
ma.Pks = make([]ed25519.PublicKey, len(addrs))
for i := 0; i < len(addrs); i++ {
ma.Pks[i] = addrs[i][:]
}
err = ma.Validate()
return
}
// MultisigAccountFromSig is a convenience method that creates an account
// from a sig in a signed tx. Useful for getting addresses from signed msig txs, etc.
func MultisigAccountFromSig(sig types.MultisigSig) (ma MultisigAccount, err error) {
ma.Version = sig.Version
ma.Threshold = sig.Threshold
ma.Pks = make([]ed25519.PublicKey, len(sig.Subsigs))
for i := 0; i < len(sig.Subsigs); i++ {
c := make([]byte, len(sig.Subsigs[i].Key))
copy(c, sig.Subsigs[i].Key)
ma.Pks[i] = c
}
err = ma.Validate()
return
}
// Address takes this multisig preimage data, and generates the corresponding identifying
// address, committing to the exact group, version, and public keys that it requires to sign.
// Hash("MultisigAddr" || version uint8 || threshold uint8 || PK1 || PK2 || ...)
func (ma MultisigAccount) Address() (addr types.Address, err error) {
// See go-algorand/crypto/multisig.go
err = ma.Validate()
if err != nil {
return
}
buffer := append([]byte(msigAddrPrefix), byte(ma.Version), byte(ma.Threshold))
for _, pki := range ma.Pks {
buffer = append(buffer, pki[:]...)
}
return sha512.Sum512_256(buffer), nil
}
// Validate ensures that this multisig setup is a valid multisig account
func (ma MultisigAccount) Validate() (err error) {
if ma.Version != 1 {
err = errMsigUnknownVersion
return
}
if ma.Threshold == 0 || len(ma.Pks) == 0 || int(ma.Threshold) > len(ma.Pks) {
err = errMsigInvalidThreshold
return
}
return
}
// Blank return true if MultisigAccount is empty
// struct containing []ed25519.PublicKey cannot be compared
func (ma MultisigAccount) Blank() bool {
if ma.Version != 0 {
return false
}
if ma.Threshold != 0 {
return false
}
if ma.Pks != nil {
return false
}
return true
}
/* LogicSig support */
// LogicSigAddress returns the contract (escrow) address for a LogicSig.
//
// NOTE: If the LogicSig is delegated to another account this will not
// return the delegated address of the LogicSig.
func LogicSigAddress(lsig types.LogicSig) types.Address {
toBeSigned := programToSign(lsig.Logic)
checksum := sha512.Sum512_256(toBeSigned)
var addr types.Address
n := copy(addr[:], checksum[:])
if n != ed25519.PublicKeySize {
panic(fmt.Sprintf("Generated public key has length of %d, expected %d", n, ed25519.PublicKeySize))
}
return addr
}
// LogicSigAccount represents an account that can sign with a LogicSig program.
type LogicSigAccount struct {
_struct struct{} `codec:",omitempty,omitemptyarray"`
// The underlying LogicSig object
Lsig types.LogicSig `codec:"lsig"`
// The key that provided Lsig.Sig, if any
SigningKey ed25519.PublicKey `codec:"sigkey"`
}
// MakeLogicSigAccountEscrow creates a new escrow LogicSigAccount. The address
// of this account will be a hash of its program.
func MakeLogicSigAccountEscrow(program []byte, args [][]byte) LogicSigAccount {
return LogicSigAccount{
Lsig: types.LogicSig{
Logic: program,
Args: args,
},
}
}
// MakeLogicSigAccountDelegated creates a new delegated LogicSigAccount. This
// type of LogicSig has the authority to sign transactions on behalf of another
// account, called the delegating account. If the delegating account is a
// multisig account, use MakeLogicSigAccountDelegated instead.
//
// The parameter signer is the private key of the delegating account.
func MakeLogicSigAccountDelegated(program []byte, args [][]byte, signer ed25519.PrivateKey) (lsa LogicSigAccount, err error) {
var ma MultisigAccount
lsig, err := MakeLogicSig(program, args, signer, ma)
if err != nil {
return
}
signerAccount, err := AccountFromPrivateKey(signer)
if err != nil {
return
}
lsa = LogicSigAccount{
Lsig: lsig,
// attach SigningKey to remember which account the signature belongs to
SigningKey: signerAccount.PublicKey,
}
return
}
// MakeLogicSigAccountDelegatedMsig creates a new delegated LogicSigAccount.
// This type of LogicSig has the authority to sign transactions on behalf of
// another account, called the delegating account. Use this function if the
// delegating account is a multisig account, otherwise use
// MakeLogicSigAccountDelegated.
//
// The parameter msigAccount is the delegating multisig account.
//
// The parameter signer is the private key of one of the members of the
// delegating multisig account. Use the method AppendMultisigSignature on the
// returned LogicSigAccount to add additional signatures from other members.
func MakeLogicSigAccountDelegatedMsig(program []byte, args [][]byte, msigAccount MultisigAccount, signer ed25519.PrivateKey) (lsa LogicSigAccount, err error) {
lsig, err := MakeLogicSig(program, args, signer, msigAccount)
if err != nil {
return
}
lsa = LogicSigAccount{
Lsig: lsig,
// do not attach SigningKey, since that doesn't apply to an msig signature
}
return
}
// AppendMultisigSignature adds an additional signature from a member of the
// delegating multisig account.
//
// The LogicSigAccount must represent a delegated LogicSig backed by a multisig
// account.
func (lsa *LogicSigAccount) AppendMultisigSignature(signer ed25519.PrivateKey) error {
return AppendMultisigToLogicSig(&lsa.Lsig, signer)
}
// LogicSigAccountFromLogicSig creates a LogicSigAccount from an existing
// LogicSig object.
//
// The parameter signerPublicKey must be present if the LogicSig is delegated
// and the delegating account is backed by a single private key (i.e. not a
// multisig account). In this case, signerPublicKey must be the public key of
// the delegating account. In all other cases, an error will be returned if
// signerPublicKey is present.
func LogicSigAccountFromLogicSig(lsig types.LogicSig, signerPublicKey *ed25519.PublicKey) (lsa LogicSigAccount, err error) {
hasSig := lsig.Sig != (types.Signature{})
hasMsig := !lsig.Msig.Blank()
if hasSig && hasMsig {
err = errLsigTooManySignatures
return
}
if hasSig {
if signerPublicKey == nil {
err = errLsigNoPublicKey
return
}
toBeSigned := programToSign(lsig.Logic)
valid := ed25519.Verify(*signerPublicKey, toBeSigned, lsig.Sig[:])
if !valid {
err = errLsigInvalidPublicKey
return
}
lsa.Lsig = lsig
lsa.SigningKey = make(ed25519.PublicKey, len(*signerPublicKey))
copy(lsa.SigningKey, *signerPublicKey)
return
}
if signerPublicKey != nil {
err = errLsigAccountPublicKeyNotNeeded
return
}
lsa.Lsig = lsig
return
}
// IsDelegated returns true if and only if the LogicSig has been delegated to
// another account with a signature.
//
// Note this function only checks for the presence of a delegation signature. To
// verify the delegation signature, use VerifyLogicSig.
func (lsa LogicSigAccount) IsDelegated() bool {
hasSig := lsa.Lsig.Sig != (types.Signature{})
hasMsig := !lsa.Lsig.Msig.Blank()
return hasSig || hasMsig
}
// Address returns the address of this LogicSigAccount.
//
// If the LogicSig is delegated to another account, this will return the address
// of that account.
//
// If the LogicSig is not delegated to another account, this will return an
// escrow address that is the hash of the LogicSig's program code.
func (lsa LogicSigAccount) Address() (addr types.Address, err error) {
hasSig := lsa.Lsig.Sig != (types.Signature{})
hasMsig := !lsa.Lsig.Msig.Blank()
// require at most one sig
if hasSig && hasMsig {
err = errLsigTooManySignatures
return
}
if hasSig {
n := copy(addr[:], lsa.SigningKey)
if n != ed25519.PublicKeySize {
err = fmt.Errorf("Generated public key has length of %d, expected %d", n, ed25519.PublicKeySize)
}
return
}
if hasMsig {
var msigAccount MultisigAccount
msigAccount, err = MultisigAccountFromSig(lsa.Lsig.Msig)
if err != nil {
return
}
addr, err = msigAccount.Address()
return
}
addr = LogicSigAddress(lsa.Lsig)
return
}