-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathaccounts.go
72 lines (54 loc) · 1.69 KB
/
accounts.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
package system
import (
"crypto/ed25519"
"github.com/code-payments/code-server/pkg/solana/binary"
"github.com/pkg/errors"
)
type NonceVersion uint32
const (
NonceAccountSize = 80
)
const (
NonceVersion0 NonceVersion = iota
NonceVersion1
)
var (
ErrInvalidAccountSize = errors.New("invalid nonce account size")
ErrInvalidAccountVersion = errors.New("invalid nonce account version")
)
// https://github.com/solana-labs/solana/blob/da00b39f4f92fb16417bd2d8bd218a04a34527b8/sdk/program/src/nonce/state/current.rs#L8
type NonceAccount struct {
Version uint32
State uint32
Authority ed25519.PublicKey
Blockhash ed25519.PublicKey
FeeCalculator FeeCalculator
}
type FeeCalculator struct {
LamportsPerSignature uint64
}
func (obj NonceAccount) Marshal() []byte {
res := make([]byte, NonceAccountSize)
var offset int
binary.PutUint32(res[offset:], obj.Version, &offset)
binary.PutUint32(res[offset:], obj.State, &offset)
binary.PutKey32(res[offset:], obj.Authority, &offset)
binary.PutKey32(res[offset:], obj.Blockhash, &offset)
binary.PutUint64(res[offset:], obj.FeeCalculator.LamportsPerSignature, &offset)
return res
}
func (obj *NonceAccount) Unmarshal(data []byte) error {
if len(data) != NonceAccountSize {
return ErrInvalidAccountSize
}
var offset int
binary.GetUint32(data[offset:], &obj.Version, &offset)
binary.GetUint32(data[offset:], &obj.State, &offset)
binary.GetKey32(data[offset:], &obj.Authority, &offset)
binary.GetKey32(data[offset:], &obj.Blockhash, &offset)
binary.GetUint64(data[offset:], &obj.FeeCalculator.LamportsPerSignature, &offset)
if NonceVersion(obj.Version) != NonceVersion1 {
return ErrInvalidAccountVersion
}
return nil
}