Skip to content

Commit 430477a

Browse files
authored
move some solana utils from ntt sdk (#685)
1 parent 8309985 commit 430477a

File tree

4 files changed

+51
-14
lines changed

4 files changed

+51
-14
lines changed

platforms/solana/protocols/core/src/utils/instructions/governance.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,11 @@ export function getUpgradeContractAccounts(
248248
upgradeAuthority: deriveUpgradeAuthorityKey(wormholeProgramId),
249249
spill: new PublicKey(spill === undefined ? payer : spill),
250250
implementation: new SolanaAddress(newContract).unwrap(),
251-
programData: utils.deriveUpgradeableProgramKey(wormholeProgramId),
251+
programData: utils.deriveProgramDataAddress(wormholeProgramId),
252252
wormholeProgram: new PublicKey(wormholeProgramId),
253253
rent: SYSVAR_RENT_PUBKEY,
254254
clock: SYSVAR_CLOCK_PUBKEY,
255-
bpfLoaderUpgradeable: utils.BpfLoaderUpgradeable.programId,
255+
bpfLoaderUpgradeable: utils.BPF_LOADER_UPGRADEABLE_PROGRAM_ID,
256256
systemProgram: SystemProgram.programId,
257257
};
258258
}

platforms/solana/protocols/tokenBridge/src/utils/tokenBridge/instructions/governance.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,11 @@ export function getUpgradeContractAccounts(
144144
upgradeAuthority: CoreUtils.deriveUpgradeAuthorityKey(tokenBridgeProgramId),
145145
spill: new PublicKey(spill === undefined ? payer : spill),
146146
implementation: new PublicKey(vaa.payload.actionArgs.newContract),
147-
programData: utils.deriveUpgradeableProgramKey(tokenBridgeProgramId),
147+
programData: utils.deriveProgramDataAddress(tokenBridgeProgramId),
148148
tokenBridgeProgram: new PublicKey(tokenBridgeProgramId),
149149
rent: SYSVAR_RENT_PUBKEY,
150150
clock: SYSVAR_CLOCK_PUBKEY,
151-
bpfLoaderUpgradeable: utils.BpfLoaderUpgradeable.programId,
151+
bpfLoaderUpgradeable: utils.BPF_LOADER_UPGRADEABLE_PROGRAM_ID,
152152
systemProgram: SystemProgram.programId,
153153
};
154154
}

platforms/solana/src/utils/utils/account.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,21 @@ import { PublicKey } from '@solana/web3.js';
88
/**
99
* Find valid program address. See {@link PublicKey.findProgramAddressSync} for details.
1010
*
11-
* @param {(Buffer | Uint8Array)[]} seeds - seeds for PDA
11+
* @param {string | Buffer | Uint8Array |
12+
* readonly (string | Buffer | Uint8Array)[]} seeds - seeds for PDA
1213
* @param {PublicKeyInitData} programId - program address
1314
* @returns PDA
1415
*/
16+
type Seed = string | Buffer | Uint8Array;
17+
const toBytes = (s: Seed) => typeof s === "string" ? Buffer.from(s) : s;
1518
export function deriveAddress(
16-
seeds: (Buffer | Uint8Array)[],
19+
seeds: Seed | readonly Seed[],
1720
programId: PublicKeyInitData,
1821
): PublicKey {
19-
return PublicKey.findProgramAddressSync(seeds, new PublicKey(programId))[0];
22+
return PublicKey.findProgramAddressSync(
23+
Array.isArray(seeds) ? seeds.map(toBytes) : [toBytes(seeds as Seed)],
24+
new PublicKey(programId)
25+
)[0];
2026
}
2127

2228
/**

platforms/solana/src/utils/utils/bpfLoaderUpgradeable.ts

+38-7
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,46 @@ import type { PublicKeyInitData } from '@solana/web3.js';
22
import { PublicKey } from '@solana/web3.js';
33
import { deriveAddress } from './account.js';
44

5-
export class BpfLoaderUpgradeable {
6-
static programId: PublicKey = new PublicKey(
7-
'BPFLoaderUpgradeab1e11111111111111111111111',
8-
);
9-
}
5+
import type { CustomConversion, Layout } from '@wormhole-foundation/sdk-connect';
6+
7+
export const BPF_LOADER_UPGRADEABLE_PROGRAM_ID = new PublicKey(
8+
"BPFLoaderUpgradeab1e11111111111111111111111"
9+
);
1010

11-
export function deriveUpgradeableProgramKey(programId: PublicKeyInitData) {
11+
export function deriveProgramDataAddress(programId: PublicKeyInitData): PublicKey {
1212
return deriveAddress(
1313
[new PublicKey(programId).toBuffer()],
14-
BpfLoaderUpgradeable.programId,
14+
BPF_LOADER_UPGRADEABLE_PROGRAM_ID,
1515
);
1616
}
17+
18+
const pubKeyConversion = { //TODO find a better place for this
19+
to: (encoded: Uint8Array) => new PublicKey(encoded),
20+
from: (decoded: PublicKey) => decoded.toBytes(),
21+
} as const satisfies CustomConversion<Uint8Array, PublicKey>;
22+
23+
//neither anchor nor solana web3 have a built-in way to parse this
24+
//see here: https://docs.rs/solana-program/latest/solana_program/bpf_loader_upgradeable/enum.UpgradeableLoaderState.html
25+
export const programDataLayout = [
26+
{ name: "slot", binary: "uint", endianness: "little", size: 8 },
27+
{
28+
name: "upgradeAuthority",
29+
binary: "switch",
30+
idSize: 1,
31+
idTag: "isSome",
32+
layouts: [
33+
[[0, false], []],
34+
[
35+
[1, true],
36+
[
37+
{
38+
name: "value",
39+
binary: "bytes",
40+
size: 32,
41+
custom: pubKeyConversion,
42+
},
43+
],
44+
],
45+
],
46+
},
47+
] as const satisfies Layout;

0 commit comments

Comments
 (0)