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

added transactionFromBase64 #56

Merged
merged 2 commits into from
Feb 27, 2025
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
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