-
Notifications
You must be signed in to change notification settings - Fork 4
/
instruction_activate.go
103 lines (82 loc) · 2.18 KB
/
instruction_activate.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
package timelock_token
import (
"bytes"
"crypto/ed25519"
)
var activateInstructionDiscriminator = []byte{
194, 203, 35, 100, 151, 55, 170, 82,
}
const (
ActivateInstructionArgsSize = (1 + // TimelockBump
8) // UnlockDuration
ActivateInstructionAccountsSize = (32 + // timelock
32 + // vaultOwner
32) // payer
ActivateInstructionSize = (8 + // discriminator
ActivateInstructionArgsSize + // args
ActivateInstructionAccountsSize) // accounts
)
type ActivateInstructionArgs struct {
TimelockBump uint8
UnlockDuration uint64
}
type ActivateInstructionAccounts struct {
Timelock ed25519.PublicKey
VaultOwner ed25519.PublicKey
Payer ed25519.PublicKey
}
func NewActivateInstruction(
accounts *ActivateInstructionAccounts,
args *ActivateInstructionArgs,
) Instruction {
var offset int
// Serialize instruction arguments
data := make([]byte,
len(activateInstructionDiscriminator)+
ActivateInstructionArgsSize)
putDiscriminator(data, activateInstructionDiscriminator, &offset)
putUint8(data, args.TimelockBump, &offset)
return Instruction{
Program: PROGRAM_ADDRESS,
// Instruction args
Data: data,
// Instruction accounts
Accounts: []AccountMeta{
{
PublicKey: accounts.Timelock,
IsWritable: true,
IsSigner: false,
},
{
PublicKey: accounts.VaultOwner,
IsWritable: false,
IsSigner: true,
},
{
PublicKey: accounts.Payer,
IsWritable: true,
IsSigner: true,
},
},
}
}
func ActivateInstructionFromBinary(data []byte) (*ActivateInstructionArgs, *ActivateInstructionAccounts, error) {
var offset int
var discriminator []byte
if len(data) < ActivateInstructionSize {
return nil, nil, ErrInvalidInstructionData
}
getDiscriminator(data, &discriminator, &offset)
if !bytes.Equal(discriminator, activateInstructionDiscriminator) {
return nil, nil, ErrInvalidInstructionData
}
var args ActivateInstructionArgs
var accounts ActivateInstructionAccounts
// Instruction Args
getUint8(data, &args.TimelockBump, &offset)
// Instruction Accounts
getKey(data, &accounts.Timelock, &offset)
getKey(data, &accounts.VaultOwner, &offset)
getKey(data, &accounts.Payer, &offset)
return &args, &accounts, nil
}