Skip to content

Commit 409df8e

Browse files
committed
end to end tests
1 parent cfd87d0 commit 409df8e

File tree

8 files changed

+1925
-1035
lines changed

8 files changed

+1925
-1035
lines changed

packages/vault-sdk/package.json

Lines changed: 71 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,73 @@
11
{
2-
"author": "thirdweb eng <[email protected]>",
3-
"bugs": {
4-
"url": "https://github.com/thirdweb-dev/js/issues"
5-
},
6-
"dependencies": {
7-
"@noble/ciphers": "^1.2.1",
8-
"@noble/curves": "1.8.2",
9-
"@noble/hashes": "1.7.2",
10-
"@solana/addresses": "^3.0.0",
11-
"@solana/keys": "^3.0.0",
12-
"@solana/transactions": "^3.0.0",
13-
"@solana/rpc": "^3.0.0",
14-
"abitype": "1.0.8",
15-
"jose": "6.0.11"
16-
},
17-
"devDependencies": {
18-
"@biomejs/biome": "2.0.6",
19-
"tsx": "^4.0.0",
20-
"rimraf": "6.0.1"
21-
},
22-
"engines": {
23-
"node": ">=18"
24-
},
25-
"exports": {
26-
".": {
27-
"types": "./dist/types/exports/thirdweb.d.ts",
28-
"import": "./dist/esm/exports/thirdweb.js",
29-
"default": "./dist/cjs/exports/thirdweb.js"
30-
},
31-
"./package.json": "./package.json"
32-
},
33-
"files": [
34-
"dist/*",
35-
"src/*"
36-
],
37-
"license": "Apache-2.0",
38-
"main": "./dist/cjs/exports/thirdweb.js",
39-
"module": "./dist/esm/exports/thirdweb.js",
40-
"name": "@thirdweb-dev/vault-sdk",
41-
"peerDependencies": {
42-
"typescript": ">=5.0.4"
43-
},
44-
"peerDependenciesMeta": {
45-
"typescript": {
46-
"optional": true
47-
}
48-
},
49-
"repository": {
50-
"type": "git",
51-
"url": "git+https://github.com/thirdweb-dev/js.git#main"
52-
},
53-
"scripts": {
54-
"build": "pnpm clean && pnpm build:cjs && pnpm build:esm && pnpm build:types",
55-
"build:cjs": "tsc --project ./tsconfig.build.json --module commonjs --outDir ./dist/cjs --verbatimModuleSyntax false && printf '{\"type\":\"commonjs\"}' > ./dist/cjs/package.json",
56-
"build:esm": "tsc --project ./tsconfig.build.json --module es2020 --outDir ./dist/esm && printf '{\"type\": \"module\",\"sideEffects\":false}' > ./dist/esm/package.json",
57-
"build:types": "tsc --project ./tsconfig.build.json --module esnext --declarationDir ./dist/types --emitDeclarationOnly --declaration --declarationMap",
58-
"clean": "rimraf dist",
59-
"test:solana": "tsx test-solana.ts",
60-
"dev": "tsc --project ./tsconfig.build.json --module es2020 --outDir ./dist/esm --watch",
61-
"fix": "biome check ./src --fix",
62-
"format": "biome format ./src --write",
63-
"lint": "biome check ./src"
64-
},
65-
"type": "module",
66-
"types": "./dist/types/exports/thirdweb.d.ts",
67-
"typings": "./dist/types/exports/thirdweb.d.ts",
68-
"version": "0.0.3"
2+
"author": "thirdweb eng <[email protected]>",
3+
"bugs": {
4+
"url": "https://github.com/thirdweb-dev/js/issues"
5+
},
6+
"dependencies": {
7+
"@noble/ciphers": "^1.2.1",
8+
"@noble/curves": "1.8.2",
9+
"@noble/hashes": "1.7.2",
10+
"@solana/addresses": "^3.0.0",
11+
"@solana/keys": "^3.0.0",
12+
"@solana/rpc": "^3.0.0",
13+
"@solana/transactions": "^3.0.0",
14+
"abitype": "1.0.8",
15+
"@solana/kit": "^4.0.0",
16+
"jose": "6.0.11"
17+
},
18+
"devDependencies": {
19+
"@biomejs/biome": "2.0.6",
20+
"@solana-program/system": "^0.8.1",
21+
"bs58": "^6.0.0",
22+
"rimraf": "6.0.1",
23+
"tsx": "^4.0.0",
24+
"vitest": "3.2.4"
25+
},
26+
"engines": {
27+
"node": ">=18"
28+
},
29+
"exports": {
30+
".": {
31+
"types": "./dist/types/exports/thirdweb.d.ts",
32+
"import": "./dist/esm/exports/thirdweb.js",
33+
"default": "./dist/cjs/exports/thirdweb.js"
34+
},
35+
"./package.json": "./package.json"
36+
},
37+
"files": [
38+
"dist/*",
39+
"src/*"
40+
],
41+
"license": "Apache-2.0",
42+
"main": "./dist/cjs/exports/thirdweb.js",
43+
"module": "./dist/esm/exports/thirdweb.js",
44+
"name": "@thirdweb-dev/vault-sdk",
45+
"peerDependencies": {
46+
"typescript": ">=5.0.4"
47+
},
48+
"peerDependenciesMeta": {
49+
"typescript": {
50+
"optional": true
51+
}
52+
},
53+
"repository": {
54+
"type": "git",
55+
"url": "git+https://github.com/thirdweb-dev/js.git#main"
56+
},
57+
"scripts": {
58+
"build": "pnpm clean && pnpm build:cjs && pnpm build:esm && pnpm build:types",
59+
"build:cjs": "tsc --project ./tsconfig.build.json --module commonjs --outDir ./dist/cjs --verbatimModuleSyntax false && printf '{\"type\":\"commonjs\"}' > ./dist/cjs/package.json",
60+
"build:esm": "tsc --project ./tsconfig.build.json --module es2020 --outDir ./dist/esm && printf '{\"type\": \"module\",\"sideEffects\":false}' > ./dist/esm/package.json",
61+
"build:types": "tsc --project ./tsconfig.build.json --module esnext --declarationDir ./dist/types --emitDeclarationOnly --declaration --declarationMap",
62+
"clean": "rimraf dist",
63+
"test": "vitest",
64+
"dev": "tsc --project ./tsconfig.build.json --module es2020 --outDir ./dist/esm --watch",
65+
"fix": "biome check ./src --fix",
66+
"format": "biome format ./src --write",
67+
"lint": "biome check ./src"
68+
},
69+
"type": "module",
70+
"types": "./dist/types/exports/thirdweb.d.ts",
71+
"typings": "./dist/types/exports/thirdweb.d.ts",
72+
"version": "0.0.3"
6973
}

packages/vault-sdk/src/sdk.ts

Lines changed: 44 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,16 @@ import { x25519 } from "@noble/curves/ed25519";
33
import { hkdf } from "@noble/hashes/hkdf";
44
import { sha256 } from "@noble/hashes/sha256";
55
import { bytesToHex, hexToBytes, randomBytes } from "@noble/hashes/utils";
6-
import type { TypedData } from "abitype";
7-
import * as jose from "jose";
8-
9-
// Base58 encoding for Solana public keys
10-
const BASE58_ALPHABET =
11-
"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
12-
13-
function base58Encode(bytes: number[] | Uint8Array): string {
14-
const byteArray = Array.isArray(bytes) ? new Uint8Array(bytes) : bytes;
156

16-
if (byteArray.length === 0) return "";
17-
18-
// Convert to big integer
19-
let num = 0n;
20-
for (const byte of byteArray) {
21-
num = num * 256n + BigInt(byte);
22-
}
23-
24-
// Convert to base58
25-
let result = "";
26-
while (num > 0n) {
27-
const remainder = Number(num % 58n);
28-
result = BASE58_ALPHABET[remainder] + result;
29-
num = num / 58n;
30-
}
31-
32-
// Add leading zeros
33-
for (const byte of byteArray) {
34-
if (byte === 0) {
35-
result = BASE58_ALPHABET[0] + result;
36-
} else {
37-
break;
38-
}
39-
}
40-
41-
return result;
42-
}
43-
44-
// Helper function to process Solana account data
45-
function _processSolanaAccountData(accountData: any): any {
46-
if (!accountData) return accountData;
47-
48-
// If we have a pubkey byte array, convert it to base58 string
49-
if (accountData.pubkey && Array.isArray(accountData.pubkey)) {
50-
accountData.publicKey = base58Encode(accountData.pubkey);
51-
accountData.address = accountData.publicKey; // Use the same value for address
52-
}
53-
54-
return accountData;
55-
}
7+
import {
8+
address,
9+
getBase58Encoder,
10+
getTransactionDecoder,
11+
getTransactionEncoder,
12+
} from "@solana/kit";
5613

14+
import type { TypedData } from "abitype";
15+
import * as jose from "jose";
5716
import type {
5817
CheckedSignTypedDataPayload,
5918
CreateAccessTokenPayload,
@@ -540,6 +499,42 @@ export function signSolanaTransaction({
540499
});
541500
}
542501

502+
/**
503+
* Reconstruct a signed solana transaction from the vault signature using @solana/kit
504+
*/
505+
export function reconstructSolanaSignedTransaction(
506+
base64Transaction: string,
507+
base58Signature: string,
508+
signerPubkey: string,
509+
): Uint8Array {
510+
// Decode the base64 transaction into bytes
511+
const base64TransactionBytes = new Uint8Array(
512+
Buffer.from(base64Transaction, "base64"),
513+
);
514+
// Decode the transaction to get its structure
515+
const transactionDecoder = getTransactionDecoder();
516+
const transaction = transactionDecoder.decode(base64TransactionBytes);
517+
518+
// Decode the base58 signature to bytes
519+
const base58Encoder = getBase58Encoder();
520+
const signatureBytes = base58Encoder.encode(base58Signature);
521+
522+
// Add the signature to the transaction
523+
const signedTransaction = {
524+
...transaction,
525+
signatures: {
526+
...transaction.signatures,
527+
[address(signerPubkey)]: signatureBytes,
528+
},
529+
};
530+
531+
// Re-encode the signed transaction
532+
const transactionEncoder = getTransactionEncoder();
533+
const signedTransactionBytes = transactionEncoder.encode(signedTransaction);
534+
535+
return new Uint8Array(signedTransactionBytes);
536+
}
537+
543538
export function signSolanaMessage({
544539
client,
545540
request: options,

0 commit comments

Comments
 (0)