4
4
encoding ,
5
5
} from "@wormhole-foundation/sdk-base" ;
6
6
7
- import { PublicKey , PublicKeyInitData } from "@solana/web3.js" ;
7
+ import { PublicKey , PublicKeyInitData , TransactionInstruction } from "@solana/web3.js" ;
8
8
import { BN } from "@coral-xyz/anchor" ;
9
9
10
10
const CHAIN_ID_BYTE_SIZE = 2 ;
@@ -39,13 +39,13 @@ export const U64 = {
39
39
MAX : new BN ( ( 2n ** 64n - 1n ) . toString ( ) ) ,
40
40
to : ( amount : number , unit : number ) => {
41
41
const ret = new BN ( Math . round ( amount * unit ) ) ;
42
-
42
+
43
43
if ( ret . isNeg ( ) )
44
44
throw new Error ( "Value negative" ) ;
45
-
45
+
46
46
if ( ret . bitLength ( ) > 64 )
47
- throw new Error ( "Value too large" ) ;
48
-
47
+ throw new Error ( "Value too large" ) ;
48
+
49
49
return ret ;
50
50
} ,
51
51
from : ( amount : BN , unit : number ) => amount . toNumber ( ) / unit ,
@@ -57,8 +57,41 @@ export function derivePda(
57
57
programId : PublicKeyInitData
58
58
) {
59
59
const toBytes = ( s : string | Uint8Array ) => typeof s === "string" ? encoding . bytes . encode ( s ) : s ;
60
- return PublicKey . findProgramAddressSync (
60
+ return PublicKey . findProgramAddressSync (
61
61
Array . isArray ( seeds ) ? seeds . map ( toBytes ) : [ toBytes ( seeds as Seed ) ] ,
62
62
new PublicKey ( programId ) ,
63
63
) [ 0 ] ;
64
- }
64
+ }
65
+
66
+ // governance utils
67
+
68
+ export function serializeInstruction ( ix : TransactionInstruction ) : Buffer {
69
+ const programId = ix . programId . toBuffer ( ) ;
70
+ const accountsLen = Buffer . alloc ( 2 ) ;
71
+ accountsLen . writeUInt16BE ( ix . keys . length ) ;
72
+ const accounts = Buffer . concat ( ix . keys . map ( ( account ) => {
73
+ const isSigner = Buffer . alloc ( 1 ) ;
74
+ isSigner . writeUInt8 ( account . isSigner ? 1 : 0 ) ;
75
+ const isWritable = Buffer . alloc ( 1 ) ;
76
+ isWritable . writeUInt8 ( account . isWritable ? 1 : 0 ) ;
77
+ const pubkey = account . pubkey . toBuffer ( ) ;
78
+ return Buffer . concat ( [ pubkey , isSigner , isWritable ] ) ;
79
+ } ) )
80
+ const dataLen = Buffer . alloc ( 2 ) ;
81
+ dataLen . writeUInt16BE ( ix . data . length ) ;
82
+ return Buffer . concat ( [ programId , accountsLen , accounts , dataLen , ix . data ] ) ;
83
+ }
84
+
85
+ export function appendGovernanceHeader ( data : Buffer , governanceProgramId : PublicKey ) : Buffer {
86
+ const module = Buffer . from ( "GeneralPurposeGovernance" . padStart ( 32 , "\0" ) ) ;
87
+ const action = Buffer . alloc ( 1 ) ;
88
+ action . writeUInt8 ( 2 ) ; // SolanaCall
89
+ const chainId = Buffer . alloc ( 2 ) ;
90
+ chainId . writeUInt16BE ( 1 ) ; // solana
91
+ const programId = governanceProgramId . toBuffer ( ) ;
92
+ return Buffer . concat ( [ module , action , chainId , programId , data ] ) ;
93
+ }
94
+
95
+ // sentinel values used in governance
96
+ export const OWNER = new PublicKey ( Buffer . from ( "owner" . padEnd ( 32 , "\0" ) ) ) ;
97
+ export const PAYER = new PublicKey ( Buffer . from ( "payer" . padEnd ( 32 , "\0" ) ) ) ;
0 commit comments