utils.ts 974 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import {
  2. type AccountMeta,
  3. Connection,
  4. Keypair,
  5. PublicKey,
  6. Signer,
  7. TransactionInstruction,
  8. TransactionMessage,
  9. VersionedTransaction,
  10. } from '@solana/web3.js';
  11. import * as bs58 from 'bs58';
  12. export function loadWalletKey(keypairFile: string): Keypair {
  13. const fs = require('node:fs');
  14. return Keypair.fromSecretKey(new Uint8Array(JSON.parse(fs.readFileSync(keypairFile).toString())));
  15. }
  16. export function decode(stuff: string) {
  17. return bufferToArray(bs58.decode(stuff));
  18. }
  19. function bufferToArray(buffer: Buffer): number[] {
  20. const nums: number[] = [];
  21. for (let i = 0; i < buffer.length; i++) {
  22. nums.push(buffer[i]);
  23. }
  24. return nums;
  25. }
  26. export const mapProof = (assetProof: { proof: string[] }): AccountMeta[] => {
  27. if (!assetProof.proof || assetProof.proof.length === 0) {
  28. throw new Error('Proof is empty');
  29. }
  30. return assetProof.proof.map((node) => ({
  31. pubkey: new PublicKey(node),
  32. isSigner: false,
  33. isWritable: false,
  34. }));
  35. };