Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move some solana utils from ntt sdk #685

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,11 @@ export function getUpgradeContractAccounts(
upgradeAuthority: deriveUpgradeAuthorityKey(wormholeProgramId),
spill: new PublicKey(spill === undefined ? payer : spill),
implementation: new SolanaAddress(newContract).unwrap(),
programData: utils.deriveUpgradeableProgramKey(wormholeProgramId),
programData: utils.deriveProgramDataAddress(wormholeProgramId),
wormholeProgram: new PublicKey(wormholeProgramId),
rent: SYSVAR_RENT_PUBKEY,
clock: SYSVAR_CLOCK_PUBKEY,
bpfLoaderUpgradeable: utils.BpfLoaderUpgradeable.programId,
bpfLoaderUpgradeable: utils.BPF_LOADER_UPGRADEABLE_PROGRAM_ID,
systemProgram: SystemProgram.programId,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,11 @@ export function getUpgradeContractAccounts(
upgradeAuthority: CoreUtils.deriveUpgradeAuthorityKey(tokenBridgeProgramId),
spill: new PublicKey(spill === undefined ? payer : spill),
implementation: new PublicKey(vaa.payload.actionArgs.newContract),
programData: utils.deriveUpgradeableProgramKey(tokenBridgeProgramId),
programData: utils.deriveProgramDataAddress(tokenBridgeProgramId),
tokenBridgeProgram: new PublicKey(tokenBridgeProgramId),
rent: SYSVAR_RENT_PUBKEY,
clock: SYSVAR_CLOCK_PUBKEY,
bpfLoaderUpgradeable: utils.BpfLoaderUpgradeable.programId,
bpfLoaderUpgradeable: utils.BPF_LOADER_UPGRADEABLE_PROGRAM_ID,
systemProgram: SystemProgram.programId,
};
}
12 changes: 9 additions & 3 deletions platforms/solana/src/utils/utils/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,21 @@ import { PublicKey } from '@solana/web3.js';
/**
* Find valid program address. See {@link PublicKey.findProgramAddressSync} for details.
*
* @param {(Buffer | Uint8Array)[]} seeds - seeds for PDA
* @param {string | Buffer | Uint8Array |
* readonly (string | Buffer | Uint8Array)[]} seeds - seeds for PDA
* @param {PublicKeyInitData} programId - program address
* @returns PDA
*/
type Seed = string | Buffer | Uint8Array;
const toBytes = (s: Seed) => typeof s === "string" ? Buffer.from(s) : s;
export function deriveAddress(
seeds: (Buffer | Uint8Array)[],
seeds: Seed | readonly Seed[],
programId: PublicKeyInitData,
): PublicKey {
return PublicKey.findProgramAddressSync(seeds, new PublicKey(programId))[0];
return PublicKey.findProgramAddressSync(
Array.isArray(seeds) ? seeds.map(toBytes) : [toBytes(seeds as Seed)],
new PublicKey(programId)
)[0];
}

/**
Expand Down
45 changes: 38 additions & 7 deletions platforms/solana/src/utils/utils/bpfLoaderUpgradeable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,46 @@ import type { PublicKeyInitData } from '@solana/web3.js';
import { PublicKey } from '@solana/web3.js';
import { deriveAddress } from './account.js';

export class BpfLoaderUpgradeable {
static programId: PublicKey = new PublicKey(
'BPFLoaderUpgradeab1e11111111111111111111111',
);
}
import type { CustomConversion, Layout } from '@wormhole-foundation/sdk-connect';

export const BPF_LOADER_UPGRADEABLE_PROGRAM_ID = new PublicKey(
"BPFLoaderUpgradeab1e11111111111111111111111"
);

export function deriveUpgradeableProgramKey(programId: PublicKeyInitData) {
export function deriveProgramDataAddress(programId: PublicKeyInitData): PublicKey {
return deriveAddress(
[new PublicKey(programId).toBuffer()],
BpfLoaderUpgradeable.programId,
BPF_LOADER_UPGRADEABLE_PROGRAM_ID,
);
}

const pubKeyConversion = { //TODO find a better place for this
to: (encoded: Uint8Array) => new PublicKey(encoded),
from: (decoded: PublicKey) => decoded.toBytes(),
} as const satisfies CustomConversion<Uint8Array, PublicKey>;

//neither anchor nor solana web3 have a built-in way to parse this
//see here: https://docs.rs/solana-program/latest/solana_program/bpf_loader_upgradeable/enum.UpgradeableLoaderState.html
export const programDataLayout = [
{ name: "slot", binary: "uint", endianness: "little", size: 8 },
{
name: "upgradeAuthority",
binary: "switch",
idSize: 1,
idTag: "isSome",
layouts: [
[[0, false], []],
[
[1, true],
[
{
name: "value",
binary: "bytes",
size: 32,
custom: pubKeyConversion,
},
],
],
],
},
] as const satisfies Layout;
Loading