@arkade-os/sdk 0.2.3 → 0.3.0-alpha.1
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.
- package/README.md +114 -43
- package/dist/cjs/adapters/asyncStorage.js +5 -0
- package/dist/cjs/adapters/fileSystem.js +5 -0
- package/dist/cjs/adapters/indexedDB.js +5 -0
- package/dist/cjs/adapters/localStorage.js +5 -0
- package/dist/cjs/arknote/index.js +4 -4
- package/dist/cjs/bip322/index.js +11 -9
- package/dist/cjs/forfeit.js +2 -2
- package/dist/cjs/identity/index.js +15 -0
- package/dist/cjs/identity/singleKey.js +24 -5
- package/dist/cjs/index.js +7 -5
- package/dist/cjs/musig2/keys.js +7 -7
- package/dist/cjs/musig2/nonces.js +1 -1
- package/dist/cjs/musig2/sign.js +6 -6
- package/dist/cjs/networks.js +6 -6
- package/dist/cjs/repositories/contractRepository.js +130 -0
- package/dist/cjs/repositories/index.js +18 -0
- package/dist/cjs/repositories/walletRepository.js +136 -0
- package/dist/cjs/script/address.js +3 -3
- package/dist/cjs/script/base.js +7 -7
- package/dist/cjs/script/tapscript.js +21 -21
- package/dist/cjs/script/vhtlc.js +2 -2
- package/dist/cjs/storage/asyncStorage.js +47 -0
- package/dist/cjs/storage/fileSystem.js +138 -0
- package/dist/cjs/storage/inMemory.js +21 -0
- package/dist/cjs/storage/indexedDB.js +97 -0
- package/dist/cjs/storage/localStorage.js +48 -0
- package/dist/cjs/tree/signingSession.js +12 -11
- package/dist/cjs/tree/txTree.js +6 -6
- package/dist/cjs/tree/validation.js +5 -5
- package/dist/cjs/utils/arkTransaction.js +5 -5
- package/dist/cjs/utils/unknownFields.js +5 -5
- package/dist/cjs/wallet/onchain.js +16 -10
- package/dist/cjs/wallet/serviceWorker/request.js +4 -14
- package/dist/cjs/wallet/serviceWorker/response.js +0 -13
- package/dist/cjs/wallet/serviceWorker/wallet.js +124 -130
- package/dist/cjs/wallet/serviceWorker/worker.js +84 -53
- package/dist/cjs/wallet/unroll.js +6 -6
- package/dist/cjs/wallet/wallet.js +37 -20
- package/dist/esm/adapters/asyncStorage.js +1 -0
- package/dist/esm/adapters/fileSystem.js +1 -0
- package/dist/esm/adapters/indexedDB.js +1 -0
- package/dist/esm/adapters/localStorage.js +1 -0
- package/dist/esm/arknote/index.js +2 -2
- package/dist/esm/bip322/index.js +4 -2
- package/dist/esm/forfeit.js +1 -1
- package/dist/esm/identity/index.js +1 -1
- package/dist/esm/identity/singleKey.js +22 -3
- package/dist/esm/index.js +5 -4
- package/dist/esm/musig2/keys.js +7 -7
- package/dist/esm/musig2/nonces.js +1 -1
- package/dist/esm/musig2/sign.js +5 -5
- package/dist/esm/networks.js +1 -1
- package/dist/esm/repositories/contractRepository.js +126 -0
- package/dist/esm/repositories/index.js +2 -0
- package/dist/esm/repositories/walletRepository.js +132 -0
- package/dist/esm/script/address.js +1 -1
- package/dist/esm/script/base.js +3 -3
- package/dist/esm/script/tapscript.js +2 -2
- package/dist/esm/script/vhtlc.js +1 -1
- package/dist/esm/storage/asyncStorage.js +43 -0
- package/dist/esm/storage/fileSystem.js +101 -0
- package/dist/esm/storage/inMemory.js +17 -0
- package/dist/esm/storage/indexedDB.js +93 -0
- package/dist/esm/storage/localStorage.js +44 -0
- package/dist/esm/tree/signingSession.js +4 -3
- package/dist/esm/tree/txTree.js +2 -2
- package/dist/esm/tree/validation.js +2 -2
- package/dist/esm/utils/arkTransaction.js +2 -2
- package/dist/esm/utils/unknownFields.js +1 -1
- package/dist/esm/wallet/onchain.js +14 -8
- package/dist/esm/wallet/serviceWorker/request.js +4 -14
- package/dist/esm/wallet/serviceWorker/response.js +0 -13
- package/dist/esm/wallet/serviceWorker/wallet.js +125 -131
- package/dist/esm/wallet/serviceWorker/worker.js +85 -54
- package/dist/esm/wallet/unroll.js +2 -2
- package/dist/esm/wallet/wallet.js +25 -8
- package/dist/types/adapters/asyncStorage.d.ts +2 -0
- package/dist/types/adapters/fileSystem.d.ts +2 -0
- package/dist/types/adapters/indexedDB.d.ts +2 -0
- package/dist/types/adapters/localStorage.d.ts +2 -0
- package/dist/types/arknote/index.d.ts +1 -1
- package/dist/types/bip322/index.d.ts +2 -2
- package/dist/types/forfeit.d.ts +1 -1
- package/dist/types/identity/index.d.ts +4 -2
- package/dist/types/identity/singleKey.d.ts +13 -2
- package/dist/types/index.d.ts +5 -5
- package/dist/types/repositories/contractRepository.d.ts +20 -0
- package/dist/types/repositories/index.d.ts +2 -0
- package/dist/types/repositories/walletRepository.d.ts +38 -0
- package/dist/types/script/address.d.ts +1 -1
- package/dist/types/script/base.d.ts +1 -1
- package/dist/types/script/default.d.ts +1 -1
- package/dist/types/script/tapscript.d.ts +1 -1
- package/dist/types/script/vhtlc.d.ts +1 -1
- package/dist/types/storage/asyncStorage.d.ts +9 -0
- package/dist/types/storage/fileSystem.d.ts +11 -0
- package/dist/types/storage/inMemory.d.ts +8 -0
- package/dist/types/storage/index.d.ts +6 -0
- package/dist/types/storage/indexedDB.d.ts +12 -0
- package/dist/types/storage/localStorage.d.ts +8 -0
- package/dist/types/tree/txTree.d.ts +1 -1
- package/dist/types/tree/validation.d.ts +1 -1
- package/dist/types/utils/anchor.d.ts +1 -1
- package/dist/types/utils/arkTransaction.d.ts +3 -3
- package/dist/types/utils/unknownFields.d.ts +1 -1
- package/dist/types/wallet/index.d.ts +4 -1
- package/dist/types/wallet/onchain.d.ts +5 -4
- package/dist/types/wallet/serviceWorker/request.d.ts +1 -7
- package/dist/types/wallet/serviceWorker/response.d.ts +1 -8
- package/dist/types/wallet/serviceWorker/wallet.d.ts +67 -21
- package/dist/types/wallet/serviceWorker/worker.d.ts +16 -4
- package/dist/types/wallet/unroll.d.ts +1 -1
- package/dist/types/wallet/wallet.d.ts +5 -1
- package/package.json +39 -15
- package/dist/cjs/wallet/serviceWorker/db/vtxo/idb.js +0 -185
- package/dist/esm/wallet/serviceWorker/db/vtxo/idb.js +0 -181
- package/dist/types/wallet/serviceWorker/db/vtxo/idb.d.ts +0 -20
- package/dist/types/wallet/serviceWorker/db/vtxo/index.d.ts +0 -14
- /package/dist/cjs/{wallet/serviceWorker/db/vtxo → storage}/index.js +0 -0
- /package/dist/esm/{wallet/serviceWorker/db/vtxo → storage}/index.js +0 -0
|
@@ -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;
|
|
@@ -36,10 +36,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
36
36
|
exports.TreeSignerSession = exports.ErrMissingAggregateKey = exports.ErrMissingVtxoGraph = void 0;
|
|
37
37
|
exports.validateTreeSigs = validateTreeSigs;
|
|
38
38
|
const musig2 = __importStar(require("../musig2"));
|
|
39
|
-
const
|
|
39
|
+
const script_js_1 = require("@scure/btc-signer/script.js");
|
|
40
|
+
const transaction_js_1 = require("@scure/btc-signer/transaction.js");
|
|
40
41
|
const base_1 = require("@scure/base");
|
|
41
|
-
const
|
|
42
|
-
const
|
|
42
|
+
const secp256k1_js_1 = require("@noble/curves/secp256k1.js");
|
|
43
|
+
const utils_js_1 = require("@scure/btc-signer/utils.js");
|
|
43
44
|
const unknownFields_1 = require("../utils/unknownFields");
|
|
44
45
|
exports.ErrMissingVtxoGraph = new Error("missing vtxo graph");
|
|
45
46
|
exports.ErrMissingAggregateKey = new Error("missing aggregate key");
|
|
@@ -53,7 +54,7 @@ class TreeSignerSession {
|
|
|
53
54
|
this.rootSharedOutputAmount = null;
|
|
54
55
|
}
|
|
55
56
|
static random() {
|
|
56
|
-
const secretKey = (0,
|
|
57
|
+
const secretKey = (0, utils_js_1.randomPrivateKeyBytes)();
|
|
57
58
|
return new TreeSignerSession(secretKey);
|
|
58
59
|
}
|
|
59
60
|
init(tree, scriptRoot, rootInputAmount) {
|
|
@@ -62,7 +63,7 @@ class TreeSignerSession {
|
|
|
62
63
|
this.rootSharedOutputAmount = rootInputAmount;
|
|
63
64
|
}
|
|
64
65
|
getPublicKey() {
|
|
65
|
-
return
|
|
66
|
+
return secp256k1_js_1.secp256k1.getPublicKey(this.secretKey);
|
|
66
67
|
}
|
|
67
68
|
getNonces() {
|
|
68
69
|
if (!this.graph)
|
|
@@ -99,7 +100,7 @@ class TreeSignerSession {
|
|
|
99
100
|
if (!this.graph)
|
|
100
101
|
throw exports.ErrMissingVtxoGraph;
|
|
101
102
|
const myNonces = new Map();
|
|
102
|
-
const publicKey =
|
|
103
|
+
const publicKey = secp256k1_js_1.secp256k1.getPublicKey(this.secretKey);
|
|
103
104
|
for (const g of this.graph) {
|
|
104
105
|
const nonces = musig2.generateNonces(publicKey);
|
|
105
106
|
myNonces.set(g.txid, nonces);
|
|
@@ -131,7 +132,7 @@ class TreeSignerSession {
|
|
|
131
132
|
prevoutScripts.push(prevout.script);
|
|
132
133
|
}
|
|
133
134
|
const message = g.root.preimageWitnessV1(0, // always first input
|
|
134
|
-
prevoutScripts,
|
|
135
|
+
prevoutScripts, transaction_js_1.SigHash.DEFAULT, prevoutAmounts);
|
|
135
136
|
return musig2.sign(myNonce.secNonce, this.secretKey, aggNonce.pubNonce, cosigners, message, {
|
|
136
137
|
taprootTweak: this.scriptRoot,
|
|
137
138
|
sortKeys: true,
|
|
@@ -154,9 +155,9 @@ async function validateTreeSigs(finalAggregatedKey, sharedOutputAmount, vtxoTree
|
|
|
154
155
|
const prevout = getPrevOutput(finalAggregatedKey, vtxoTree, sharedOutputAmount, g.root);
|
|
155
156
|
// Calculate the message that was signed
|
|
156
157
|
const message = g.root.preimageWitnessV1(0, // always first input
|
|
157
|
-
[prevout.script],
|
|
158
|
+
[prevout.script], transaction_js_1.SigHash.DEFAULT, [prevout.amount]);
|
|
158
159
|
// Verify the signature
|
|
159
|
-
const isValid =
|
|
160
|
+
const isValid = secp256k1_js_1.schnorr.verify(input.tapKeySig, message, finalAggregatedKey);
|
|
160
161
|
if (!isValid) {
|
|
161
162
|
throw new Error("invalid signature");
|
|
162
163
|
}
|
|
@@ -164,8 +165,8 @@ async function validateTreeSigs(finalAggregatedKey, sharedOutputAmount, vtxoTree
|
|
|
164
165
|
}
|
|
165
166
|
function getPrevOutput(finalKey, graph, sharedOutputAmount, tx) {
|
|
166
167
|
// generate P2TR script from musig2 final key
|
|
167
|
-
const pkScript =
|
|
168
|
-
const txid = base_1.hex.encode((0,
|
|
168
|
+
const pkScript = script_js_1.Script.encode(["OP_1", finalKey.slice(1)]);
|
|
169
|
+
const txid = base_1.hex.encode((0, utils_js_1.sha256x2)(tx.toBytes(true)).reverse());
|
|
169
170
|
// if the input is the root input, return the shared output amount
|
|
170
171
|
if (txid === graph.txid) {
|
|
171
172
|
return {
|
package/dist/cjs/tree/txTree.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.TxTree = void 0;
|
|
4
|
-
const
|
|
4
|
+
const transaction_js_1 = require("@scure/btc-signer/transaction.js");
|
|
5
5
|
const base_1 = require("@scure/base");
|
|
6
6
|
const base_2 = require("@scure/base");
|
|
7
|
-
const
|
|
7
|
+
const utils_js_1 = require("@scure/btc-signer/utils.js");
|
|
8
8
|
/**
|
|
9
9
|
* TxTree is a graph of bitcoin transactions.
|
|
10
10
|
* It is used to represent batch tree created during settlement session
|
|
@@ -22,7 +22,7 @@ class TxTree {
|
|
|
22
22
|
const chunksByTxid = new Map();
|
|
23
23
|
for (const chunk of chunks) {
|
|
24
24
|
const decodedChunk = decodeNode(chunk);
|
|
25
|
-
const txid = base_2.hex.encode((0,
|
|
25
|
+
const txid = base_2.hex.encode((0, utils_js_1.sha256x2)(decodedChunk.tx.toBytes(true)).reverse());
|
|
26
26
|
chunksByTxid.set(txid, decodedChunk);
|
|
27
27
|
}
|
|
28
28
|
// Find the root chunks (the ones that aren't referenced as a child)
|
|
@@ -91,7 +91,7 @@ class TxTree {
|
|
|
91
91
|
}
|
|
92
92
|
child.validate();
|
|
93
93
|
const childInput = child.root.getInput(0);
|
|
94
|
-
const parentTxid = base_2.hex.encode((0,
|
|
94
|
+
const parentTxid = base_2.hex.encode((0, utils_js_1.sha256x2)(this.root.toBytes(true)).reverse());
|
|
95
95
|
// verify the input of the child is the output of the parent
|
|
96
96
|
if (!childInput.txid ||
|
|
97
97
|
base_2.hex.encode(childInput.txid) !== parentTxid ||
|
|
@@ -126,7 +126,7 @@ class TxTree {
|
|
|
126
126
|
return leaves;
|
|
127
127
|
}
|
|
128
128
|
get txid() {
|
|
129
|
-
return base_2.hex.encode((0,
|
|
129
|
+
return base_2.hex.encode((0, utils_js_1.sha256x2)(this.root.toBytes(true)).reverse());
|
|
130
130
|
}
|
|
131
131
|
find(txid) {
|
|
132
132
|
if (txid === this.txid) {
|
|
@@ -188,6 +188,6 @@ function buildGraph(rootTxid, chunksByTxid) {
|
|
|
188
188
|
return new TxTree(rootTx, children);
|
|
189
189
|
}
|
|
190
190
|
function decodeNode(chunk) {
|
|
191
|
-
const tx =
|
|
191
|
+
const tx = transaction_js_1.Transaction.fromPSBT(base_1.base64.decode(chunk.tx));
|
|
192
192
|
return { tx, children: chunk.children };
|
|
193
193
|
}
|
|
@@ -4,9 +4,9 @@ exports.ErrMissingCosignersPublicKeys = exports.ErrWrongCommitmentTxid = exports
|
|
|
4
4
|
exports.validateConnectorsTxGraph = validateConnectorsTxGraph;
|
|
5
5
|
exports.validateVtxoTxGraph = validateVtxoTxGraph;
|
|
6
6
|
const base_1 = require("@scure/base");
|
|
7
|
-
const
|
|
7
|
+
const transaction_js_1 = require("@scure/btc-signer/transaction.js");
|
|
8
8
|
const base_2 = require("@scure/base");
|
|
9
|
-
const
|
|
9
|
+
const utils_js_1 = require("@scure/btc-signer/utils.js");
|
|
10
10
|
const musig2_1 = require("../musig2");
|
|
11
11
|
const unknownFields_1 = require("../utils/unknownFields");
|
|
12
12
|
const ErrInvalidSettlementTx = (tx) => new Error(`invalid settlement transaction: ${tx}`);
|
|
@@ -28,10 +28,10 @@ function validateConnectorsTxGraph(settlementTxB64, connectorsGraph) {
|
|
|
28
28
|
if (connectorsGraph.root.inputsLength !== 1)
|
|
29
29
|
throw exports.ErrNumberOfInputs;
|
|
30
30
|
const rootInput = connectorsGraph.root.getInput(0);
|
|
31
|
-
const settlementTx =
|
|
31
|
+
const settlementTx = transaction_js_1.Transaction.fromPSBT(base_2.base64.decode(settlementTxB64));
|
|
32
32
|
if (settlementTx.outputsLength <= BATCH_OUTPUT_CONNECTORS_INDEX)
|
|
33
33
|
throw exports.ErrInvalidSettlementTxOutputs;
|
|
34
|
-
const expectedRootTxid = base_1.hex.encode((0,
|
|
34
|
+
const expectedRootTxid = base_1.hex.encode((0, utils_js_1.sha256x2)(settlementTx.toBytes(true)).reverse());
|
|
35
35
|
if (!rootInput.txid)
|
|
36
36
|
throw exports.ErrWrongSettlementTxid;
|
|
37
37
|
if (base_1.hex.encode(rootInput.txid) !== expectedRootTxid)
|
|
@@ -58,7 +58,7 @@ function validateVtxoTxGraph(graph, roundTransaction, sweepTapTreeRoot) {
|
|
|
58
58
|
throw exports.ErrEmptyTree;
|
|
59
59
|
}
|
|
60
60
|
const rootInput = graph.root.getInput(0);
|
|
61
|
-
const commitmentTxid = base_1.hex.encode((0,
|
|
61
|
+
const commitmentTxid = base_1.hex.encode((0, utils_js_1.sha256x2)(roundTransaction.toBytes(true)).reverse());
|
|
62
62
|
if (!rootInput.txid ||
|
|
63
63
|
base_1.hex.encode(rootInput.txid) !== commitmentTxid ||
|
|
64
64
|
rootInput.index !== BATCH_OUTPUT_VTXO_INDEX) {
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.buildOffchainTx = buildOffchainTx;
|
|
4
|
-
const
|
|
4
|
+
const transaction_js_1 = require("@scure/btc-signer/transaction.js");
|
|
5
5
|
const tapscript_1 = require("../script/tapscript");
|
|
6
6
|
const base_1 = require("../script/base");
|
|
7
7
|
const anchor_1 = require("./anchor");
|
|
8
8
|
const base_2 = require("@scure/base");
|
|
9
|
-
const
|
|
9
|
+
const utils_js_1 = require("@scure/btc-signer/utils.js");
|
|
10
10
|
const unknownFields_1 = require("./unknownFields");
|
|
11
11
|
/**
|
|
12
12
|
* Builds an offchain transaction with checkpoint transactions.
|
|
@@ -45,7 +45,7 @@ function buildVirtualTx(inputs, outputs) {
|
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
|
-
const tx = new
|
|
48
|
+
const tx = new transaction_js_1.Transaction({
|
|
49
49
|
version: 3,
|
|
50
50
|
allowUnknown: true,
|
|
51
51
|
allowUnknownOutputs: true,
|
|
@@ -55,7 +55,7 @@ function buildVirtualTx(inputs, outputs) {
|
|
|
55
55
|
tx.addInput({
|
|
56
56
|
txid: input.txid,
|
|
57
57
|
index: input.vout,
|
|
58
|
-
sequence: lockTime ?
|
|
58
|
+
sequence: lockTime ? transaction_js_1.DEFAULT_SEQUENCE - 1 : undefined,
|
|
59
59
|
witnessUtxo: {
|
|
60
60
|
script: base_1.VtxoScript.decode(input.tapTree).pkScript,
|
|
61
61
|
amount: BigInt(input.value),
|
|
@@ -91,7 +91,7 @@ function buildCheckpointTx(vtxo, serverUnrollScript) {
|
|
|
91
91
|
const collaborativeLeafProof = checkpointVtxoScript.findLeaf(base_2.hex.encode(collaborativeClosure.script));
|
|
92
92
|
// create the checkpoint input that will be used as input of the virtual tx
|
|
93
93
|
const checkpointInput = {
|
|
94
|
-
txid: base_2.hex.encode((0,
|
|
94
|
+
txid: base_2.hex.encode((0, utils_js_1.sha256x2)(checkpointTx.toBytes(true)).reverse()),
|
|
95
95
|
vout: 0,
|
|
96
96
|
value: vtxo.value,
|
|
97
97
|
tapLeafScript: collaborativeLeafProof,
|
|
@@ -37,7 +37,7 @@ exports.VtxoTreeExpiry = exports.CosignerPublicKey = exports.ConditionWitness =
|
|
|
37
37
|
exports.setArkPsbtField = setArkPsbtField;
|
|
38
38
|
exports.getArkPsbtFields = getArkPsbtFields;
|
|
39
39
|
const bip68 = __importStar(require("bip68"));
|
|
40
|
-
const
|
|
40
|
+
const script_js_1 = require("@scure/btc-signer/script.js");
|
|
41
41
|
const base_1 = require("@scure/base");
|
|
42
42
|
/**
|
|
43
43
|
* ArkPsbtFieldKey is the key values for ark psbt fields.
|
|
@@ -126,12 +126,12 @@ exports.ConditionWitness = {
|
|
|
126
126
|
type: exports.ArkPsbtFieldKeyType,
|
|
127
127
|
key: encodedPsbtFieldKey[ArkPsbtFieldKey.ConditionWitness],
|
|
128
128
|
},
|
|
129
|
-
|
|
129
|
+
script_js_1.RawWitness.encode(value),
|
|
130
130
|
],
|
|
131
131
|
decode: (value) => nullIfCatch(() => {
|
|
132
132
|
if (!checkKeyIncludes(value[0], ArkPsbtFieldKey.ConditionWitness))
|
|
133
133
|
return null;
|
|
134
|
-
return
|
|
134
|
+
return script_js_1.RawWitness.decode(value[1]);
|
|
135
135
|
}),
|
|
136
136
|
};
|
|
137
137
|
/**
|
|
@@ -176,12 +176,12 @@ exports.VtxoTreeExpiry = {
|
|
|
176
176
|
type: exports.ArkPsbtFieldKeyType,
|
|
177
177
|
key: encodedPsbtFieldKey[ArkPsbtFieldKey.VtxoTreeExpiry],
|
|
178
178
|
},
|
|
179
|
-
(0,
|
|
179
|
+
(0, script_js_1.ScriptNum)(6, true).encode(value.value === 0n ? 0n : value.value),
|
|
180
180
|
],
|
|
181
181
|
decode: (unknown) => nullIfCatch(() => {
|
|
182
182
|
if (!checkKeyIncludes(unknown[0], ArkPsbtFieldKey.VtxoTreeExpiry))
|
|
183
183
|
return null;
|
|
184
|
-
const v = (0,
|
|
184
|
+
const v = (0, script_js_1.ScriptNum)(6, true).decode(unknown[1]);
|
|
185
185
|
if (!v)
|
|
186
186
|
return null;
|
|
187
187
|
const { blocks, seconds } = bip68.decode(Number(v));
|
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.OnchainWallet = void 0;
|
|
4
4
|
exports.selectCoins = selectCoins;
|
|
5
|
-
const
|
|
5
|
+
const payment_js_1 = require("@scure/btc-signer/payment.js");
|
|
6
6
|
const networks_1 = require("../networks");
|
|
7
7
|
const onchain_1 = require("../providers/onchain");
|
|
8
|
-
const
|
|
8
|
+
const transaction_js_1 = require("@scure/btc-signer/transaction.js");
|
|
9
9
|
const anchor_1 = require("../utils/anchor");
|
|
10
10
|
const txSizeEstimator_1 = require("../utils/txSizeEstimator");
|
|
11
11
|
/**
|
|
@@ -17,7 +17,7 @@ const txSizeEstimator_1 = require("../utils/txSizeEstimator");
|
|
|
17
17
|
*
|
|
18
18
|
* @example
|
|
19
19
|
* ```typescript
|
|
20
|
-
* const wallet =
|
|
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
|
-
|
|
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
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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_js_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 || "";
|
|
@@ -74,7 +80,7 @@ class OnchainWallet {
|
|
|
74
80
|
// Select coins
|
|
75
81
|
const selected = selectCoins(coins, totalNeeded);
|
|
76
82
|
// Create transaction
|
|
77
|
-
let tx = new
|
|
83
|
+
let tx = new transaction_js_1.Transaction();
|
|
78
84
|
// Add inputs
|
|
79
85
|
for (const input of selected.inputs) {
|
|
80
86
|
tx.addInput({
|
|
@@ -102,7 +108,7 @@ class OnchainWallet {
|
|
|
102
108
|
}
|
|
103
109
|
async bumpP2A(parent) {
|
|
104
110
|
const parentVsize = parent.vsize;
|
|
105
|
-
let child = new
|
|
111
|
+
let child = new transaction_js_1.Transaction({
|
|
106
112
|
allowUnknownInputs: true,
|
|
107
113
|
allowLegacyWitnessUtxo: true,
|
|
108
114
|
version: 3,
|
|
@@ -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
|
-
?
|
|
21
|
-
message.arkServerPublicKey ===
|
|
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 = {}));
|