hodl-wallet 1.8.2 → 1.8.4
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/index.js +8 -2
- package/network/lib/BitcoinNetwork.js +18 -10
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -464,17 +464,23 @@ class Wallet {
|
|
|
464
464
|
|
|
465
465
|
const transactionHash = receipt?.transactionHash || receipt?.hash || 'UNKNOWN_HASH';
|
|
466
466
|
|
|
467
|
-
//
|
|
467
|
+
// Calculate the post-transaction balance
|
|
468
468
|
let currentBalance = 0;
|
|
469
469
|
try {
|
|
470
470
|
const walletAddress = await this.getAddress();
|
|
471
|
+
|
|
472
|
+
// Get the balance AFTER transaction (not before)
|
|
471
473
|
if (token === this.selectedNetwork.nativeToken) {
|
|
472
474
|
currentBalance = await this.network.getBalance(walletAddress);
|
|
473
475
|
} else {
|
|
474
476
|
currentBalance = await this.network.getTokenBalance(walletAddress, token);
|
|
475
477
|
}
|
|
478
|
+
|
|
479
|
+
// Round to avoid floating point precision issues
|
|
480
|
+
currentBalance = Math.round(currentBalance * 100000000) / 100000000;
|
|
481
|
+
|
|
476
482
|
} catch (error) {
|
|
477
|
-
console.error('Error getting
|
|
483
|
+
console.error('Error getting post-transaction balance:', error.message);
|
|
478
484
|
}
|
|
479
485
|
|
|
480
486
|
await this.displayTransactionResult(address, token, numericAmount, transactionHash, currentBalance);
|
|
@@ -40,7 +40,7 @@ export default class BitcoinNetwork extends BaseNetwork {
|
|
|
40
40
|
async transfer(from, to, amount, options = {}) {
|
|
41
41
|
try {
|
|
42
42
|
const utxos = await this.getUTXOs(from.address);
|
|
43
|
-
const satoshis =
|
|
43
|
+
const satoshis = this.BTCToSatoshis(amount); // Regular number, not BigInt
|
|
44
44
|
const feeRate = options.feeRate || 10; // sats/byte
|
|
45
45
|
|
|
46
46
|
// Create PSBT instance with network
|
|
@@ -48,7 +48,7 @@ export default class BitcoinNetwork extends BaseNetwork {
|
|
|
48
48
|
psbt.setVersion(2);
|
|
49
49
|
psbt.setLocktime(0);
|
|
50
50
|
|
|
51
|
-
let totalInputValue =
|
|
51
|
+
let totalInputValue = 0;
|
|
52
52
|
|
|
53
53
|
// Add inputs
|
|
54
54
|
for (const utxo of utxos) {
|
|
@@ -62,16 +62,16 @@ export default class BitcoinNetwork extends BaseNetwork {
|
|
|
62
62
|
sequence: 0xffffffff
|
|
63
63
|
});
|
|
64
64
|
|
|
65
|
-
totalInputValue +=
|
|
65
|
+
totalInputValue += utxo.value;
|
|
66
66
|
|
|
67
67
|
// Break if we have enough funds (considering estimated fee)
|
|
68
|
-
const estimatedFee =
|
|
68
|
+
const estimatedFee = this.estimateTxSize(psbt.inputCount, 2) * feeRate;
|
|
69
69
|
if (totalInputValue >= satoshis + estimatedFee) {
|
|
70
70
|
break;
|
|
71
71
|
}
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
-
if (totalInputValue < satoshis +
|
|
74
|
+
if (totalInputValue < satoshis + feeRate) {
|
|
75
75
|
throw new Error('Insufficient balance for the transaction including fees.');
|
|
76
76
|
}
|
|
77
77
|
|
|
@@ -82,9 +82,9 @@ export default class BitcoinNetwork extends BaseNetwork {
|
|
|
82
82
|
});
|
|
83
83
|
|
|
84
84
|
// Add change output if needed
|
|
85
|
-
const estimatedFee =
|
|
85
|
+
const estimatedFee = this.estimateTxSize(psbt.inputCount, 2) * feeRate;
|
|
86
86
|
const changeValue = totalInputValue - satoshis - estimatedFee;
|
|
87
|
-
if (changeValue >
|
|
87
|
+
if (changeValue > 546) { // Dust threshold
|
|
88
88
|
psbt.addOutput({
|
|
89
89
|
address: from.address,
|
|
90
90
|
value: changeValue
|
|
@@ -164,15 +164,23 @@ export default class BitcoinNetwork extends BaseNetwork {
|
|
|
164
164
|
const path = `m/84'/0'/0'/0/0`; // BIP84 para SegWit nativo
|
|
165
165
|
const child = root.derivePath(path);
|
|
166
166
|
|
|
167
|
+
// Convert Uint8Array to Buffer if needed
|
|
168
|
+
const privateKeyBuffer = Buffer.isBuffer(child.privateKey)
|
|
169
|
+
? child.privateKey
|
|
170
|
+
: Buffer.from(child.privateKey);
|
|
171
|
+
|
|
172
|
+
// Create ECPair from the private key buffer
|
|
173
|
+
const keyPair = ECPair.fromPrivateKey(privateKeyBuffer, { network: this.network });
|
|
174
|
+
|
|
167
175
|
const { address } = bitcoin.payments.p2wpkh({
|
|
168
|
-
pubkey:
|
|
176
|
+
pubkey: keyPair.publicKey,
|
|
169
177
|
network: this.network
|
|
170
178
|
});
|
|
171
179
|
|
|
172
180
|
return {
|
|
173
181
|
address,
|
|
174
|
-
privateKey:
|
|
175
|
-
publicKey:
|
|
182
|
+
privateKey: keyPair.toWIF(),
|
|
183
|
+
publicKey: keyPair.publicKey.toString('hex'),
|
|
176
184
|
mnemonic
|
|
177
185
|
};
|
|
178
186
|
}
|