|
1 | 1 | import { test, expect, describe } from "@jest/globals";
|
2 | 2 | import { TurnkeySigner } from "../";
|
3 |
| -import { TurnkeyClient } from "@turnkey/http"; |
| 3 | +import { TurnkeyClient, assertNonNull } from "@turnkey/http"; |
4 | 4 | import { ApiKeyStamper } from "@turnkey/api-key-stamper";
|
5 | 5 | import { Turnkey } from "@turnkey/sdk-server";
|
6 | 6 | import {
|
@@ -161,38 +161,47 @@ describe("TurnkeySigner", () => {
|
161 | 161 | transactions.push(transferTransaction);
|
162 | 162 | }
|
163 | 163 |
|
164 |
| - const signedTransactions = |
165 |
| - await signerConfig.signer.signAllTransactions( |
| 164 | + const rawResult = await signerConfig.signer.signAllTransactions([ |
| 165 | + { |
| 166 | + signWith: turnkeySolAddress, |
166 | 167 | transactions,
|
167 |
| - turnkeySolAddress |
168 |
| - ); |
169 |
| - expect(signedTransactions.length).toBe(numTxs); |
| 168 | + }, |
| 169 | + ]); |
170 | 170 |
|
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); |
173 | 175 |
|
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(); |
181 | 189 |
|
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); |
184 | 192 |
|
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!; |
187 | 195 |
|
188 |
| - expect(programId).toEqual(SystemProgram.programId); |
189 |
| - expect(data[0]).toEqual(2); |
| 196 | + expect(programId).toEqual(SystemProgram.programId); |
| 197 | + expect(data[0]).toEqual(2); |
190 | 198 |
|
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); |
194 | 202 |
|
195 |
| - expect(amounts[i]).toEqual(amountLamports); |
| 203 | + expect(amounts[i]).toEqual(amountLamports); |
| 204 | + } |
196 | 205 | }
|
197 | 206 | });
|
198 | 207 |
|
@@ -232,45 +241,53 @@ describe("TurnkeySigner", () => {
|
232 | 241 | transactions.push(transaction);
|
233 | 242 | }
|
234 | 243 |
|
235 |
| - const signedTransactions = |
236 |
| - await signerConfig.signer.signAllTransactions( |
| 244 | + const rawResult = await signerConfig.signer.signAllTransactions([ |
| 245 | + { |
| 246 | + signWith: turnkeySolAddress, |
237 | 247 | 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 | + } |
274 | 291 | }
|
275 | 292 | });
|
276 | 293 |
|
|
0 commit comments