Skip to content
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
25 changes: 25 additions & 0 deletions packages/wasm-solana/eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,29 @@ export default tseslint.config(
{
ignores: ["dist/", "pkg/", "target/", "node_modules/", "js/wasm/", "*.config.js"],
},
// Ban Node.js globals in production code
{
files: ["js/**/*.ts"],
rules: {
"no-restricted-globals": [
"error",
{
name: "Buffer",
message: "Use Uint8Array instead of Buffer for ESM compatibility.",
},
{
name: "process",
message: "Avoid Node.js process global for ESM compatibility.",
},
{
name: "__dirname",
message: "Use import.meta.url instead of __dirname for ESM.",
},
{
name: "__filename",
message: "Use import.meta.url instead of __filename for ESM.",
},
],
},
},
);
7 changes: 3 additions & 4 deletions packages/wasm-solana/js/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,10 @@ export class Transaction {
/**
* Serialize the message portion of the transaction.
* Alias for signablePayload() - provides compatibility with @solana/web3.js API.
* Returns a Buffer for compatibility with code expecting .toString('base64').
* @returns The serialized message bytes as a Buffer
* @returns The serialized message bytes
*/
serializeMessage(): Buffer {
return Buffer.from(this.signablePayload());
serializeMessage(): Uint8Array {
return this.signablePayload();
}

/**
Expand Down
22 changes: 3 additions & 19 deletions packages/wasm-solana/js/versioned.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,6 @@ export class VersionedTransaction {
return new VersionedTransaction(WasmVersionedTransaction.from_bytes(bytes));
}

/**
* Deserialize a transaction from base64 string.
*/
static fromBase64(base64: string): VersionedTransaction {
const bytes = Uint8Array.from(Buffer.from(base64, "base64"));
return VersionedTransaction.fromBytes(bytes);
}

/**
* Create a VersionedTransaction from a WasmVersionedTransaction instance.
* @internal Used by builder functions
Expand Down Expand Up @@ -188,11 +180,10 @@ export class VersionedTransaction {
/**
* Serialize the message portion of the transaction.
* Alias for signablePayload() - provides compatibility with @solana/web3.js API.
* Returns a Buffer for compatibility with code expecting .toString('base64').
* @returns The serialized message bytes as a Buffer
* @returns The serialized message bytes
*/
serializeMessage(): Buffer {
return Buffer.from(this.signablePayload());
serializeMessage(): Uint8Array {
return this.signablePayload();
}

/**
Expand All @@ -202,13 +193,6 @@ export class VersionedTransaction {
return this.inner.to_bytes();
}

/**
* Serialize the transaction to base64.
*/
toBase64(): string {
return Buffer.from(this.toBytes()).toString("base64");
}

/**
* Serialize to network broadcast format.
* @returns The transaction as bytes ready for broadcast
Expand Down
30 changes: 20 additions & 10 deletions packages/wasm-solana/test/versioned.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import * as assert from "assert";
import { VersionedTransaction, isVersionedTransaction } from "../js/versioned.js";

/** Helper to decode base64 in tests (Buffer is allowed in tests) */
function base64ToBytes(base64: string): Uint8Array {
return Uint8Array.from(Buffer.from(base64, "base64"));
}

/** Helper to encode bytes to base64 in tests */
function bytesToBase64(bytes: Uint8Array): string {
return Buffer.from(bytes).toString("base64");
}

describe("VersionedTransaction", () => {
// Legacy transaction (same as transaction.ts test)
const LEGACY_TX_BASE64 =
Expand All @@ -15,22 +25,22 @@ describe("VersionedTransaction", () => {

describe("legacy transaction parsing", () => {
it("should parse legacy transaction as versioned", () => {
const tx = VersionedTransaction.fromBase64(LEGACY_TX_BASE64);
const tx = VersionedTransaction.fromBytes(base64ToBytes(LEGACY_TX_BASE64));

assert.strictEqual(tx.isVersioned, false);
assert.ok(tx.feePayer);
assert.ok(tx.recentBlockhash);
});

it("should have empty address lookup tables for legacy", () => {
const tx = VersionedTransaction.fromBase64(LEGACY_TX_BASE64);
const tx = VersionedTransaction.fromBytes(base64ToBytes(LEGACY_TX_BASE64));
const alts = tx.addressLookupTables();

assert.strictEqual(alts.length, 0);
});

it("should have static account keys", () => {
const tx = VersionedTransaction.fromBase64(LEGACY_TX_BASE64);
const tx = VersionedTransaction.fromBytes(base64ToBytes(LEGACY_TX_BASE64));
const keys = tx.staticAccountKeys();

assert.ok(Array.isArray(keys));
Expand All @@ -40,7 +50,7 @@ describe("VersionedTransaction", () => {
});

it("should get instructions", () => {
const tx = VersionedTransaction.fromBase64(LEGACY_TX_BASE64);
const tx = VersionedTransaction.fromBytes(base64ToBytes(LEGACY_TX_BASE64));
const instructions = tx.instructions();

assert.ok(Array.isArray(instructions));
Expand All @@ -53,15 +63,15 @@ describe("VersionedTransaction", () => {
});

it("should get signable payload", () => {
const tx = VersionedTransaction.fromBase64(LEGACY_TX_BASE64);
const tx = VersionedTransaction.fromBytes(base64ToBytes(LEGACY_TX_BASE64));
const payload = tx.signablePayload();

assert.ok(payload instanceof Uint8Array);
assert.ok(payload.length > 0);
});

it("should roundtrip", () => {
const tx = VersionedTransaction.fromBase64(LEGACY_TX_BASE64);
const tx = VersionedTransaction.fromBytes(base64ToBytes(LEGACY_TX_BASE64));
const bytes = tx.toBytes();

const tx2 = VersionedTransaction.fromBytes(bytes);
Expand All @@ -71,7 +81,7 @@ describe("VersionedTransaction", () => {
});

it("should add signature", () => {
const tx = VersionedTransaction.fromBase64(LEGACY_TX_BASE64);
const tx = VersionedTransaction.fromBytes(base64ToBytes(LEGACY_TX_BASE64));
const feePayer = tx.feePayer;

const signature = new Uint8Array(64).fill(42);
Expand All @@ -85,10 +95,10 @@ describe("VersionedTransaction", () => {

describe("base64 serialization", () => {
it("should roundtrip base64", () => {
const tx = VersionedTransaction.fromBase64(LEGACY_TX_BASE64);
const base64 = tx.toBase64();
const tx = VersionedTransaction.fromBytes(base64ToBytes(LEGACY_TX_BASE64));
const base64 = bytesToBase64(tx.toBytes());

const tx2 = VersionedTransaction.fromBase64(base64);
const tx2 = VersionedTransaction.fromBytes(base64ToBytes(base64));
assert.strictEqual(tx.feePayer, tx2.feePayer);
});
});
Expand Down