Use msgpackr

This commit is contained in:
Jim Hamill
2025-02-02 07:08:30 +00:00
parent a604363bec
commit e64d964e6e
5 changed files with 152 additions and 30 deletions
+2 -1
View File
@@ -35,6 +35,7 @@
"@electrum-cash/network": "^4.1.1",
"@electrum-cash/protocol": "2.1.0-development.8075178089",
"@xocash/primitives": "*",
"idb": "^8.0.0"
"idb": "^8.0.0",
"msgpackr": "^1.11.2"
}
}
@@ -224,23 +224,13 @@ export class BlockchainElectrum extends BaseBlockchain {
const cacheKey = `transactions.${txHash}`;
// Attempt to get the transaction hex from the cache or fetch it if not available.
const transactionHex = await this.store.getOrSet(cacheKey, async () => {
return await fetchTransaction(this.electrumClient, txHash);
const transactionRaw = await this.store.getOrSet(cacheKey, async () => {
const txHex = await fetchTransaction(this.electrumClient, txHash);
return Transaction.fromHex(txHex).toRaw();
});
// Decode the hex into our transaction primitive.
const transaction = Transaction.fromHex(transactionHex);
// Attempt to get the transaction hex from the cache or fetch it if not available.
// NOTE: We store JSON as opposed to hex because it is MUCH faster to decode.
/*
const transaction = new Transaction(
await this.store.getOrSet(cacheKey, async () => {
const tx = await fetchTransaction(this.electrumClient, txHash);
return Transaction.fromHex(tx).transaction;
}),
);
*/
const transaction = Transaction.fromRaw(transactionRaw);
// Return the transaction hex.
return transaction;
+9 -12
View File
@@ -3,10 +3,10 @@ import { StoreInMemorySynced } from './store-inmemory-synced.js';
import { Encrypter } from '../utils';
import {
encodeExtendedJsonObject,
decodeExtendedJsonObject,
} from '@xocash/primitives';
// TODO: Only use pack/unpack on encrypted content.
// Unsure why you were doing it on non-encrypted content (as it's not needed for IDB).
// Did you bump into an object that couldn't be structuredClone'd?
import { pack, unpack } from 'msgpackr';
import { openDB, deleteDB, IDBPDatabase } from 'idb';
@@ -156,7 +156,7 @@ export class StoreIndexedDB extends BaseStore {
return keys.reduce(
(acc, key, index) => {
acc[key.toString()] = decodeExtendedJsonObject(values[index]);
acc[key.toString()] = unpack(values[index]);
return acc;
},
{} as { [key: string]: T },
@@ -177,11 +177,11 @@ export class StoreIndexedDB extends BaseStore {
? this.encryption.decrypt(encodedValue)
: encodedValue;
return decodeExtendedJsonObject(decryptedValue) as T;
return unpack(decryptedValue) as T;
}
async set<T>(key: string, value: T) {
const encodedValue = encodeExtendedJsonObject(value);
const encodedValue = pack(value);
const encryptedValue = this.encryption
? this.encryption.encrypt(encodedValue)
@@ -203,10 +203,7 @@ export class StoreIndexedDB extends BaseStore {
const results = await Promise.all(
keys.map(async (key) => {
const value = await store.get(key);
return [
key,
value ? decodeExtendedJsonObject(value) : undefined,
] as const;
return [key, value ? unpack(value) : undefined] as const;
}),
);
@@ -223,7 +220,7 @@ export class StoreIndexedDB extends BaseStore {
await Promise.all(
Array.from(items).map(([key, value]) => {
const encodedValue = encodeExtendedJsonObject(value);
const encodedValue = pack(value);
return store.put(encodedValue, key);
}),
);
+1 -2
View File
@@ -1,4 +1,3 @@
// Import the Stamp class to derive the private keys
import { BaseWallet } from './base-wallet.js';
import { type TransactionTemplate } from './types.js';
import { TransactionBuilder } from './transaction-builder.js';
@@ -106,7 +105,7 @@ export class WalletHD<
this.wallets[CHAIN_INTERNAL] = [];
// Initialize proxies to our stored data.
this.genesisData = this.store.proxy('genesisData');
this.genesisData = this.store.proxy('genesis');
}
static async from<T extends typeof WalletHD>(