Skip to content

Commit

Permalink
added transactionFromBase64 (#56)
Browse files Browse the repository at this point in the history
* feat: from base64

* refactor: correct decoder
  • Loading branch information
nickfrosty authored Feb 27, 2025
1 parent 7e1ce79 commit 3439498
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/twenty-jars-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gill": minor
---

added `transactionFromBase64` function
20 changes: 19 additions & 1 deletion packages/gill/src/__tests__/base64-transactions.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand Down Expand Up @@ -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);
});
});
12 changes: 12 additions & 0 deletions packages/gill/src/core/base64-from-transaction.ts
Original file line number Diff line number Diff line change
@@ -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));
}
3 changes: 2 additions & 1 deletion packages/gill/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
2 changes: 1 addition & 1 deletion packages/gill/src/core/prepare-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3439498

Please sign in to comment.