Skip to content

Commit cff3ef8

Browse files
author
Jeff Yanta
committed
Add support for VM airdrop virtual instruction
1 parent eed91c1 commit cff3ef8

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

pkg/solana/cvm/types_message.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,30 @@ func GetCompactWithdrawMessage(args *GetCompactWithdrawMessageArgs) CompactMessa
4949
return hashMessage(message)
5050
}
5151

52+
type GetCompactAirdropMessageArgs struct {
53+
Source ed25519.PublicKey
54+
Destinations []ed25519.PublicKey
55+
Amount uint64
56+
NonceAddress ed25519.PublicKey
57+
NonceValue Hash
58+
}
59+
60+
func GetCompactAirdropMessage(args *GetCompactAirdropMessageArgs) CompactMessage {
61+
amountBytes := make([]byte, 8)
62+
binary.LittleEndian.PutUint64(amountBytes, args.Amount)
63+
64+
var message Message
65+
message = append(message, []byte("airdrop")...)
66+
message = append(message, args.Source...)
67+
message = append(message, args.NonceAddress...)
68+
message = append(message, args.NonceValue[:]...)
69+
message = append(message, amountBytes...)
70+
for _, destination := range args.Destinations {
71+
message = append(message, destination...)
72+
}
73+
return hashMessage(message)
74+
}
75+
5276
func hashMessage(msg Message) CompactMessage {
5377
h := sha256.New()
5478
h.Write(msg)

pkg/solana/cvm/types_opcode.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ const (
1414
OpcodeExternalRelay Opcode = 20
1515

1616
OpcodeConditionalTransfer Opcode = 12
17+
18+
OpcodeAirdrop Opcode = 30
1719
)
1820

1921
func putOpcode(dst []byte, v Opcode, offset *int) {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package cvm
2+
3+
const (
4+
AirdropVirtrualInstructionDataSize = (SignatureSize + // signature
5+
8 + // amount
6+
1) // count
7+
)
8+
9+
type AirdropVirtualInstructionArgs struct {
10+
Amount uint64
11+
Count uint8
12+
Signature Signature
13+
}
14+
15+
func NewAirdropVirtualInstruction(
16+
args *AirdropVirtualInstructionArgs,
17+
) VirtualInstruction {
18+
var offset int
19+
data := make([]byte, AirdropVirtrualInstructionDataSize)
20+
putSignature(data, args.Signature, &offset)
21+
putUint64(data, args.Amount, &offset)
22+
putUint8(data, args.Count, &offset)
23+
24+
return VirtualInstruction{
25+
Opcode: OpcodeAirdrop,
26+
Data: data,
27+
}
28+
}

0 commit comments

Comments
 (0)