@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.
- 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/bip322/index.js +2 -2
- package/dist/cjs/identity/index.js +15 -0
- package/dist/cjs/identity/singleKey.js +20 -1
- package/dist/cjs/index.js +5 -3
- package/dist/cjs/musig2/keys.js +6 -6
- package/dist/cjs/musig2/sign.js +5 -5
- 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/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 +4 -4
- package/dist/cjs/wallet/onchain.js +12 -6
- 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/wallet.js +21 -4
- 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/bip322/index.js +1 -1
- package/dist/esm/identity/index.js +1 -1
- package/dist/esm/identity/singleKey.js +21 -2
- package/dist/esm/index.js +4 -3
- package/dist/esm/musig2/keys.js +6 -6
- package/dist/esm/musig2/sign.js +4 -4
- 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/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 +1 -1
- package/dist/esm/wallet/onchain.js +12 -6
- 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/wallet.js +21 -4
- 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/identity/index.d.ts +3 -1
- package/dist/types/identity/singleKey.d.ts +12 -1
- package/dist/types/index.d.ts +4 -4
- 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/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/wallet/index.d.ts +3 -0
- package/dist/types/wallet/onchain.d.ts +3 -2
- 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/wallet.d.ts +4 -0
- package/package.json +38 -14
- 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
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { pubSchnorr, randomPrivateKeyBytes } from "@scure/btc-signer/utils";
|
|
1
|
+
import { pubSchnorr, randomPrivateKeyBytes, sha256, } from "@scure/btc-signer/utils";
|
|
2
2
|
import { hex } from "@scure/base";
|
|
3
3
|
import { SigHash } from "@scure/btc-signer";
|
|
4
4
|
import { TreeSignerSession } from '../tree/signingSession.js';
|
|
5
|
+
import { schnorr } from "@noble/secp256k1";
|
|
5
6
|
const ZERO_32 = new Uint8Array(32).fill(0);
|
|
6
7
|
const ALL_SIGHASH = Object.values(SigHash).filter((x) => typeof x === "number");
|
|
7
8
|
/**
|
|
@@ -15,6 +16,9 @@ const ALL_SIGHASH = Object.values(SigHash).filter((x) => typeof x === "number");
|
|
|
15
16
|
* // Create from raw bytes
|
|
16
17
|
* const key = SingleKey.fromPrivateKey(privateKeyBytes);
|
|
17
18
|
*
|
|
19
|
+
* // Create random key
|
|
20
|
+
* const randomKey = SingleKey.fromRandomBytes();
|
|
21
|
+
*
|
|
18
22
|
* // Sign a transaction
|
|
19
23
|
* const signedTx = await key.sign(transaction);
|
|
20
24
|
* ```
|
|
@@ -29,6 +33,17 @@ export class SingleKey {
|
|
|
29
33
|
static fromHex(privateKeyHex) {
|
|
30
34
|
return new SingleKey(hex.decode(privateKeyHex));
|
|
31
35
|
}
|
|
36
|
+
static fromRandomBytes() {
|
|
37
|
+
return new SingleKey(randomPrivateKeyBytes());
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Export the private key as a hex string.
|
|
41
|
+
*
|
|
42
|
+
* @returns The private key as a hex string
|
|
43
|
+
*/
|
|
44
|
+
toHex() {
|
|
45
|
+
return hex.encode(this.key);
|
|
46
|
+
}
|
|
32
47
|
async sign(tx, inputIndexes) {
|
|
33
48
|
const txCpy = tx.clone();
|
|
34
49
|
if (!inputIndexes) {
|
|
@@ -56,9 +71,13 @@ export class SingleKey {
|
|
|
56
71
|
return txCpy;
|
|
57
72
|
}
|
|
58
73
|
xOnlyPublicKey() {
|
|
59
|
-
return pubSchnorr(this.key);
|
|
74
|
+
return Promise.resolve(pubSchnorr(this.key));
|
|
60
75
|
}
|
|
61
76
|
signerSession() {
|
|
62
77
|
return TreeSignerSession.random();
|
|
63
78
|
}
|
|
79
|
+
async signMessage(message) {
|
|
80
|
+
const msgBytes = new TextEncoder().encode(message);
|
|
81
|
+
return schnorr.sign(sha256(msgBytes), this.key);
|
|
82
|
+
}
|
|
64
83
|
}
|
package/dist/esm/index.js
CHANGED
|
@@ -21,11 +21,12 @@ import { buildOffchainTx, } from './utils/arkTransaction.js';
|
|
|
21
21
|
import { VtxoTaprootTree, ConditionWitness, getArkPsbtFields, setArkPsbtField, ArkPsbtFieldKey, ArkPsbtFieldKeyType, CosignerPublicKey, VtxoTreeExpiry, } from './utils/unknownFields.js';
|
|
22
22
|
import { BIP322 } from './bip322/index.js';
|
|
23
23
|
import { ArkNote } from './arknote/index.js';
|
|
24
|
-
import { IndexedDBVtxoRepository } from './wallet/serviceWorker/db/vtxo/idb.js';
|
|
25
24
|
import { networks } from './networks.js';
|
|
26
25
|
import { RestIndexerProvider, IndexerTxType, ChainTxType, } from './providers/indexer.js';
|
|
27
26
|
import { P2A } from './utils/anchor.js';
|
|
28
27
|
import { Unroll } from './wallet/unroll.js';
|
|
28
|
+
import { WalletRepositoryImpl } from './repositories/walletRepository.js';
|
|
29
|
+
import { ContractRepositoryImpl } from './repositories/contractRepository.js';
|
|
29
30
|
export {
|
|
30
31
|
// Wallets
|
|
31
32
|
Wallet, SingleKey, OnchainWallet, Ramps,
|
|
@@ -47,8 +48,8 @@ buildOffchainTx, waitForIncomingFunds,
|
|
|
47
48
|
ArkNote,
|
|
48
49
|
// Network
|
|
49
50
|
networks,
|
|
50
|
-
//
|
|
51
|
-
|
|
51
|
+
// Repositories
|
|
52
|
+
WalletRepositoryImpl, ContractRepositoryImpl,
|
|
52
53
|
// BIP322
|
|
53
54
|
BIP322,
|
|
54
55
|
// TxTree
|
package/dist/esm/musig2/keys.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as musig from "@scure/btc-signer/musig2";
|
|
2
|
-
import { schnorr } from "@noble/curves/secp256k1";
|
|
2
|
+
import { schnorr } from "@noble/curves/secp256k1.js";
|
|
3
3
|
// Aggregates multiple public keys according to the MuSig2 algorithm
|
|
4
4
|
export function aggregateKeys(publicKeys, sort, options = {}) {
|
|
5
5
|
if (sort) {
|
|
@@ -8,14 +8,14 @@ export function aggregateKeys(publicKeys, sort, options = {}) {
|
|
|
8
8
|
const { aggPublicKey: preTweakedKey } = musig.keyAggregate(publicKeys);
|
|
9
9
|
if (!options.taprootTweak) {
|
|
10
10
|
return {
|
|
11
|
-
preTweakedKey: preTweakedKey.
|
|
12
|
-
finalKey: preTweakedKey.
|
|
11
|
+
preTweakedKey: preTweakedKey.toBytes(true),
|
|
12
|
+
finalKey: preTweakedKey.toBytes(true),
|
|
13
13
|
};
|
|
14
14
|
}
|
|
15
|
-
const tweakBytes = schnorr.utils.taggedHash("TapTweak", preTweakedKey.
|
|
15
|
+
const tweakBytes = schnorr.utils.taggedHash("TapTweak", preTweakedKey.toBytes(true).subarray(1), options.taprootTweak ?? new Uint8Array(0));
|
|
16
16
|
const { aggPublicKey: finalKey } = musig.keyAggregate(publicKeys, [tweakBytes], [true]);
|
|
17
17
|
return {
|
|
18
|
-
preTweakedKey: preTweakedKey.
|
|
19
|
-
finalKey: finalKey.
|
|
18
|
+
preTweakedKey: preTweakedKey.toBytes(true),
|
|
19
|
+
finalKey: finalKey.toBytes(true),
|
|
20
20
|
};
|
|
21
21
|
}
|
package/dist/esm/musig2/sign.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import * as musig from "@scure/btc-signer/musig2";
|
|
2
|
-
import { bytesToNumberBE } from "@noble/curves/
|
|
3
|
-
import {
|
|
2
|
+
import { bytesToNumberBE } from "@noble/curves/utils.js";
|
|
3
|
+
import { Point } from "@noble/secp256k1";
|
|
4
4
|
import { aggregateKeys } from './keys.js';
|
|
5
|
-
import { schnorr } from "@noble/curves/secp256k1";
|
|
5
|
+
import { schnorr } from "@noble/curves/secp256k1.js";
|
|
6
6
|
// Add this error type for decode failures
|
|
7
7
|
export class PartialSignatureError extends Error {
|
|
8
8
|
constructor(message) {
|
|
@@ -40,7 +40,7 @@ export class PartialSig {
|
|
|
40
40
|
}
|
|
41
41
|
// Verify s is less than curve order
|
|
42
42
|
const s = bytesToNumberBE(bytes);
|
|
43
|
-
if (s >= CURVE.n) {
|
|
43
|
+
if (s >= Point.CURVE().n) {
|
|
44
44
|
throw new PartialSignatureError("s value overflows curve order");
|
|
45
45
|
}
|
|
46
46
|
// For decode we don't have R, so we'll need to compute it later
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
export class ContractRepositoryImpl {
|
|
2
|
+
constructor(storage) {
|
|
3
|
+
this.cache = new Map();
|
|
4
|
+
this.storage = storage;
|
|
5
|
+
}
|
|
6
|
+
async getContractData(contractId, key) {
|
|
7
|
+
const storageKey = `contract:${contractId}:${key}`;
|
|
8
|
+
const cached = this.cache.get(storageKey);
|
|
9
|
+
if (cached !== undefined)
|
|
10
|
+
return cached;
|
|
11
|
+
const stored = await this.storage.getItem(storageKey);
|
|
12
|
+
if (!stored)
|
|
13
|
+
return null;
|
|
14
|
+
try {
|
|
15
|
+
const data = JSON.parse(stored);
|
|
16
|
+
this.cache.set(storageKey, data);
|
|
17
|
+
return data;
|
|
18
|
+
}
|
|
19
|
+
catch (error) {
|
|
20
|
+
console.error(`Failed to parse contract data for ${contractId}:${key}:`, error);
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
async setContractData(contractId, key, data) {
|
|
25
|
+
const storageKey = `contract:${contractId}:${key}`;
|
|
26
|
+
try {
|
|
27
|
+
// First persist to storage, only update cache if successful
|
|
28
|
+
await this.storage.setItem(storageKey, JSON.stringify(data));
|
|
29
|
+
this.cache.set(storageKey, data);
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
// Storage operation failed, cache remains unchanged
|
|
33
|
+
console.error(`Failed to persist contract data for ${contractId}:${key}:`, error);
|
|
34
|
+
throw error; // Rethrow to notify caller of failure
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
async deleteContractData(contractId, key) {
|
|
38
|
+
const storageKey = `contract:${contractId}:${key}`;
|
|
39
|
+
try {
|
|
40
|
+
// First remove from persistent storage, only delete from cache if successful
|
|
41
|
+
await this.storage.removeItem(storageKey);
|
|
42
|
+
this.cache.delete(storageKey);
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
// Storage operation failed, cache remains unchanged
|
|
46
|
+
console.error(`Failed to remove contract data for ${contractId}:${key}:`, error);
|
|
47
|
+
throw error; // Rethrow to notify caller of failure
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
async getContractCollection(contractType) {
|
|
51
|
+
const storageKey = `collection:${contractType}`;
|
|
52
|
+
const cached = this.cache.get(storageKey);
|
|
53
|
+
if (cached !== undefined)
|
|
54
|
+
return cached;
|
|
55
|
+
const stored = await this.storage.getItem(storageKey);
|
|
56
|
+
if (!stored) {
|
|
57
|
+
this.cache.set(storageKey, []);
|
|
58
|
+
return [];
|
|
59
|
+
}
|
|
60
|
+
try {
|
|
61
|
+
const collection = JSON.parse(stored);
|
|
62
|
+
this.cache.set(storageKey, collection);
|
|
63
|
+
return collection;
|
|
64
|
+
}
|
|
65
|
+
catch (error) {
|
|
66
|
+
console.error(`Failed to parse contract collection ${contractType}:`, error);
|
|
67
|
+
this.cache.set(storageKey, []);
|
|
68
|
+
return [];
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
async saveToContractCollection(contractType, item, idField) {
|
|
72
|
+
const collection = await this.getContractCollection(contractType);
|
|
73
|
+
// Validate that the item has the required id field
|
|
74
|
+
const itemId = item[idField];
|
|
75
|
+
if (itemId === undefined || itemId === null) {
|
|
76
|
+
throw new Error(`Item is missing required field '${String(idField)}'`);
|
|
77
|
+
}
|
|
78
|
+
// Find existing item index without mutating the original collection
|
|
79
|
+
const existingIndex = collection.findIndex((i) => i[idField] === itemId);
|
|
80
|
+
// Build new collection without mutating the cached one
|
|
81
|
+
let newCollection;
|
|
82
|
+
if (existingIndex !== -1) {
|
|
83
|
+
// Replace existing item
|
|
84
|
+
newCollection = [
|
|
85
|
+
...collection.slice(0, existingIndex),
|
|
86
|
+
item,
|
|
87
|
+
...collection.slice(existingIndex + 1),
|
|
88
|
+
];
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
// Add new item
|
|
92
|
+
newCollection = [...collection, item];
|
|
93
|
+
}
|
|
94
|
+
const storageKey = `collection:${contractType}`;
|
|
95
|
+
try {
|
|
96
|
+
// First persist to storage, only update cache if successful
|
|
97
|
+
await this.storage.setItem(storageKey, JSON.stringify(newCollection));
|
|
98
|
+
this.cache.set(storageKey, newCollection);
|
|
99
|
+
}
|
|
100
|
+
catch (error) {
|
|
101
|
+
// Storage operation failed, cache remains unchanged
|
|
102
|
+
console.error(`Failed to persist contract collection ${contractType}:`, error);
|
|
103
|
+
throw error; // Rethrow to notify caller of failure
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
async removeFromContractCollection(contractType, id, idField) {
|
|
107
|
+
// Validate input parameters
|
|
108
|
+
if (id === undefined || id === null) {
|
|
109
|
+
throw new Error(`Invalid id provided for removal: ${String(id)}`);
|
|
110
|
+
}
|
|
111
|
+
const collection = await this.getContractCollection(contractType);
|
|
112
|
+
// Build new collection without the specified item
|
|
113
|
+
const filtered = collection.filter((item) => item[idField] !== id);
|
|
114
|
+
const storageKey = `collection:${contractType}`;
|
|
115
|
+
try {
|
|
116
|
+
// First persist to storage, only update cache if successful
|
|
117
|
+
await this.storage.setItem(storageKey, JSON.stringify(filtered));
|
|
118
|
+
this.cache.set(storageKey, filtered);
|
|
119
|
+
}
|
|
120
|
+
catch (error) {
|
|
121
|
+
// Storage operation failed, cache remains unchanged
|
|
122
|
+
console.error(`Failed to persist contract collection removal for ${contractType}:`, error);
|
|
123
|
+
throw error; // Rethrow to notify caller of failure
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
export class WalletRepositoryImpl {
|
|
2
|
+
constructor(storage) {
|
|
3
|
+
this.storage = storage;
|
|
4
|
+
this.cache = {
|
|
5
|
+
vtxos: new Map(),
|
|
6
|
+
transactions: new Map(),
|
|
7
|
+
walletState: null,
|
|
8
|
+
initialized: new Set(),
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
async getVtxos(address) {
|
|
12
|
+
const cacheKey = `vtxos:${address}`;
|
|
13
|
+
if (this.cache.vtxos.has(address)) {
|
|
14
|
+
return this.cache.vtxos.get(address);
|
|
15
|
+
}
|
|
16
|
+
const stored = await this.storage.getItem(cacheKey);
|
|
17
|
+
if (!stored) {
|
|
18
|
+
this.cache.vtxos.set(address, []);
|
|
19
|
+
return [];
|
|
20
|
+
}
|
|
21
|
+
try {
|
|
22
|
+
const vtxos = JSON.parse(stored);
|
|
23
|
+
this.cache.vtxos.set(address, vtxos);
|
|
24
|
+
return vtxos.slice();
|
|
25
|
+
}
|
|
26
|
+
catch (error) {
|
|
27
|
+
console.error(`Failed to parse VTXOs for address ${address}:`, error);
|
|
28
|
+
this.cache.vtxos.set(address, []);
|
|
29
|
+
return [];
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
async saveVtxo(address, vtxo) {
|
|
33
|
+
const vtxos = await this.getVtxos(address);
|
|
34
|
+
const existing = vtxos.findIndex((v) => v.txid === vtxo.txid && v.vout === vtxo.vout);
|
|
35
|
+
if (existing !== -1) {
|
|
36
|
+
vtxos[existing] = vtxo;
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
vtxos.push(vtxo);
|
|
40
|
+
}
|
|
41
|
+
this.cache.vtxos.set(address, vtxos);
|
|
42
|
+
await this.storage.setItem(`vtxos:${address}`, JSON.stringify(vtxos));
|
|
43
|
+
}
|
|
44
|
+
async saveVtxos(address, vtxos) {
|
|
45
|
+
const storedVtxos = await this.getVtxos(address);
|
|
46
|
+
for (const vtxo of vtxos) {
|
|
47
|
+
const existing = storedVtxos.findIndex((v) => v.txid === vtxo.txid && v.vout === vtxo.vout);
|
|
48
|
+
if (existing !== -1) {
|
|
49
|
+
storedVtxos[existing] = vtxo;
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
storedVtxos.push(vtxo);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
this.cache.vtxos.set(address, storedVtxos);
|
|
56
|
+
await this.storage.setItem(`vtxos:${address}`, JSON.stringify(storedVtxos));
|
|
57
|
+
}
|
|
58
|
+
async removeVtxo(address, vtxoId) {
|
|
59
|
+
const vtxos = await this.getVtxos(address);
|
|
60
|
+
const [txid, vout] = vtxoId.split(":");
|
|
61
|
+
const filtered = vtxos.filter((v) => !(v.txid === txid && v.vout === parseInt(vout)));
|
|
62
|
+
this.cache.vtxos.set(address, filtered);
|
|
63
|
+
await this.storage.setItem(`vtxos:${address}`, JSON.stringify(filtered));
|
|
64
|
+
}
|
|
65
|
+
async clearVtxos(address) {
|
|
66
|
+
this.cache.vtxos.set(address, []);
|
|
67
|
+
await this.storage.removeItem(`vtxos:${address}`);
|
|
68
|
+
}
|
|
69
|
+
async getTransactionHistory(address) {
|
|
70
|
+
const cacheKey = `tx:${address}`;
|
|
71
|
+
if (this.cache.transactions.has(address)) {
|
|
72
|
+
return this.cache.transactions.get(address);
|
|
73
|
+
}
|
|
74
|
+
const stored = await this.storage.getItem(cacheKey);
|
|
75
|
+
if (!stored) {
|
|
76
|
+
this.cache.transactions.set(address, []);
|
|
77
|
+
return [];
|
|
78
|
+
}
|
|
79
|
+
try {
|
|
80
|
+
const transactions = JSON.parse(stored);
|
|
81
|
+
this.cache.transactions.set(address, transactions);
|
|
82
|
+
return transactions.slice();
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
console.error(`Failed to parse transactions for address ${address}:`, error);
|
|
86
|
+
this.cache.transactions.set(address, []);
|
|
87
|
+
return [];
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
async saveTransaction(address, tx) {
|
|
91
|
+
const transactions = await this.getTransactionHistory(address);
|
|
92
|
+
const existing = transactions.findIndex((t) => t.id === tx.id);
|
|
93
|
+
if (existing !== -1) {
|
|
94
|
+
transactions[existing] = tx;
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
transactions.push(tx);
|
|
98
|
+
// Sort by timestamp descending
|
|
99
|
+
transactions.sort((a, b) => b.timestamp - a.timestamp);
|
|
100
|
+
}
|
|
101
|
+
this.cache.transactions.set(address, transactions);
|
|
102
|
+
await this.storage.setItem(`tx:${address}`, JSON.stringify(transactions));
|
|
103
|
+
}
|
|
104
|
+
async getWalletState() {
|
|
105
|
+
if (this.cache.walletState !== null ||
|
|
106
|
+
this.cache.initialized.has("walletState")) {
|
|
107
|
+
return this.cache.walletState;
|
|
108
|
+
}
|
|
109
|
+
const stored = await this.storage.getItem("wallet:state");
|
|
110
|
+
if (!stored) {
|
|
111
|
+
this.cache.walletState = null;
|
|
112
|
+
this.cache.initialized.add("walletState");
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
try {
|
|
116
|
+
const state = JSON.parse(stored);
|
|
117
|
+
this.cache.walletState = state;
|
|
118
|
+
this.cache.initialized.add("walletState");
|
|
119
|
+
return state;
|
|
120
|
+
}
|
|
121
|
+
catch (error) {
|
|
122
|
+
console.error("Failed to parse wallet state:", error);
|
|
123
|
+
this.cache.walletState = null;
|
|
124
|
+
this.cache.initialized.add("walletState");
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
async saveWalletState(state) {
|
|
129
|
+
this.cache.walletState = state;
|
|
130
|
+
await this.storage.setItem("wallet:state", JSON.stringify(state));
|
|
131
|
+
}
|
|
132
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// Note: This requires @react-native-async-storage/async-storage to be installed
|
|
2
|
+
export class AsyncStorageAdapter {
|
|
3
|
+
constructor() {
|
|
4
|
+
try {
|
|
5
|
+
// Dynamic import to avoid errors in non-React Native environments
|
|
6
|
+
this.AsyncStorage =
|
|
7
|
+
require("@react-native-async-storage/async-storage").default;
|
|
8
|
+
}
|
|
9
|
+
catch (error) {
|
|
10
|
+
throw new Error("AsyncStorage is not available. Make sure @react-native-async-storage/async-storage is installed in React Native environment.");
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
async getItem(key) {
|
|
14
|
+
return await this.AsyncStorage.getItem(key);
|
|
15
|
+
}
|
|
16
|
+
async setItem(key, value) {
|
|
17
|
+
try {
|
|
18
|
+
await this.AsyncStorage.setItem(key, value);
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
console.error(`Failed to set item for key ${key}:`, error);
|
|
22
|
+
throw error;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
async removeItem(key) {
|
|
26
|
+
try {
|
|
27
|
+
await this.AsyncStorage.removeItem(key);
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
console.error(`Failed to remove item for key ${key}:`, error);
|
|
31
|
+
throw error;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
async clear() {
|
|
35
|
+
try {
|
|
36
|
+
await this.AsyncStorage.clear();
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
console.error("Failed to clear AsyncStorage:", error);
|
|
40
|
+
throw error;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import * as fs from "fs/promises";
|
|
2
|
+
import * as path from "path";
|
|
3
|
+
export class FileSystemStorageAdapter {
|
|
4
|
+
constructor(dirPath) {
|
|
5
|
+
// Normalize and resolve the storage base path once
|
|
6
|
+
this.basePath = path.resolve(dirPath).replace(/[/\\]+$/, "");
|
|
7
|
+
}
|
|
8
|
+
validateAndGetFilePath(key) {
|
|
9
|
+
// Reject dangerous keys
|
|
10
|
+
if (key === "." || key === "..") {
|
|
11
|
+
throw new Error("Invalid key: '.' and '..' are not allowed");
|
|
12
|
+
}
|
|
13
|
+
// Check for null bytes
|
|
14
|
+
if (key.includes("\0")) {
|
|
15
|
+
throw new Error("Invalid key: null bytes are not allowed");
|
|
16
|
+
}
|
|
17
|
+
// Check for path traversal attempts before normalization
|
|
18
|
+
if (key.includes("..")) {
|
|
19
|
+
throw new Error("Invalid key: directory traversal is not allowed");
|
|
20
|
+
}
|
|
21
|
+
// Check for reserved Windows names (case-insensitive)
|
|
22
|
+
const reservedNames = /^(CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])$/i;
|
|
23
|
+
const keyWithoutExt = key.split(".")[0];
|
|
24
|
+
if (reservedNames.test(keyWithoutExt)) {
|
|
25
|
+
throw new Error(`Invalid key: '${key}' uses a reserved Windows name`);
|
|
26
|
+
}
|
|
27
|
+
// Check for trailing spaces or dots
|
|
28
|
+
if (key.endsWith(" ") || key.endsWith(".")) {
|
|
29
|
+
throw new Error("Invalid key: trailing spaces or dots are not allowed");
|
|
30
|
+
}
|
|
31
|
+
// Normalize path separators and sanitize key
|
|
32
|
+
const normalizedKey = key
|
|
33
|
+
.replace(/[/\\]/g, "_")
|
|
34
|
+
.replace(/[^a-zA-Z0-9._-]/g, "_");
|
|
35
|
+
// Resolve the full path and check for directory traversal
|
|
36
|
+
const resolved = path.resolve(this.basePath, normalizedKey);
|
|
37
|
+
const relative = path.relative(this.basePath, resolved);
|
|
38
|
+
// Reject if trying to escape the base directory
|
|
39
|
+
if (relative.startsWith("..") || relative.includes(path.sep + "..")) {
|
|
40
|
+
throw new Error("Invalid key: directory traversal is not allowed");
|
|
41
|
+
}
|
|
42
|
+
return resolved;
|
|
43
|
+
}
|
|
44
|
+
async ensureDirectory() {
|
|
45
|
+
try {
|
|
46
|
+
await fs.access(this.basePath);
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
await fs.mkdir(this.basePath, { recursive: true });
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
async getItem(key) {
|
|
53
|
+
try {
|
|
54
|
+
const filePath = this.validateAndGetFilePath(key);
|
|
55
|
+
const data = await fs.readFile(filePath, "utf-8");
|
|
56
|
+
return data;
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
if (error.code === "ENOENT") {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
console.error(`Failed to read file for key ${key}:`, error);
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
async setItem(key, value) {
|
|
67
|
+
try {
|
|
68
|
+
await this.ensureDirectory();
|
|
69
|
+
const filePath = this.validateAndGetFilePath(key);
|
|
70
|
+
await fs.writeFile(filePath, value, "utf-8");
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
console.error(`Failed to write file for key ${key}:`, error);
|
|
74
|
+
throw error;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
async removeItem(key) {
|
|
78
|
+
try {
|
|
79
|
+
const filePath = this.validateAndGetFilePath(key);
|
|
80
|
+
await fs.unlink(filePath);
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
if (error.code !== "ENOENT") {
|
|
84
|
+
console.error(`Failed to remove file for key ${key}:`, error);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
async clear() {
|
|
89
|
+
try {
|
|
90
|
+
const entries = await fs.readdir(this.basePath);
|
|
91
|
+
await Promise.all(entries.map(async (entry) => {
|
|
92
|
+
const entryPath = path.join(this.basePath, entry);
|
|
93
|
+
// Use fs.rm with recursive option to handle both files and directories
|
|
94
|
+
await fs.rm(entryPath, { recursive: true, force: true });
|
|
95
|
+
}));
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
console.error("Failed to clear storage directory:", error);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export class InMemoryStorageAdapter {
|
|
2
|
+
constructor() {
|
|
3
|
+
this.store = new Map();
|
|
4
|
+
}
|
|
5
|
+
async getItem(key) {
|
|
6
|
+
return this.store.get(key) ?? null;
|
|
7
|
+
}
|
|
8
|
+
async setItem(key, value) {
|
|
9
|
+
this.store.set(key, value);
|
|
10
|
+
}
|
|
11
|
+
async removeItem(key) {
|
|
12
|
+
this.store.delete(key);
|
|
13
|
+
}
|
|
14
|
+
async clear() {
|
|
15
|
+
this.store.clear();
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
export class IndexedDBStorageAdapter {
|
|
2
|
+
constructor(dbName, version = 1) {
|
|
3
|
+
this.db = null;
|
|
4
|
+
this.dbName = dbName;
|
|
5
|
+
this.version = version;
|
|
6
|
+
}
|
|
7
|
+
async getDB() {
|
|
8
|
+
if (this.db)
|
|
9
|
+
return this.db;
|
|
10
|
+
const globalObject = typeof window === "undefined" ? self : window;
|
|
11
|
+
if (!(globalObject && "indexedDB" in globalObject)) {
|
|
12
|
+
throw new Error("IndexedDB is not available in this environment");
|
|
13
|
+
}
|
|
14
|
+
return new Promise((resolve, reject) => {
|
|
15
|
+
const request = globalObject.indexedDB.open(this.dbName, this.version);
|
|
16
|
+
request.onerror = () => reject(request.error);
|
|
17
|
+
request.onsuccess = () => {
|
|
18
|
+
this.db = request.result;
|
|
19
|
+
resolve(this.db);
|
|
20
|
+
};
|
|
21
|
+
request.onupgradeneeded = () => {
|
|
22
|
+
const db = request.result;
|
|
23
|
+
if (!db.objectStoreNames.contains("storage")) {
|
|
24
|
+
db.createObjectStore("storage");
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
async getItem(key) {
|
|
30
|
+
try {
|
|
31
|
+
const db = await this.getDB();
|
|
32
|
+
return new Promise((resolve, reject) => {
|
|
33
|
+
const transaction = db.transaction(["storage"], "readonly");
|
|
34
|
+
const store = transaction.objectStore("storage");
|
|
35
|
+
const request = store.get(key);
|
|
36
|
+
request.onerror = () => reject(request.error);
|
|
37
|
+
request.onsuccess = () => {
|
|
38
|
+
resolve(request.result || null);
|
|
39
|
+
};
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
console.error(`Failed to get item for key ${key}:`, error);
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
async setItem(key, value) {
|
|
48
|
+
try {
|
|
49
|
+
const db = await this.getDB();
|
|
50
|
+
return new Promise((resolve, reject) => {
|
|
51
|
+
const transaction = db.transaction(["storage"], "readwrite");
|
|
52
|
+
const store = transaction.objectStore("storage");
|
|
53
|
+
const request = store.put(value, key);
|
|
54
|
+
request.onerror = () => reject(request.error);
|
|
55
|
+
request.onsuccess = () => resolve();
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
console.error(`Failed to set item for key ${key}:`, error);
|
|
60
|
+
throw error;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
async removeItem(key) {
|
|
64
|
+
try {
|
|
65
|
+
const db = await this.getDB();
|
|
66
|
+
return new Promise((resolve, reject) => {
|
|
67
|
+
const transaction = db.transaction(["storage"], "readwrite");
|
|
68
|
+
const store = transaction.objectStore("storage");
|
|
69
|
+
const request = store.delete(key);
|
|
70
|
+
request.onerror = () => reject(request.error);
|
|
71
|
+
request.onsuccess = () => resolve();
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
console.error(`Failed to remove item for key ${key}:`, error);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
async clear() {
|
|
79
|
+
try {
|
|
80
|
+
const db = await this.getDB();
|
|
81
|
+
return new Promise((resolve, reject) => {
|
|
82
|
+
const transaction = db.transaction(["storage"], "readwrite");
|
|
83
|
+
const store = transaction.objectStore("storage");
|
|
84
|
+
const request = store.clear();
|
|
85
|
+
request.onerror = () => reject(request.error);
|
|
86
|
+
request.onsuccess = () => resolve();
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
catch (error) {
|
|
90
|
+
console.error("Failed to clear storage:", error);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|