From 343949824950f700e572ada151b4dc07fd68d229 Mon Sep 17 00:00:00 2001 From: Nick Frostbutter <75431177+nickfrosty@users.noreply.github.com> Date: Thu, 27 Feb 2025 11:33:36 -0500 Subject: [PATCH] added `transactionFromBase64` (#56) * feat: from base64 * refactor: correct decoder --- .changeset/twenty-jars-double.md | 5 +++++ .../gill/src/__tests__/base64-transactions.ts | 20 ++++++++++++++++++- .../gill/src/core/base64-from-transaction.ts | 12 +++++++++++ ...ansactions.ts => base64-to-transaction.ts} | 0 packages/gill/src/core/index.ts | 3 ++- packages/gill/src/core/prepare-transaction.ts | 2 +- 6 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 .changeset/twenty-jars-double.md create mode 100644 packages/gill/src/core/base64-from-transaction.ts rename packages/gill/src/core/{base64-transactions.ts => base64-to-transaction.ts} (100%) diff --git a/.changeset/twenty-jars-double.md b/.changeset/twenty-jars-double.md new file mode 100644 index 0000000..2959b1a --- /dev/null +++ b/.changeset/twenty-jars-double.md @@ -0,0 +1,5 @@ +--- +"gill": minor +--- + +added `transactionFromBase64` function diff --git a/packages/gill/src/__tests__/base64-transactions.ts b/packages/gill/src/__tests__/base64-transactions.ts index ca3223a..e05c600 100644 --- a/packages/gill/src/__tests__/base64-transactions.ts +++ b/packages/gill/src/__tests__/base64-transactions.ts @@ -1,7 +1,12 @@ import { blockhash } from "@solana/rpc-types"; import { address } from "@solana/addresses"; import { createKeyPairSignerFromPrivateKeyBytes, type KeyPairSigner } from "@solana/signers"; -import { createTransaction, transactionToBase64, transactionToBase64WithSigners } from "../core"; +import { + createTransaction, + transactionFromBase64, + transactionToBase64, + transactionToBase64WithSigners, +} from "../core"; // initialize a sample transaction const tx = createTransaction({ @@ -66,3 +71,16 @@ describe("transactionToBase64WithSigners", () => { expect(result).toBe(expected); }); }); + +describe("transactionFromBase64", () => { + test("can decode base64 an unsigned transaction", () => { + const input = + "AQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAABC7YxPJkVXZH3qqq8Nq1nwYa5Pm6+M9ZeObND0CCtBLXjfKbGfbEEIU1AEH81ttgpyiNLO+xurYCsjdCVcfR4YQA="; + + const tx = transactionFromBase64(input); + + const result = transactionToBase64(tx); + + expect(result).toBe(input); + }); +}); diff --git a/packages/gill/src/core/base64-from-transaction.ts b/packages/gill/src/core/base64-from-transaction.ts new file mode 100644 index 0000000..b4d6a78 --- /dev/null +++ b/packages/gill/src/core/base64-from-transaction.ts @@ -0,0 +1,12 @@ +import { getBase64Encoder } from "@solana/codecs"; +import { getTransactionDecoder, type Transaction } from "@solana/transactions"; +import type { transactionToBase64, transactionToBase64WithSigners } from "./base64-to-transaction"; + +/** + * Convert a base64 encoded transaction string into compiled transaction + * + * Use {@link transactionToBase64} or {@link transactionToBase64WithSigners} to create the base64 encoded transaction string + */ +export function transactionFromBase64(base64EncodedTransaction: string): Transaction { + return getTransactionDecoder().decode(getBase64Encoder().encode(base64EncodedTransaction)); +} diff --git a/packages/gill/src/core/base64-transactions.ts b/packages/gill/src/core/base64-to-transaction.ts similarity index 100% rename from packages/gill/src/core/base64-transactions.ts rename to packages/gill/src/core/base64-to-transaction.ts diff --git a/packages/gill/src/core/index.ts b/packages/gill/src/core/index.ts index f20bbb4..84db290 100644 --- a/packages/gill/src/core/index.ts +++ b/packages/gill/src/core/index.ts @@ -4,8 +4,9 @@ export * from "./utils"; export * from "./rpc"; export * from "./explorer"; export * from "./create-transaction"; -export * from "./base64-transactions"; export * from "./prepare-transaction"; export * from "./create-solana-client"; export * from "./accounts"; export * from "./keypairs"; +export * from "./base64-to-transaction"; +export * from "./base64-from-transaction"; diff --git a/packages/gill/src/core/prepare-transaction.ts b/packages/gill/src/core/prepare-transaction.ts index a865c1c..2991967 100644 --- a/packages/gill/src/core/prepare-transaction.ts +++ b/packages/gill/src/core/prepare-transaction.ts @@ -15,7 +15,7 @@ import type { GetLatestBlockhashApi, Rpc, SimulateTransactionApi } from "@solana import { isSetComputeLimitInstruction } from "../programs/compute-budget"; import { getComputeUnitEstimateForTransactionMessageFactory } from "../kit"; import { debug, isDebugEnabled } from "./debug"; -import { transactionToBase64WithSigners } from "./base64-transactions"; +import { transactionToBase64WithSigners } from "./base64-to-transaction"; type PrepareCompilableTransactionMessage = | CompilableTransactionMessage