Skip to content

Commit 448216a

Browse files
committed
examples and tests
1 parent 38b9b6c commit 448216a

File tree

2 files changed

+105
-78
lines changed

2 files changed

+105
-78
lines changed

examples/with-solana/src/advanced.ts

+25-15
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { Turnkey } from "@turnkey/sdk-server";
1818
import { TurnkeySigner } from "@turnkey/solana";
1919

2020
import { createNewSolanaWallet, solanaNetwork } from "./utils";
21+
import { assertNonNull } from "@turnkey/http";
2122

2223
const TURNKEY_WAR_CHEST = "tkhqC9QX2gkqJtUFk2QKhBmQfFyyqZXSpr73VFRi35C";
2324

@@ -145,24 +146,33 @@ async function main() {
145146
unsignedTxs.push(transferTransaction);
146147
}
147148

148-
const signedTransactions = (await turnkeySigner.signAllTransactions(
149-
unsignedTxs,
150-
solAddress
151-
)) as VersionedTransaction[];
149+
const signedTransactionsResult = await turnkeySigner.signAllTransactions([
150+
{
151+
signWith: solAddress,
152+
transactions: unsignedTxs as VersionedTransaction[],
153+
},
154+
]);
152155

153-
for (let i = 0; i < signedTransactions.length; i++) {
154-
const isValidSignature = nacl.sign.detached.verify(
155-
signedTransactions[i]!.message.serialize(),
156-
signedTransactions[i]!.signatures[0]!,
157-
bs58.decode(solAddress)
158-
);
156+
const result = assertNonNull(signedTransactionsResult);
159157

160-
if (!isValidSignature) {
161-
throw new Error("unable to verify transaction signatures");
162-
}
158+
for (let i = 0; i < result.length; i++) {
159+
const txs = result[i]?.transactions!;
160+
161+
for (const t of txs) {
162+
const signedTx = t as VersionedTransaction;
163+
const isValidSignature = nacl.sign.detached.verify(
164+
signedTx!.message.serialize(),
165+
signedTx!.signatures[0]!,
166+
bs58.decode(solAddress)
167+
);
163168

164-
// 3. Broadcast the signed payload on devnet
165-
await solanaNetwork.broadcast(connection, signedTransactions[i]!);
169+
if (!isValidSignature) {
170+
throw new Error("unable to verify transaction signatures");
171+
}
172+
173+
// 3. Broadcast the signed payload on devnet
174+
await solanaNetwork.broadcast(connection, signedTx);
175+
}
166176
}
167177

168178
process.exit(0);

packages/solana/src/__tests__/index-test.ts

+80-63
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { test, expect, describe } from "@jest/globals";
22
import { TurnkeySigner } from "../";
3-
import { TurnkeyClient } from "@turnkey/http";
3+
import { TurnkeyClient, assertNonNull } from "@turnkey/http";
44
import { ApiKeyStamper } from "@turnkey/api-key-stamper";
55
import { Turnkey } from "@turnkey/sdk-server";
66
import {
@@ -161,38 +161,47 @@ describe("TurnkeySigner", () => {
161161
transactions.push(transferTransaction);
162162
}
163163

164-
const signedTransactions =
165-
await signerConfig.signer.signAllTransactions(
164+
const rawResult = await signerConfig.signer.signAllTransactions([
165+
{
166+
signWith: turnkeySolAddress,
166167
transactions,
167-
turnkeySolAddress
168-
);
169-
expect(signedTransactions.length).toBe(numTxs);
168+
},
169+
]);
170170

171-
for (let i = 0; i < signedTransactions.length; i++) {
172-
const tx = signedTransactions[i] as Transaction;
171+
const result = assertNonNull(rawResult);
172+
expect(result.length).toBe(1);
173+
expect(result[0]!.signWith).toBe(turnkeySolAddress);
174+
expect(result[0]!.transactions.length).toBe(numTxs);
173175

174-
// Verify the signature itself
175-
const isValidSignature = nacl.sign.detached.verify(
176-
tx.serializeMessage(),
177-
tx.signature as Uint8Array,
178-
bs58.decode(turnkeySolAddress)
179-
);
180-
expect(isValidSignature).toBeTruthy();
176+
for (let i = 0; i < result.length; i++) {
177+
const txs = result[i]!.transactions;
178+
179+
for (const t of txs) {
180+
const tx = t as Transaction;
181+
182+
// Verify the signature itself
183+
const isValidSignature = nacl.sign.detached.verify(
184+
tx.serializeMessage(),
185+
tx.signature as Uint8Array,
186+
bs58.decode(turnkeySolAddress)
187+
);
188+
expect(isValidSignature).toBeTruthy();
181189

182-
// Ensure it's a simple, native transfer
183-
expect(tx.instructions.length).toEqual(1);
190+
// Ensure it's a simple, native transfer
191+
expect(tx.instructions.length).toEqual(1);
184192

185-
const programId = tx.instructions[0]!.programId!;
186-
const data = tx.instructions[0]!.data!;
193+
const programId = tx.instructions[0]!.programId!;
194+
const data = tx.instructions[0]!.data!;
187195

188-
expect(programId).toEqual(SystemProgram.programId);
189-
expect(data[0]).toEqual(2);
196+
expect(programId).toEqual(SystemProgram.programId);
197+
expect(data[0]).toEqual(2);
190198

191-
// Convert raw data to lamports, then to whole SOL units
192-
const amountLamportsBigInt = Buffer.from(data).readBigUInt64LE(4);
193-
const amountLamports = Number(amountLamportsBigInt);
199+
// Convert raw data to lamports, then to whole SOL units
200+
const amountLamportsBigInt = Buffer.from(data).readBigUInt64LE(4);
201+
const amountLamports = Number(amountLamportsBigInt);
194202

195-
expect(amounts[i]).toEqual(amountLamports);
203+
expect(amounts[i]).toEqual(amountLamports);
204+
}
196205
}
197206
});
198207

@@ -232,45 +241,53 @@ describe("TurnkeySigner", () => {
232241
transactions.push(transaction);
233242
}
234243

235-
const signedTransactions =
236-
await signerConfig.signer.signAllTransactions(
244+
const rawResult = await signerConfig.signer.signAllTransactions([
245+
{
246+
signWith: turnkeySolAddress,
237247
transactions,
238-
turnkeySolAddress
239-
);
240-
expect(signedTransactions.length).toBe(numTxs);
241-
242-
for (let i = 0; i < signedTransactions.length; i++) {
243-
const tx = signedTransactions[i] as VersionedTransaction;
244-
245-
// After signing the version transaction, the default signature is replaced with the new one
246-
expect(tx.signatures.length).toBe(1);
247-
expect(tx.signatures[0]).not.toEqual(DEFAULT_SIGNATURE);
248-
249-
// Verify the signature itself
250-
const isValidSignature = nacl.sign.detached.verify(
251-
tx.message.serialize(),
252-
tx.signatures[0] as Uint8Array,
253-
bs58.decode(turnkeySolAddress)
254-
);
255-
expect(isValidSignature).toBeTruthy();
256-
257-
// Ensure it's a simple, native transfer
258-
expect(tx.message.compiledInstructions.length).toEqual(1);
259-
260-
const programIdIndex =
261-
tx.message.compiledInstructions[0]!.programIdIndex!;
262-
const keys = tx.message.getAccountKeys();
263-
const programId = keys.staticAccountKeys[programIdIndex];
264-
const data = tx.message.compiledInstructions[0]!.data!;
265-
266-
expect(programId).toEqual(SystemProgram.programId);
267-
expect(data[0]).toEqual(2);
268-
269-
// Convert raw data to lamports, then to whole SOL units
270-
const amountLamportsBigInt = Buffer.from(data).readBigUInt64LE(4);
271-
const amountLamports = Number(amountLamportsBigInt);
272-
273-
expect(amounts[i]).toEqual(amountLamports);
248+
},
249+
]);
250+
const result = assertNonNull(rawResult);
251+
expect(result.length).toBe(1);
252+
expect(result[0]!.signWith).toBe(turnkeySolAddress);
253+
expect(result[0]!.transactions.length).toBe(numTxs);
254+
255+
for (let i = 0; i < result.length; i++) {
256+
const txs = result[i]!.transactions;
257+
258+
for (const t of txs) {
259+
const tx = t as VersionedTransaction;
260+
261+
// Verify the signature itself
262+
const isValidSignature = nacl.sign.detached.verify(
263+
tx.message.serialize(),
264+
tx.signatures[0] as Uint8Array,
265+
bs58.decode(turnkeySolAddress)
266+
);
267+
expect(isValidSignature).toBeTruthy();
268+
269+
// After signing the versioned transaction, the default signature is replaced with the new one
270+
expect(tx.signatures.length).toBe(1);
271+
expect(tx.signatures[0]).not.toEqual(DEFAULT_SIGNATURE);
272+
273+
// Ensure it's a simple, native transfer
274+
expect(tx.message.compiledInstructions.length).toEqual(1);
275+
276+
const programIdIndex =
277+
tx.message.compiledInstructions[0]!.programIdIndex!;
278+
const keys = tx.message.getAccountKeys();
279+
const programId = keys.staticAccountKeys[programIdIndex];
280+
const data = tx.message.compiledInstructions[0]!.data!;
281+
282+
expect(programId).toEqual(SystemProgram.programId);
283+
expect(data[0]).toEqual(2);
284+
285+
// Convert raw data to lamports, then to whole SOL units
286+
const amountLamportsBigInt = Buffer.from(data).readBigUInt64LE(4);
287+
const amountLamports = Number(amountLamportsBigInt);
288+
289+
expect(amounts[i]).toEqual(amountLamports);
290+
}
274291
}
275292
});
276293

0 commit comments

Comments
 (0)