@arkade-os/sdk 0.2.3 → 0.3.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. package/README.md +114 -43
  2. package/dist/cjs/adapters/asyncStorage.js +5 -0
  3. package/dist/cjs/adapters/fileSystem.js +5 -0
  4. package/dist/cjs/adapters/indexedDB.js +5 -0
  5. package/dist/cjs/adapters/localStorage.js +5 -0
  6. package/dist/cjs/bip322/index.js +2 -2
  7. package/dist/cjs/identity/index.js +15 -0
  8. package/dist/cjs/identity/singleKey.js +20 -1
  9. package/dist/cjs/index.js +5 -3
  10. package/dist/cjs/musig2/keys.js +6 -6
  11. package/dist/cjs/musig2/sign.js +5 -5
  12. package/dist/cjs/repositories/contractRepository.js +130 -0
  13. package/dist/cjs/repositories/index.js +18 -0
  14. package/dist/cjs/repositories/walletRepository.js +136 -0
  15. package/dist/cjs/storage/asyncStorage.js +47 -0
  16. package/dist/cjs/storage/fileSystem.js +138 -0
  17. package/dist/cjs/storage/inMemory.js +21 -0
  18. package/dist/cjs/storage/indexedDB.js +97 -0
  19. package/dist/cjs/storage/localStorage.js +48 -0
  20. package/dist/cjs/tree/signingSession.js +4 -4
  21. package/dist/cjs/wallet/onchain.js +12 -6
  22. package/dist/cjs/wallet/serviceWorker/request.js +4 -14
  23. package/dist/cjs/wallet/serviceWorker/response.js +0 -13
  24. package/dist/cjs/wallet/serviceWorker/wallet.js +124 -130
  25. package/dist/cjs/wallet/serviceWorker/worker.js +84 -53
  26. package/dist/cjs/wallet/wallet.js +21 -4
  27. package/dist/esm/adapters/asyncStorage.js +1 -0
  28. package/dist/esm/adapters/fileSystem.js +1 -0
  29. package/dist/esm/adapters/indexedDB.js +1 -0
  30. package/dist/esm/adapters/localStorage.js +1 -0
  31. package/dist/esm/bip322/index.js +1 -1
  32. package/dist/esm/identity/index.js +1 -1
  33. package/dist/esm/identity/singleKey.js +21 -2
  34. package/dist/esm/index.js +4 -3
  35. package/dist/esm/musig2/keys.js +6 -6
  36. package/dist/esm/musig2/sign.js +4 -4
  37. package/dist/esm/repositories/contractRepository.js +126 -0
  38. package/dist/esm/repositories/index.js +2 -0
  39. package/dist/esm/repositories/walletRepository.js +132 -0
  40. package/dist/esm/storage/asyncStorage.js +43 -0
  41. package/dist/esm/storage/fileSystem.js +101 -0
  42. package/dist/esm/storage/inMemory.js +17 -0
  43. package/dist/esm/storage/indexedDB.js +93 -0
  44. package/dist/esm/storage/localStorage.js +44 -0
  45. package/dist/esm/tree/signingSession.js +1 -1
  46. package/dist/esm/wallet/onchain.js +12 -6
  47. package/dist/esm/wallet/serviceWorker/request.js +4 -14
  48. package/dist/esm/wallet/serviceWorker/response.js +0 -13
  49. package/dist/esm/wallet/serviceWorker/wallet.js +125 -131
  50. package/dist/esm/wallet/serviceWorker/worker.js +85 -54
  51. package/dist/esm/wallet/wallet.js +21 -4
  52. package/dist/types/adapters/asyncStorage.d.ts +2 -0
  53. package/dist/types/adapters/fileSystem.d.ts +2 -0
  54. package/dist/types/adapters/indexedDB.d.ts +2 -0
  55. package/dist/types/adapters/localStorage.d.ts +2 -0
  56. package/dist/types/identity/index.d.ts +3 -1
  57. package/dist/types/identity/singleKey.d.ts +12 -1
  58. package/dist/types/index.d.ts +4 -4
  59. package/dist/types/repositories/contractRepository.d.ts +20 -0
  60. package/dist/types/repositories/index.d.ts +2 -0
  61. package/dist/types/repositories/walletRepository.d.ts +38 -0
  62. package/dist/types/storage/asyncStorage.d.ts +9 -0
  63. package/dist/types/storage/fileSystem.d.ts +11 -0
  64. package/dist/types/storage/inMemory.d.ts +8 -0
  65. package/dist/types/storage/index.d.ts +6 -0
  66. package/dist/types/storage/indexedDB.d.ts +12 -0
  67. package/dist/types/storage/localStorage.d.ts +8 -0
  68. package/dist/types/wallet/index.d.ts +3 -0
  69. package/dist/types/wallet/onchain.d.ts +3 -2
  70. package/dist/types/wallet/serviceWorker/request.d.ts +1 -7
  71. package/dist/types/wallet/serviceWorker/response.d.ts +1 -8
  72. package/dist/types/wallet/serviceWorker/wallet.d.ts +67 -21
  73. package/dist/types/wallet/serviceWorker/worker.d.ts +16 -4
  74. package/dist/types/wallet/wallet.d.ts +4 -0
  75. package/package.json +38 -14
  76. package/dist/cjs/wallet/serviceWorker/db/vtxo/idb.js +0 -185
  77. package/dist/esm/wallet/serviceWorker/db/vtxo/idb.js +0 -181
  78. package/dist/types/wallet/serviceWorker/db/vtxo/idb.d.ts +0 -20
  79. package/dist/types/wallet/serviceWorker/db/vtxo/index.d.ts +0 -14
  80. /package/dist/cjs/{wallet/serviceWorker/db/vtxo → storage}/index.js +0 -0
  81. /package/dist/esm/{wallet/serviceWorker/db/vtxo → storage}/index.js +0 -0
@@ -0,0 +1,136 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WalletRepositoryImpl = void 0;
4
+ class WalletRepositoryImpl {
5
+ constructor(storage) {
6
+ this.storage = storage;
7
+ this.cache = {
8
+ vtxos: new Map(),
9
+ transactions: new Map(),
10
+ walletState: null,
11
+ initialized: new Set(),
12
+ };
13
+ }
14
+ async getVtxos(address) {
15
+ const cacheKey = `vtxos:${address}`;
16
+ if (this.cache.vtxos.has(address)) {
17
+ return this.cache.vtxos.get(address);
18
+ }
19
+ const stored = await this.storage.getItem(cacheKey);
20
+ if (!stored) {
21
+ this.cache.vtxos.set(address, []);
22
+ return [];
23
+ }
24
+ try {
25
+ const vtxos = JSON.parse(stored);
26
+ this.cache.vtxos.set(address, vtxos);
27
+ return vtxos.slice();
28
+ }
29
+ catch (error) {
30
+ console.error(`Failed to parse VTXOs for address ${address}:`, error);
31
+ this.cache.vtxos.set(address, []);
32
+ return [];
33
+ }
34
+ }
35
+ async saveVtxo(address, vtxo) {
36
+ const vtxos = await this.getVtxos(address);
37
+ const existing = vtxos.findIndex((v) => v.txid === vtxo.txid && v.vout === vtxo.vout);
38
+ if (existing !== -1) {
39
+ vtxos[existing] = vtxo;
40
+ }
41
+ else {
42
+ vtxos.push(vtxo);
43
+ }
44
+ this.cache.vtxos.set(address, vtxos);
45
+ await this.storage.setItem(`vtxos:${address}`, JSON.stringify(vtxos));
46
+ }
47
+ async saveVtxos(address, vtxos) {
48
+ const storedVtxos = await this.getVtxos(address);
49
+ for (const vtxo of vtxos) {
50
+ const existing = storedVtxos.findIndex((v) => v.txid === vtxo.txid && v.vout === vtxo.vout);
51
+ if (existing !== -1) {
52
+ storedVtxos[existing] = vtxo;
53
+ }
54
+ else {
55
+ storedVtxos.push(vtxo);
56
+ }
57
+ }
58
+ this.cache.vtxos.set(address, storedVtxos);
59
+ await this.storage.setItem(`vtxos:${address}`, JSON.stringify(storedVtxos));
60
+ }
61
+ async removeVtxo(address, vtxoId) {
62
+ const vtxos = await this.getVtxos(address);
63
+ const [txid, vout] = vtxoId.split(":");
64
+ const filtered = vtxos.filter((v) => !(v.txid === txid && v.vout === parseInt(vout)));
65
+ this.cache.vtxos.set(address, filtered);
66
+ await this.storage.setItem(`vtxos:${address}`, JSON.stringify(filtered));
67
+ }
68
+ async clearVtxos(address) {
69
+ this.cache.vtxos.set(address, []);
70
+ await this.storage.removeItem(`vtxos:${address}`);
71
+ }
72
+ async getTransactionHistory(address) {
73
+ const cacheKey = `tx:${address}`;
74
+ if (this.cache.transactions.has(address)) {
75
+ return this.cache.transactions.get(address);
76
+ }
77
+ const stored = await this.storage.getItem(cacheKey);
78
+ if (!stored) {
79
+ this.cache.transactions.set(address, []);
80
+ return [];
81
+ }
82
+ try {
83
+ const transactions = JSON.parse(stored);
84
+ this.cache.transactions.set(address, transactions);
85
+ return transactions.slice();
86
+ }
87
+ catch (error) {
88
+ console.error(`Failed to parse transactions for address ${address}:`, error);
89
+ this.cache.transactions.set(address, []);
90
+ return [];
91
+ }
92
+ }
93
+ async saveTransaction(address, tx) {
94
+ const transactions = await this.getTransactionHistory(address);
95
+ const existing = transactions.findIndex((t) => t.id === tx.id);
96
+ if (existing !== -1) {
97
+ transactions[existing] = tx;
98
+ }
99
+ else {
100
+ transactions.push(tx);
101
+ // Sort by timestamp descending
102
+ transactions.sort((a, b) => b.timestamp - a.timestamp);
103
+ }
104
+ this.cache.transactions.set(address, transactions);
105
+ await this.storage.setItem(`tx:${address}`, JSON.stringify(transactions));
106
+ }
107
+ async getWalletState() {
108
+ if (this.cache.walletState !== null ||
109
+ this.cache.initialized.has("walletState")) {
110
+ return this.cache.walletState;
111
+ }
112
+ const stored = await this.storage.getItem("wallet:state");
113
+ if (!stored) {
114
+ this.cache.walletState = null;
115
+ this.cache.initialized.add("walletState");
116
+ return null;
117
+ }
118
+ try {
119
+ const state = JSON.parse(stored);
120
+ this.cache.walletState = state;
121
+ this.cache.initialized.add("walletState");
122
+ return state;
123
+ }
124
+ catch (error) {
125
+ console.error("Failed to parse wallet state:", error);
126
+ this.cache.walletState = null;
127
+ this.cache.initialized.add("walletState");
128
+ return null;
129
+ }
130
+ }
131
+ async saveWalletState(state) {
132
+ this.cache.walletState = state;
133
+ await this.storage.setItem("wallet:state", JSON.stringify(state));
134
+ }
135
+ }
136
+ exports.WalletRepositoryImpl = WalletRepositoryImpl;
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AsyncStorageAdapter = void 0;
4
+ // Note: This requires @react-native-async-storage/async-storage to be installed
5
+ class AsyncStorageAdapter {
6
+ constructor() {
7
+ try {
8
+ // Dynamic import to avoid errors in non-React Native environments
9
+ this.AsyncStorage =
10
+ require("@react-native-async-storage/async-storage").default;
11
+ }
12
+ catch (error) {
13
+ throw new Error("AsyncStorage is not available. Make sure @react-native-async-storage/async-storage is installed in React Native environment.");
14
+ }
15
+ }
16
+ async getItem(key) {
17
+ return await this.AsyncStorage.getItem(key);
18
+ }
19
+ async setItem(key, value) {
20
+ try {
21
+ await this.AsyncStorage.setItem(key, value);
22
+ }
23
+ catch (error) {
24
+ console.error(`Failed to set item for key ${key}:`, error);
25
+ throw error;
26
+ }
27
+ }
28
+ async removeItem(key) {
29
+ try {
30
+ await this.AsyncStorage.removeItem(key);
31
+ }
32
+ catch (error) {
33
+ console.error(`Failed to remove item for key ${key}:`, error);
34
+ throw error;
35
+ }
36
+ }
37
+ async clear() {
38
+ try {
39
+ await this.AsyncStorage.clear();
40
+ }
41
+ catch (error) {
42
+ console.error("Failed to clear AsyncStorage:", error);
43
+ throw error;
44
+ }
45
+ }
46
+ }
47
+ exports.AsyncStorageAdapter = AsyncStorageAdapter;
@@ -0,0 +1,138 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.FileSystemStorageAdapter = void 0;
37
+ const fs = __importStar(require("fs/promises"));
38
+ const path = __importStar(require("path"));
39
+ class FileSystemStorageAdapter {
40
+ constructor(dirPath) {
41
+ // Normalize and resolve the storage base path once
42
+ this.basePath = path.resolve(dirPath).replace(/[/\\]+$/, "");
43
+ }
44
+ validateAndGetFilePath(key) {
45
+ // Reject dangerous keys
46
+ if (key === "." || key === "..") {
47
+ throw new Error("Invalid key: '.' and '..' are not allowed");
48
+ }
49
+ // Check for null bytes
50
+ if (key.includes("\0")) {
51
+ throw new Error("Invalid key: null bytes are not allowed");
52
+ }
53
+ // Check for path traversal attempts before normalization
54
+ if (key.includes("..")) {
55
+ throw new Error("Invalid key: directory traversal is not allowed");
56
+ }
57
+ // Check for reserved Windows names (case-insensitive)
58
+ const reservedNames = /^(CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])$/i;
59
+ const keyWithoutExt = key.split(".")[0];
60
+ if (reservedNames.test(keyWithoutExt)) {
61
+ throw new Error(`Invalid key: '${key}' uses a reserved Windows name`);
62
+ }
63
+ // Check for trailing spaces or dots
64
+ if (key.endsWith(" ") || key.endsWith(".")) {
65
+ throw new Error("Invalid key: trailing spaces or dots are not allowed");
66
+ }
67
+ // Normalize path separators and sanitize key
68
+ const normalizedKey = key
69
+ .replace(/[/\\]/g, "_")
70
+ .replace(/[^a-zA-Z0-9._-]/g, "_");
71
+ // Resolve the full path and check for directory traversal
72
+ const resolved = path.resolve(this.basePath, normalizedKey);
73
+ const relative = path.relative(this.basePath, resolved);
74
+ // Reject if trying to escape the base directory
75
+ if (relative.startsWith("..") || relative.includes(path.sep + "..")) {
76
+ throw new Error("Invalid key: directory traversal is not allowed");
77
+ }
78
+ return resolved;
79
+ }
80
+ async ensureDirectory() {
81
+ try {
82
+ await fs.access(this.basePath);
83
+ }
84
+ catch {
85
+ await fs.mkdir(this.basePath, { recursive: true });
86
+ }
87
+ }
88
+ async getItem(key) {
89
+ try {
90
+ const filePath = this.validateAndGetFilePath(key);
91
+ const data = await fs.readFile(filePath, "utf-8");
92
+ return data;
93
+ }
94
+ catch (error) {
95
+ if (error.code === "ENOENT") {
96
+ return null;
97
+ }
98
+ console.error(`Failed to read file for key ${key}:`, error);
99
+ return null;
100
+ }
101
+ }
102
+ async setItem(key, value) {
103
+ try {
104
+ await this.ensureDirectory();
105
+ const filePath = this.validateAndGetFilePath(key);
106
+ await fs.writeFile(filePath, value, "utf-8");
107
+ }
108
+ catch (error) {
109
+ console.error(`Failed to write file for key ${key}:`, error);
110
+ throw error;
111
+ }
112
+ }
113
+ async removeItem(key) {
114
+ try {
115
+ const filePath = this.validateAndGetFilePath(key);
116
+ await fs.unlink(filePath);
117
+ }
118
+ catch (error) {
119
+ if (error.code !== "ENOENT") {
120
+ console.error(`Failed to remove file for key ${key}:`, error);
121
+ }
122
+ }
123
+ }
124
+ async clear() {
125
+ try {
126
+ const entries = await fs.readdir(this.basePath);
127
+ await Promise.all(entries.map(async (entry) => {
128
+ const entryPath = path.join(this.basePath, entry);
129
+ // Use fs.rm with recursive option to handle both files and directories
130
+ await fs.rm(entryPath, { recursive: true, force: true });
131
+ }));
132
+ }
133
+ catch (error) {
134
+ console.error("Failed to clear storage directory:", error);
135
+ }
136
+ }
137
+ }
138
+ exports.FileSystemStorageAdapter = FileSystemStorageAdapter;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InMemoryStorageAdapter = void 0;
4
+ class InMemoryStorageAdapter {
5
+ constructor() {
6
+ this.store = new Map();
7
+ }
8
+ async getItem(key) {
9
+ return this.store.get(key) ?? null;
10
+ }
11
+ async setItem(key, value) {
12
+ this.store.set(key, value);
13
+ }
14
+ async removeItem(key) {
15
+ this.store.delete(key);
16
+ }
17
+ async clear() {
18
+ this.store.clear();
19
+ }
20
+ }
21
+ exports.InMemoryStorageAdapter = InMemoryStorageAdapter;
@@ -0,0 +1,97 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.IndexedDBStorageAdapter = void 0;
4
+ class IndexedDBStorageAdapter {
5
+ constructor(dbName, version = 1) {
6
+ this.db = null;
7
+ this.dbName = dbName;
8
+ this.version = version;
9
+ }
10
+ async getDB() {
11
+ if (this.db)
12
+ return this.db;
13
+ const globalObject = typeof window === "undefined" ? self : window;
14
+ if (!(globalObject && "indexedDB" in globalObject)) {
15
+ throw new Error("IndexedDB is not available in this environment");
16
+ }
17
+ return new Promise((resolve, reject) => {
18
+ const request = globalObject.indexedDB.open(this.dbName, this.version);
19
+ request.onerror = () => reject(request.error);
20
+ request.onsuccess = () => {
21
+ this.db = request.result;
22
+ resolve(this.db);
23
+ };
24
+ request.onupgradeneeded = () => {
25
+ const db = request.result;
26
+ if (!db.objectStoreNames.contains("storage")) {
27
+ db.createObjectStore("storage");
28
+ }
29
+ };
30
+ });
31
+ }
32
+ async getItem(key) {
33
+ try {
34
+ const db = await this.getDB();
35
+ return new Promise((resolve, reject) => {
36
+ const transaction = db.transaction(["storage"], "readonly");
37
+ const store = transaction.objectStore("storage");
38
+ const request = store.get(key);
39
+ request.onerror = () => reject(request.error);
40
+ request.onsuccess = () => {
41
+ resolve(request.result || null);
42
+ };
43
+ });
44
+ }
45
+ catch (error) {
46
+ console.error(`Failed to get item for key ${key}:`, error);
47
+ return null;
48
+ }
49
+ }
50
+ async setItem(key, value) {
51
+ try {
52
+ const db = await this.getDB();
53
+ return new Promise((resolve, reject) => {
54
+ const transaction = db.transaction(["storage"], "readwrite");
55
+ const store = transaction.objectStore("storage");
56
+ const request = store.put(value, key);
57
+ request.onerror = () => reject(request.error);
58
+ request.onsuccess = () => resolve();
59
+ });
60
+ }
61
+ catch (error) {
62
+ console.error(`Failed to set item for key ${key}:`, error);
63
+ throw error;
64
+ }
65
+ }
66
+ async removeItem(key) {
67
+ try {
68
+ const db = await this.getDB();
69
+ return new Promise((resolve, reject) => {
70
+ const transaction = db.transaction(["storage"], "readwrite");
71
+ const store = transaction.objectStore("storage");
72
+ const request = store.delete(key);
73
+ request.onerror = () => reject(request.error);
74
+ request.onsuccess = () => resolve();
75
+ });
76
+ }
77
+ catch (error) {
78
+ console.error(`Failed to remove item for key ${key}:`, error);
79
+ }
80
+ }
81
+ async clear() {
82
+ try {
83
+ const db = await this.getDB();
84
+ return new Promise((resolve, reject) => {
85
+ const transaction = db.transaction(["storage"], "readwrite");
86
+ const store = transaction.objectStore("storage");
87
+ const request = store.clear();
88
+ request.onerror = () => reject(request.error);
89
+ request.onsuccess = () => resolve();
90
+ });
91
+ }
92
+ catch (error) {
93
+ console.error("Failed to clear storage:", error);
94
+ }
95
+ }
96
+ }
97
+ exports.IndexedDBStorageAdapter = IndexedDBStorageAdapter;
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LocalStorageAdapter = void 0;
4
+ class LocalStorageAdapter {
5
+ getSafeLocalStorage() {
6
+ try {
7
+ if (typeof window === "undefined" || !window.localStorage) {
8
+ return null;
9
+ }
10
+ // Test access to ensure localStorage is actually available
11
+ window.localStorage.length;
12
+ return window.localStorage;
13
+ }
14
+ catch {
15
+ // localStorage may throw in some environments (e.g., private browsing, disabled storage)
16
+ return null;
17
+ }
18
+ }
19
+ async getItem(key) {
20
+ const localStorage = this.getSafeLocalStorage();
21
+ if (!localStorage) {
22
+ throw new Error("localStorage is not available in this environment");
23
+ }
24
+ return localStorage.getItem(key);
25
+ }
26
+ async setItem(key, value) {
27
+ const localStorage = this.getSafeLocalStorage();
28
+ if (!localStorage) {
29
+ throw new Error("localStorage is not available in this environment");
30
+ }
31
+ localStorage.setItem(key, value);
32
+ }
33
+ async removeItem(key) {
34
+ const localStorage = this.getSafeLocalStorage();
35
+ if (!localStorage) {
36
+ throw new Error("localStorage is not available in this environment");
37
+ }
38
+ localStorage.removeItem(key);
39
+ }
40
+ async clear() {
41
+ const localStorage = this.getSafeLocalStorage();
42
+ if (!localStorage) {
43
+ throw new Error("localStorage is not available in this environment");
44
+ }
45
+ localStorage.clear();
46
+ }
47
+ }
48
+ exports.LocalStorageAdapter = LocalStorageAdapter;
@@ -38,7 +38,7 @@ exports.validateTreeSigs = validateTreeSigs;
38
38
  const musig2 = __importStar(require("../musig2"));
39
39
  const btc_signer_1 = require("@scure/btc-signer");
40
40
  const base_1 = require("@scure/base");
41
- const secp256k1_1 = require("@noble/curves/secp256k1");
41
+ const secp256k1_js_1 = require("@noble/curves/secp256k1.js");
42
42
  const utils_1 = require("@scure/btc-signer/utils");
43
43
  const unknownFields_1 = require("../utils/unknownFields");
44
44
  exports.ErrMissingVtxoGraph = new Error("missing vtxo graph");
@@ -62,7 +62,7 @@ class TreeSignerSession {
62
62
  this.rootSharedOutputAmount = rootInputAmount;
63
63
  }
64
64
  getPublicKey() {
65
- return secp256k1_1.secp256k1.getPublicKey(this.secretKey);
65
+ return secp256k1_js_1.secp256k1.getPublicKey(this.secretKey);
66
66
  }
67
67
  getNonces() {
68
68
  if (!this.graph)
@@ -99,7 +99,7 @@ class TreeSignerSession {
99
99
  if (!this.graph)
100
100
  throw exports.ErrMissingVtxoGraph;
101
101
  const myNonces = new Map();
102
- const publicKey = secp256k1_1.secp256k1.getPublicKey(this.secretKey);
102
+ const publicKey = secp256k1_js_1.secp256k1.getPublicKey(this.secretKey);
103
103
  for (const g of this.graph) {
104
104
  const nonces = musig2.generateNonces(publicKey);
105
105
  myNonces.set(g.txid, nonces);
@@ -156,7 +156,7 @@ async function validateTreeSigs(finalAggregatedKey, sharedOutputAmount, vtxoTree
156
156
  const message = g.root.preimageWitnessV1(0, // always first input
157
157
  [prevout.script], btc_signer_1.SigHash.DEFAULT, [prevout.amount]);
158
158
  // Verify the signature
159
- const isValid = secp256k1_1.schnorr.verify(input.tapKeySig, message, finalAggregatedKey);
159
+ const isValid = secp256k1_js_1.schnorr.verify(input.tapKeySig, message, finalAggregatedKey);
160
160
  if (!isValid) {
161
161
  throw new Error("invalid signature");
162
162
  }
@@ -17,7 +17,7 @@ const txSizeEstimator_1 = require("../utils/txSizeEstimator");
17
17
  *
18
18
  * @example
19
19
  * ```typescript
20
- * const wallet = new OnchainWallet(identity, 'mainnet');
20
+ * const wallet = await OnchainWallet.create(identity, 'mainnet');
21
21
  * const balance = await wallet.getBalance();
22
22
  * const txid = await wallet.send({
23
23
  * address: 'bc1...',
@@ -26,15 +26,21 @@ const txSizeEstimator_1 = require("../utils/txSizeEstimator");
26
26
  * ```
27
27
  */
28
28
  class OnchainWallet {
29
- constructor(identity, network, provider) {
29
+ constructor(identity, network, onchainP2TR, provider) {
30
30
  this.identity = identity;
31
- const pubkey = identity.xOnlyPublicKey();
31
+ this.network = network;
32
+ this.onchainP2TR = onchainP2TR;
33
+ this.provider = provider;
34
+ }
35
+ static async create(identity, networkName, provider) {
36
+ const pubkey = await identity.xOnlyPublicKey();
32
37
  if (!pubkey) {
33
38
  throw new Error("Invalid configured public key");
34
39
  }
35
- this.provider = provider || new onchain_1.EsploraProvider(onchain_1.ESPLORA_URL[network]);
36
- this.network = (0, networks_1.getNetwork)(network);
37
- this.onchainP2TR = (0, payment_1.p2tr)(pubkey, undefined, this.network);
40
+ const network = (0, networks_1.getNetwork)(networkName);
41
+ const onchainProvider = provider || new onchain_1.EsploraProvider(onchain_1.ESPLORA_URL[networkName]);
42
+ const onchainP2TR = (0, payment_1.p2tr)(pubkey, undefined, network);
43
+ return new OnchainWallet(identity, network, onchainP2TR, onchainProvider);
38
44
  }
39
45
  get address() {
40
46
  return this.onchainP2TR.address || "";
@@ -12,13 +12,13 @@ var Request;
12
12
  Request.isBase = isBase;
13
13
  function isInitWallet(message) {
14
14
  return (message.type === "INIT_WALLET" &&
15
- "privateKey" in message &&
16
- typeof message.privateKey === "string" &&
17
15
  "arkServerUrl" in message &&
18
16
  typeof message.arkServerUrl === "string" &&
17
+ "privateKey" in message &&
18
+ typeof message.privateKey === "string" &&
19
19
  ("arkServerPublicKey" in message
20
- ? typeof message.arkServerPublicKey === "string" ||
21
- message.arkServerPublicKey === undefined
20
+ ? message.arkServerPublicKey === undefined ||
21
+ typeof message.arkServerPublicKey === "string"
22
22
  : true));
23
23
  }
24
24
  Request.isInitWallet = isInitWallet;
@@ -69,14 +69,4 @@ var Request;
69
69
  return message.type === "GET_STATUS";
70
70
  }
71
71
  Request.isGetStatus = isGetStatus;
72
- function isSign(message) {
73
- return (message.type === "SIGN" &&
74
- "tx" in message &&
75
- typeof message.tx === "string" &&
76
- ("inputIndexes" in message && message.inputIndexes != undefined
77
- ? Array.isArray(message.inputIndexes) &&
78
- message.inputIndexes.every((index) => typeof index === "number")
79
- : true));
80
- }
81
- Request.isSign = isSign;
82
72
  })(Request || (exports.Request = Request = {}));
@@ -175,17 +175,4 @@ var Response;
175
175
  };
176
176
  }
177
177
  Response.clearResponse = clearResponse;
178
- function signSuccess(id, tx) {
179
- return {
180
- type: "SIGN_SUCCESS",
181
- success: true,
182
- tx,
183
- id,
184
- };
185
- }
186
- Response.signSuccess = signSuccess;
187
- function isSignSuccess(response) {
188
- return response.type === "SIGN_SUCCESS" && response.success === true;
189
- }
190
- Response.isSignSuccess = isSignSuccess;
191
178
  })(Response || (exports.Response = Response = {}));