@ledgerhq/live-common 21.4.0 → 21.4.1-btcjs.6
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/lib/__tests__/families/bitcoin/logic.d.ts +2 -0
- package/lib/__tests__/families/bitcoin/logic.d.ts.map +1 -0
- package/lib/__tests__/families/bitcoin/logic.js +39 -0
- package/lib/__tests__/families/bitcoin/logic.js.map +1 -0
- package/lib/__tests__/test-helpers/mockDevice.d.ts.map +1 -1
- package/lib/__tests__/test-helpers/mockDevice.js +0 -1
- package/lib/__tests__/test-helpers/mockDevice.js.map +1 -1
- package/lib/account/addAccounts.d.ts.map +1 -1
- package/lib/account/addAccounts.js +2 -1
- package/lib/account/addAccounts.js.map +1 -1
- package/lib/account/support.d.ts.map +1 -1
- package/lib/account/support.js +24 -1
- package/lib/account/support.js.map +1 -1
- package/lib/api/explorerConfig/index.js +15 -15
- package/lib/bridge/jsHelpers.d.ts +6 -1
- package/lib/bridge/jsHelpers.d.ts.map +1 -1
- package/lib/bridge/jsHelpers.js +11 -3
- package/lib/bridge/jsHelpers.js.map +1 -1
- package/lib/families/bitcoin/account.d.ts.map +1 -1
- package/lib/families/bitcoin/account.js +3 -2
- package/lib/families/bitcoin/account.js.map +1 -1
- package/lib/families/bitcoin/bridge/js.d.ts +8 -0
- package/lib/families/bitcoin/bridge/js.d.ts.map +1 -0
- package/lib/families/bitcoin/bridge/js.js +149 -0
- package/lib/families/bitcoin/bridge/js.js.map +1 -0
- package/lib/families/bitcoin/cache.d.ts +28 -0
- package/lib/families/bitcoin/cache.d.ts.map +1 -0
- package/lib/families/bitcoin/cache.js +109 -0
- package/lib/families/bitcoin/cache.js.map +1 -0
- package/lib/families/bitcoin/datasets/bitcoin.js +6 -6
- package/lib/families/bitcoin/datasets/bitcoin.js.map +1 -1
- package/lib/families/bitcoin/datasets/litecoin.js +3 -3
- package/lib/families/bitcoin/datasets/litecoin.js.map +1 -1
- package/lib/families/bitcoin/getAccountNetworkInfo.d.ts +6 -0
- package/lib/families/bitcoin/getAccountNetworkInfo.d.ts.map +1 -0
- package/lib/families/bitcoin/getAccountNetworkInfo.js +124 -0
- package/lib/families/bitcoin/getAccountNetworkInfo.js.map +1 -0
- package/lib/families/bitcoin/getAccountNetworkInfo.test.d.ts +2 -0
- package/lib/families/bitcoin/getAccountNetworkInfo.test.d.ts.map +1 -0
- package/lib/families/bitcoin/getAccountNetworkInfo.test.js +71 -0
- package/lib/families/bitcoin/getAccountNetworkInfo.test.js.map +1 -0
- package/lib/families/bitcoin/js-broadcast.d.ts +11 -0
- package/lib/families/bitcoin/js-broadcast.d.ts.map +1 -0
- package/lib/families/bitcoin/js-broadcast.js +80 -0
- package/lib/families/bitcoin/js-broadcast.js.map +1 -0
- package/lib/families/bitcoin/js-buildTransaction.d.ts +5 -0
- package/lib/families/bitcoin/js-buildTransaction.d.ts.map +1 -0
- package/lib/families/bitcoin/js-buildTransaction.js +106 -0
- package/lib/families/bitcoin/js-buildTransaction.js.map +1 -0
- package/lib/families/bitcoin/js-createTransaction.d.ts +9 -0
- package/lib/families/bitcoin/js-createTransaction.d.ts.map +1 -0
- package/lib/families/bitcoin/js-createTransaction.js +27 -0
- package/lib/families/bitcoin/js-createTransaction.js.map +1 -0
- package/lib/families/bitcoin/js-estimateMaxSpendable.d.ts +15 -0
- package/lib/families/bitcoin/js-estimateMaxSpendable.d.ts.map +1 -0
- package/lib/families/bitcoin/js-estimateMaxSpendable.js +95 -0
- package/lib/families/bitcoin/js-estimateMaxSpendable.js.map +1 -0
- package/lib/families/bitcoin/js-getFeesForTransaction.d.ts +26 -0
- package/lib/families/bitcoin/js-getFeesForTransaction.d.ts.map +1 -0
- package/lib/families/bitcoin/js-getFeesForTransaction.js +100 -0
- package/lib/families/bitcoin/js-getFeesForTransaction.js.map +1 -0
- package/lib/families/bitcoin/js-getTransactionStatus.d.ts +5 -0
- package/lib/families/bitcoin/js-getTransactionStatus.d.ts.map +1 -0
- package/lib/families/bitcoin/js-getTransactionStatus.js +131 -0
- package/lib/families/bitcoin/js-getTransactionStatus.js.map +1 -0
- package/lib/families/bitcoin/js-prepareTransaction.d.ts +5 -0
- package/lib/families/bitcoin/js-prepareTransaction.d.ts.map +1 -0
- package/lib/families/bitcoin/js-prepareTransaction.js +80 -0
- package/lib/families/bitcoin/js-prepareTransaction.js.map +1 -0
- package/lib/families/bitcoin/js-signOperation.d.ts +10 -0
- package/lib/families/bitcoin/js-signOperation.d.ts.map +1 -0
- package/lib/families/bitcoin/js-signOperation.js +212 -0
- package/lib/families/bitcoin/js-signOperation.js.map +1 -0
- package/lib/families/bitcoin/js-synchronisation.d.ts +10 -0
- package/lib/families/bitcoin/js-synchronisation.d.ts.map +1 -0
- package/lib/families/bitcoin/js-synchronisation.js +411 -0
- package/lib/families/bitcoin/js-synchronisation.js.map +1 -0
- package/lib/families/bitcoin/logic.d.ts +34 -0
- package/lib/families/bitcoin/logic.d.ts.map +1 -0
- package/lib/families/bitcoin/logic.js +290 -0
- package/lib/families/bitcoin/logic.js.map +1 -0
- package/lib/families/bitcoin/networks.d.ts +3 -0
- package/lib/families/bitcoin/networks.d.ts.map +1 -0
- package/lib/families/bitcoin/networks.js +379 -0
- package/lib/families/bitcoin/networks.js.map +1 -0
- package/lib/families/bitcoin/serialization.d.ts +2 -2
- package/lib/families/bitcoin/serialization.d.ts.map +1 -1
- package/lib/families/bitcoin/serialization.js +8 -4
- package/lib/families/bitcoin/serialization.js.map +1 -1
- package/lib/families/bitcoin/specs.d.ts.map +1 -1
- package/lib/families/bitcoin/specs.js +8 -5
- package/lib/families/bitcoin/specs.js.map +1 -1
- package/lib/families/bitcoin/speculos-deviceActions.js +2 -2
- package/lib/families/bitcoin/speculos-deviceActions.js.map +1 -1
- package/lib/families/bitcoin/test-dataset.js +1 -1
- package/lib/families/bitcoin/test-dataset.js.map +1 -1
- package/lib/families/bitcoin/transaction.d.ts +3 -3
- package/lib/families/bitcoin/transaction.d.ts.map +1 -1
- package/lib/families/bitcoin/transaction.js +64 -61
- package/lib/families/bitcoin/transaction.js.map +1 -1
- package/lib/families/bitcoin/types.d.ts +66 -36
- package/lib/families/bitcoin/types.d.ts.map +1 -1
- package/lib/families/bitcoin/types.js +16 -1
- package/lib/families/bitcoin/types.js.map +1 -1
- package/lib/families/bitcoin/wallet.d.ts +6 -0
- package/lib/families/bitcoin/wallet.d.ts.map +1 -0
- package/lib/families/bitcoin/wallet.js +66 -0
- package/lib/families/bitcoin/wallet.js.map +1 -0
- package/lib/generated/bridge/js.d.ts +4 -0
- package/lib/generated/bridge/js.d.ts.map +1 -1
- package/lib/generated/bridge/js.js +20 -18
- package/lib/generated/bridge/js.js.map +1 -1
- package/lib/migrations/accounts-dataset.d.ts.map +1 -1
- package/lib/migrations/accounts-dataset.js +4 -2
- package/lib/migrations/accounts-dataset.js.map +1 -1
- package/lib/migrations/accounts.d.ts.map +1 -1
- package/lib/migrations/accounts.js +17 -0
- package/lib/migrations/accounts.js.map +1 -1
- package/lib/mock/account.d.ts.map +1 -1
- package/lib/mock/account.js +2 -1
- package/lib/mock/account.js.map +1 -1
- package/package.json +6 -6
- package/src/__tests__/__snapshots__/all.libcore.ts.snap +0 -125
- package/src/__tests__/families/bitcoin/logic.ts +40 -0
- package/src/__tests__/test-helpers/mockDevice.ts +0 -1
- package/src/account/addAccounts.ts +2 -1
- package/src/account/support.ts +24 -1
- package/src/api/explorerConfig/index.ts +15 -15
- package/src/bridge/jsHelpers.ts +22 -2
- package/src/families/bitcoin/account.ts +2 -1
- package/src/families/bitcoin/bridge/js.ts +76 -0
- package/src/families/bitcoin/cache.ts +76 -0
- package/src/families/bitcoin/datasets/bitcoin.ts +6 -6
- package/src/families/bitcoin/datasets/litecoin.ts +6 -6
- package/src/families/bitcoin/getAccountNetworkInfo.test.ts +27 -0
- package/src/families/bitcoin/getAccountNetworkInfo.ts +61 -0
- package/src/families/bitcoin/js-broadcast.ts +30 -0
- package/src/families/bitcoin/js-buildTransaction.ts +57 -0
- package/src/families/bitcoin/js-createTransaction.ts +27 -0
- package/src/families/bitcoin/js-estimateMaxSpendable.ts +36 -0
- package/src/families/bitcoin/js-getFeesForTransaction.ts +59 -0
- package/src/families/bitcoin/js-getTransactionStatus.ts +118 -0
- package/src/families/bitcoin/js-prepareTransaction.ts +35 -0
- package/src/families/bitcoin/js-signOperation.ts +164 -0
- package/src/families/bitcoin/js-synchronisation.ts +362 -0
- package/src/families/bitcoin/logic.ts +255 -0
- package/src/families/bitcoin/networks.ts +360 -0
- package/src/families/bitcoin/serialization.ts +6 -0
- package/src/families/bitcoin/specs.ts +5 -2
- package/src/families/bitcoin/speculos-deviceActions.ts +1 -1
- package/src/families/bitcoin/test-dataset.ts +1 -1
- package/src/families/bitcoin/transaction.ts +98 -93
- package/src/families/bitcoin/types.ts +95 -37
- package/src/families/bitcoin/wallet.ts +21 -0
- package/src/families/ethereum/modules/__snapshots__/compound.resilience.test.ts.snap +50 -9
- package/src/generated/bridge/js.ts +3 -0
- package/src/migrations/accounts-dataset.ts +3 -0
- package/src/migrations/accounts.ts +18 -0
- package/src/mock/account.ts +2 -0
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { BigNumber } from "bignumber.js";
|
|
2
|
+
import { Observable } from "rxjs";
|
|
3
|
+
import Btc from "@ledgerhq/hw-app-btc";
|
|
4
|
+
import { log } from "@ledgerhq/logs";
|
|
5
|
+
import type { Account, Operation, SignOperationEvent } from "./../../types";
|
|
6
|
+
import { isSegwitDerivationMode } from "./../../derivation";
|
|
7
|
+
import { encodeOperationId } from "./../../operation";
|
|
8
|
+
import { open, close } from "./../../hw";
|
|
9
|
+
import type { Transaction } from "./types";
|
|
10
|
+
import { getNetworkParameters } from "./networks";
|
|
11
|
+
import { buildTransaction } from "./js-buildTransaction";
|
|
12
|
+
import { calculateFees } from "./cache";
|
|
13
|
+
import wallet, { getWalletAccount } from "./wallet";
|
|
14
|
+
import { perCoinLogic } from "./logic";
|
|
15
|
+
|
|
16
|
+
const signOperation = ({
|
|
17
|
+
account,
|
|
18
|
+
deviceId,
|
|
19
|
+
transaction,
|
|
20
|
+
}: {
|
|
21
|
+
account: Account;
|
|
22
|
+
deviceId: any;
|
|
23
|
+
transaction: Transaction;
|
|
24
|
+
}): Observable<SignOperationEvent> =>
|
|
25
|
+
Observable.create((o) => {
|
|
26
|
+
async function main() {
|
|
27
|
+
const { currency } = account;
|
|
28
|
+
const transport = await open(deviceId);
|
|
29
|
+
const hwApp = new Btc(transport);
|
|
30
|
+
const walletAccount = await getWalletAccount(account);
|
|
31
|
+
|
|
32
|
+
try {
|
|
33
|
+
log("hw", `signTransaction ${currency.id} for account ${account.id}`);
|
|
34
|
+
const txInfo = await buildTransaction(account, transaction);
|
|
35
|
+
let senders: string[] = [];
|
|
36
|
+
let recipients: string[] = [];
|
|
37
|
+
let fee = new BigNumber(0);
|
|
38
|
+
// Maybe better not re-calculate these fields here, instead include them
|
|
39
|
+
// in Transaction type and set them in prepareTransaction?
|
|
40
|
+
await calculateFees({
|
|
41
|
+
account,
|
|
42
|
+
transaction,
|
|
43
|
+
}).then((res) => {
|
|
44
|
+
senders = res.txInputs
|
|
45
|
+
.map((i) => i.address)
|
|
46
|
+
.filter(Boolean) as string[];
|
|
47
|
+
recipients = res.txOutputs
|
|
48
|
+
.map((o) => o.address)
|
|
49
|
+
.filter(Boolean) as string[];
|
|
50
|
+
fee = res.fees;
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// FIXME (legacy)
|
|
54
|
+
// should be `transaction.getLockTime()` as soon as lock time is
|
|
55
|
+
// handled by libcore (actually: it always returns a default value
|
|
56
|
+
// and that caused issue with zcash (see #904))
|
|
57
|
+
// cf. https://github.com/LedgerHQ/lib-ledger-core/blob/fc9d762b83fc2b269d072b662065747a64ab2816/core/src/wallet/bitcoin/transaction_builders/BitcoinLikeUtxoPicker.cpp#L156-L159
|
|
58
|
+
let lockTime;
|
|
59
|
+
|
|
60
|
+
// (legacy) Set lockTime for Komodo to enable reward claiming on UTXOs created by
|
|
61
|
+
// Ledger Live. We should only set this if the currency is Komodo and
|
|
62
|
+
// lockTime isn't already defined.
|
|
63
|
+
if (currency.id === "komodo" && lockTime === undefined) {
|
|
64
|
+
const unixtime = Math.floor(Date.now() / 1000);
|
|
65
|
+
lockTime = unixtime - 777;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const networkParams = getNetworkParameters(currency.id);
|
|
69
|
+
const sigHashType = networkParams.sigHash;
|
|
70
|
+
if (isNaN(sigHashType)) {
|
|
71
|
+
throw new Error("sigHashType should not be NaN");
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const segwit = isSegwitDerivationMode(account.derivationMode);
|
|
75
|
+
|
|
76
|
+
// FIXME Call to explorer needed to set timestamp
|
|
77
|
+
// cf. https://github.com/LedgerHQ/lib-ledger-core/blob/fc9d762b83fc2b269d072b662065747a64ab2816/core/src/wallet/bitcoin/transaction_builders/BitcoinLikeUtxoPicker.cpp#L150-L154
|
|
78
|
+
/*
|
|
79
|
+
const hasTimestamp = networkParams.usesTimestampedTransaction;
|
|
80
|
+
const initialTimestamp = hasTimestamp
|
|
81
|
+
? transaction.timestamp
|
|
82
|
+
: undefined;
|
|
83
|
+
*/
|
|
84
|
+
|
|
85
|
+
const perCoin = perCoinLogic[currency.id];
|
|
86
|
+
let additionals = [currency.id];
|
|
87
|
+
|
|
88
|
+
if (account.derivationMode === "native_segwit") {
|
|
89
|
+
additionals.push("bech32");
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (perCoin?.getAdditionals) {
|
|
93
|
+
additionals = additionals.concat(
|
|
94
|
+
perCoin.getAdditionals({
|
|
95
|
+
transaction,
|
|
96
|
+
})
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const expiryHeight = perCoin?.hasExpiryHeight
|
|
101
|
+
? Buffer.from([0x00, 0x00, 0x00, 0x00])
|
|
102
|
+
: undefined;
|
|
103
|
+
|
|
104
|
+
const signature = await wallet.signAccountTx({
|
|
105
|
+
btc: hwApp,
|
|
106
|
+
fromAccount: walletAccount,
|
|
107
|
+
txInfo,
|
|
108
|
+
lockTime,
|
|
109
|
+
sigHashType,
|
|
110
|
+
segwit,
|
|
111
|
+
//initialTimestamp,
|
|
112
|
+
additionals,
|
|
113
|
+
expiryHeight,
|
|
114
|
+
onDeviceSignatureGranted: () =>
|
|
115
|
+
o.next({
|
|
116
|
+
type: "device-signature-granted",
|
|
117
|
+
}),
|
|
118
|
+
onDeviceSignatureRequested: () =>
|
|
119
|
+
o.next({
|
|
120
|
+
type: "device-signature-requested",
|
|
121
|
+
}),
|
|
122
|
+
onDeviceStreaming: ({ progress, index, total }) =>
|
|
123
|
+
o.next({
|
|
124
|
+
type: "device-streaming",
|
|
125
|
+
progress,
|
|
126
|
+
index,
|
|
127
|
+
total,
|
|
128
|
+
}),
|
|
129
|
+
});
|
|
130
|
+
// Build the optimistic operation
|
|
131
|
+
const operation: Operation = {
|
|
132
|
+
id: encodeOperationId(account.id, "", "OUT"),
|
|
133
|
+
hash: "", // Will be resolved in broadcast()
|
|
134
|
+
type: "OUT",
|
|
135
|
+
value: new BigNumber(transaction.amount).plus(fee),
|
|
136
|
+
fee,
|
|
137
|
+
blockHash: null,
|
|
138
|
+
blockHeight: null,
|
|
139
|
+
senders,
|
|
140
|
+
recipients,
|
|
141
|
+
accountId: account.id,
|
|
142
|
+
date: new Date(),
|
|
143
|
+
extra: {},
|
|
144
|
+
};
|
|
145
|
+
o.next({
|
|
146
|
+
type: "signed",
|
|
147
|
+
signedOperation: {
|
|
148
|
+
operation,
|
|
149
|
+
signature,
|
|
150
|
+
expirationDate: null,
|
|
151
|
+
},
|
|
152
|
+
});
|
|
153
|
+
} finally {
|
|
154
|
+
close(transport, deviceId);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
main().then(
|
|
159
|
+
() => o.complete(),
|
|
160
|
+
(e) => o.error(e)
|
|
161
|
+
);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
export default signOperation;
|
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
import { $Shape } from "utility-types";
|
|
2
|
+
import type {
|
|
3
|
+
TX,
|
|
4
|
+
Currency,
|
|
5
|
+
Input as WalletInput,
|
|
6
|
+
Output as WalletOutput,
|
|
7
|
+
} from "@ledgerhq/wallet-btc";
|
|
8
|
+
import { DerivationModes as WalletDerivationModes } from "@ledgerhq/wallet-btc";
|
|
9
|
+
import { BigNumber } from "bignumber.js";
|
|
10
|
+
import Btc from "@ledgerhq/hw-app-btc";
|
|
11
|
+
import { log } from "@ledgerhq/logs";
|
|
12
|
+
import type {
|
|
13
|
+
Account,
|
|
14
|
+
Operation,
|
|
15
|
+
OperationType,
|
|
16
|
+
DerivationMode,
|
|
17
|
+
} from "../../types";
|
|
18
|
+
import type { GetAccountShape } from "../../bridge/jsHelpers";
|
|
19
|
+
import { makeSync, makeScanAccounts, mergeOps } from "../../bridge/jsHelpers";
|
|
20
|
+
import { findCurrencyExplorer } from "../../api/Ledger";
|
|
21
|
+
import { encodeOperationId } from "../../operation";
|
|
22
|
+
import { BitcoinOutput } from "./types";
|
|
23
|
+
import { perCoinLogic } from "./logic";
|
|
24
|
+
import wallet from "./wallet";
|
|
25
|
+
|
|
26
|
+
// Map LL's DerivationMode to wallet-btc's
|
|
27
|
+
const toWalletDerivationMode = (
|
|
28
|
+
mode: DerivationMode
|
|
29
|
+
): WalletDerivationModes => {
|
|
30
|
+
switch (mode) {
|
|
31
|
+
case "segwit":
|
|
32
|
+
case "segwit_on_legacy":
|
|
33
|
+
case "segwit_unsplit":
|
|
34
|
+
case "bch_on_bitcoin_segwit":
|
|
35
|
+
case "vertcoin_128_segwit":
|
|
36
|
+
return WalletDerivationModes.SEGWIT;
|
|
37
|
+
|
|
38
|
+
case "native_segwit":
|
|
39
|
+
return WalletDerivationModes.NATIVE_SEGWIT;
|
|
40
|
+
|
|
41
|
+
default:
|
|
42
|
+
return WalletDerivationModes.LEGACY;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
// Map LL's currency ID to wallet-btc's Account.params.network
|
|
47
|
+
const toWalletNetwork = (currencyId: string): "testnet" | "mainnet" => {
|
|
48
|
+
return ["bitcoin_testnet"].includes(currencyId) ? "testnet" : "mainnet";
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
// Map wallet-btc's Output to LL's BitcoinOutput
|
|
52
|
+
const fromWalletUtxo = (utxo: WalletOutput): BitcoinOutput => {
|
|
53
|
+
return {
|
|
54
|
+
hash: utxo.output_hash,
|
|
55
|
+
outputIndex: utxo.output_index,
|
|
56
|
+
blockHeight: utxo.block_height,
|
|
57
|
+
address: utxo.address,
|
|
58
|
+
value: new BigNumber(utxo.value),
|
|
59
|
+
rbf: utxo.rbf,
|
|
60
|
+
isChange: false, // wallet-btc limitation: doesn't provide it
|
|
61
|
+
path: "",
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
// wallet-btc limitation: returns all transactions twice (for each side of the tx)
|
|
66
|
+
// so we need to deduplicate them...
|
|
67
|
+
const deduplicateOperations = (
|
|
68
|
+
operations: (Operation | undefined)[]
|
|
69
|
+
): Operation[] => {
|
|
70
|
+
const seen = {};
|
|
71
|
+
const out: Operation[] = [];
|
|
72
|
+
let j = 0;
|
|
73
|
+
|
|
74
|
+
for (const operation of operations) {
|
|
75
|
+
if (operation) {
|
|
76
|
+
if (seen[operation.id] !== 1) {
|
|
77
|
+
seen[operation.id] = 1;
|
|
78
|
+
out[j++] = operation;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return out;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const mapTxToOperations = (
|
|
87
|
+
tx: TX,
|
|
88
|
+
currencyId: string,
|
|
89
|
+
accountId: string,
|
|
90
|
+
accountAddresses: string[],
|
|
91
|
+
changeAddresses: string[]
|
|
92
|
+
): $Shape<Operation[]> => {
|
|
93
|
+
const operations: Operation[] = [];
|
|
94
|
+
const hash = tx.hash;
|
|
95
|
+
const fee = new BigNumber(tx.fees);
|
|
96
|
+
const blockHeight = tx.block?.height;
|
|
97
|
+
const blockHash = tx.block?.hash;
|
|
98
|
+
const date = new Date(tx.block?.time || tx.received_at);
|
|
99
|
+
const senders: string[] = [];
|
|
100
|
+
const recipients: string[] = [];
|
|
101
|
+
let type: OperationType = "OUT";
|
|
102
|
+
let value = new BigNumber(0);
|
|
103
|
+
const hasFailed = false;
|
|
104
|
+
const accountInputs: WalletInput[] = [];
|
|
105
|
+
const accountOutputs: WalletOutput[] = [];
|
|
106
|
+
const syncReplaceAddress = perCoinLogic[currencyId]?.syncReplaceAddress;
|
|
107
|
+
|
|
108
|
+
for (const input of tx.inputs) {
|
|
109
|
+
if (input.address) {
|
|
110
|
+
senders.push(
|
|
111
|
+
syncReplaceAddress ? syncReplaceAddress(input.address) : input.address
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
if (input.value) {
|
|
115
|
+
if (accountAddresses.includes(input.address)) {
|
|
116
|
+
// This address is part of the account
|
|
117
|
+
value = value.plus(input.value);
|
|
118
|
+
accountInputs.push(input);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// All inputs of a same transaction have the same sequence
|
|
125
|
+
const transactionSequenceNumber =
|
|
126
|
+
(accountInputs.length > 0 && accountInputs[0].sequence) || undefined;
|
|
127
|
+
const hasSpentNothing = value.eq(0);
|
|
128
|
+
|
|
129
|
+
for (const output of tx.outputs) {
|
|
130
|
+
if (output.address) {
|
|
131
|
+
if (accountAddresses.includes(output.address)) {
|
|
132
|
+
accountOutputs.push(output);
|
|
133
|
+
|
|
134
|
+
if (changeAddresses.includes(output.address)) {
|
|
135
|
+
if (
|
|
136
|
+
(recipients.length === 0 &&
|
|
137
|
+
output.output_hash ===
|
|
138
|
+
tx.outputs[tx.outputs.length - 1].output_hash) || // Is that condition safe?
|
|
139
|
+
hasSpentNothing
|
|
140
|
+
) {
|
|
141
|
+
recipients.push(
|
|
142
|
+
syncReplaceAddress
|
|
143
|
+
? syncReplaceAddress(output.address)
|
|
144
|
+
: output.address
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
} else {
|
|
148
|
+
recipients.push(
|
|
149
|
+
syncReplaceAddress
|
|
150
|
+
? syncReplaceAddress(output.address)
|
|
151
|
+
: output.address
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
} else {
|
|
155
|
+
recipients.push(
|
|
156
|
+
syncReplaceAddress
|
|
157
|
+
? syncReplaceAddress(output.address)
|
|
158
|
+
: output.address
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (accountInputs.length > 0) {
|
|
165
|
+
// It's a SEND operation
|
|
166
|
+
for (const output of accountOutputs) {
|
|
167
|
+
if (changeAddresses.includes(output.address)) {
|
|
168
|
+
value = value.minus(output.value);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
type = "OUT";
|
|
173
|
+
operations.push({
|
|
174
|
+
id: encodeOperationId(accountId, hash, type),
|
|
175
|
+
hash,
|
|
176
|
+
type,
|
|
177
|
+
value,
|
|
178
|
+
fee,
|
|
179
|
+
senders,
|
|
180
|
+
recipients,
|
|
181
|
+
blockHeight,
|
|
182
|
+
blockHash,
|
|
183
|
+
transactionSequenceNumber,
|
|
184
|
+
accountId,
|
|
185
|
+
date,
|
|
186
|
+
hasFailed,
|
|
187
|
+
extra: {},
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (accountOutputs.length > 0) {
|
|
192
|
+
// It's a RECEIVE operation
|
|
193
|
+
const filterChangeAddresses = !!accountInputs.length;
|
|
194
|
+
let accountOutputCount = 0;
|
|
195
|
+
let finalAmount = new BigNumber(0);
|
|
196
|
+
|
|
197
|
+
for (const output of accountOutputs) {
|
|
198
|
+
if (!filterChangeAddresses || !changeAddresses.includes(output.address)) {
|
|
199
|
+
finalAmount = finalAmount.plus(output.value);
|
|
200
|
+
accountOutputCount += 1;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
if (accountOutputCount > 0) {
|
|
205
|
+
value = finalAmount;
|
|
206
|
+
type = "IN";
|
|
207
|
+
operations.push({
|
|
208
|
+
id: encodeOperationId(accountId, hash, type),
|
|
209
|
+
hash,
|
|
210
|
+
type,
|
|
211
|
+
value,
|
|
212
|
+
fee,
|
|
213
|
+
senders,
|
|
214
|
+
recipients,
|
|
215
|
+
blockHeight,
|
|
216
|
+
blockHash,
|
|
217
|
+
transactionSequenceNumber,
|
|
218
|
+
accountId,
|
|
219
|
+
date,
|
|
220
|
+
hasFailed,
|
|
221
|
+
extra: {},
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
return operations;
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
const getAccountShape: GetAccountShape = async (info) => {
|
|
230
|
+
const {
|
|
231
|
+
transport,
|
|
232
|
+
currency,
|
|
233
|
+
id: accountId,
|
|
234
|
+
index,
|
|
235
|
+
derivationPath,
|
|
236
|
+
derivationMode,
|
|
237
|
+
initialAccount,
|
|
238
|
+
} = info;
|
|
239
|
+
const paramXpub = initialAccount?.xpub;
|
|
240
|
+
|
|
241
|
+
// In case we get a full derivation path, extract the seed identification part
|
|
242
|
+
// 44'/0'/0'/0/0 --> 44'/0'
|
|
243
|
+
// FIXME Only the CLI provides a full derivationPath: why?
|
|
244
|
+
const rootPath = derivationPath.split("/", 2).join("/");
|
|
245
|
+
|
|
246
|
+
const walletNetwork = toWalletNetwork(currency.id);
|
|
247
|
+
const walletDerivationMode = toWalletDerivationMode(derivationMode);
|
|
248
|
+
const explorer = findCurrencyExplorer(currency);
|
|
249
|
+
if (!explorer) {
|
|
250
|
+
throw new Error(`No explorer found for currency ${currency.name}`);
|
|
251
|
+
}
|
|
252
|
+
if (explorer.version !== "v2" && explorer.version !== "v3") {
|
|
253
|
+
throw new Error(`Unsupported explorer version ${explorer.version}`);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const walletAccount = initialAccount?.bitcoinResources?.serializedData
|
|
257
|
+
? await wallet.importFromSerializedAccount(
|
|
258
|
+
initialAccount.bitcoinResources.serializedData
|
|
259
|
+
)
|
|
260
|
+
: await wallet.generateAccount({
|
|
261
|
+
btc: (!paramXpub && transport && new Btc(transport)) || undefined,
|
|
262
|
+
xpub: paramXpub,
|
|
263
|
+
path: rootPath,
|
|
264
|
+
index,
|
|
265
|
+
currency: <Currency>currency.id,
|
|
266
|
+
network: walletNetwork,
|
|
267
|
+
derivationMode: walletDerivationMode,
|
|
268
|
+
explorer: explorer && `ledger${explorer.version}`,
|
|
269
|
+
explorerURI: `${explorer.endpoint}/blockchain/${explorer.version}/${explorer.id}`,
|
|
270
|
+
storage: "mock",
|
|
271
|
+
storageParams: [],
|
|
272
|
+
});
|
|
273
|
+
const xpub = paramXpub || walletAccount.xpub.xpub;
|
|
274
|
+
const oldOperations = initialAccount?.operations || [];
|
|
275
|
+
await wallet.syncAccount(walletAccount);
|
|
276
|
+
const balance = await wallet.getAccountBalance(walletAccount);
|
|
277
|
+
const currentBlock = await walletAccount.xpub.explorer.getCurrentBlock();
|
|
278
|
+
const blockHeight = currentBlock?.height;
|
|
279
|
+
|
|
280
|
+
// @ts-expect-error return from wallet-btc should be typed
|
|
281
|
+
const { txs: transactions } = await wallet.getAccountTransactions(
|
|
282
|
+
walletAccount
|
|
283
|
+
);
|
|
284
|
+
const accountAddressesWithInfo = await walletAccount.xpub.getXpubAddresses();
|
|
285
|
+
const accountAddresses = accountAddressesWithInfo
|
|
286
|
+
? accountAddressesWithInfo.map((a) => a.address)
|
|
287
|
+
: [];
|
|
288
|
+
const changeAddressesWithInfo =
|
|
289
|
+
await walletAccount.xpub.storage.getUniquesAddresses({
|
|
290
|
+
account: 1,
|
|
291
|
+
});
|
|
292
|
+
const changeAddresses = changeAddressesWithInfo
|
|
293
|
+
? changeAddressesWithInfo.map((a) => a.address)
|
|
294
|
+
: [];
|
|
295
|
+
const newOperations = transactions
|
|
296
|
+
?.map((tx) =>
|
|
297
|
+
mapTxToOperations(
|
|
298
|
+
tx,
|
|
299
|
+
currency.id,
|
|
300
|
+
accountId,
|
|
301
|
+
accountAddresses,
|
|
302
|
+
changeAddresses
|
|
303
|
+
)
|
|
304
|
+
)
|
|
305
|
+
.flat();
|
|
306
|
+
const newUniqueOperations = deduplicateOperations(newOperations);
|
|
307
|
+
const operations = mergeOps(oldOperations, newUniqueOperations);
|
|
308
|
+
const rawUtxos = await wallet.getAccountUnspentUtxos(walletAccount);
|
|
309
|
+
const utxos = rawUtxos.map(fromWalletUtxo);
|
|
310
|
+
const serializedData = await wallet.exportToSerializedAccount(walletAccount);
|
|
311
|
+
return {
|
|
312
|
+
id: accountId,
|
|
313
|
+
xpub,
|
|
314
|
+
balance,
|
|
315
|
+
spendableBalance: balance,
|
|
316
|
+
operations,
|
|
317
|
+
operationsCount: operations.length,
|
|
318
|
+
blockHeight,
|
|
319
|
+
bitcoinResources: {
|
|
320
|
+
utxos,
|
|
321
|
+
serializedData,
|
|
322
|
+
},
|
|
323
|
+
};
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
const postSync = (initial: Account, synced: Account) => {
|
|
327
|
+
log("bitcoin/postSync", "bitcoinResources");
|
|
328
|
+
const perCoin = perCoinLogic[synced.currency.id];
|
|
329
|
+
|
|
330
|
+
if (perCoin) {
|
|
331
|
+
const { postBuildBitcoinResources, syncReplaceAddress } = perCoin;
|
|
332
|
+
|
|
333
|
+
if (postBuildBitcoinResources) {
|
|
334
|
+
synced.bitcoinResources = postBuildBitcoinResources(
|
|
335
|
+
synced,
|
|
336
|
+
synced.bitcoinResources
|
|
337
|
+
);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
if (syncReplaceAddress) {
|
|
341
|
+
synced.freshAddress = syncReplaceAddress(synced.freshAddress);
|
|
342
|
+
synced.freshAddresses = synced.freshAddresses.map((a) => ({
|
|
343
|
+
...a,
|
|
344
|
+
address: syncReplaceAddress(a.address),
|
|
345
|
+
}));
|
|
346
|
+
if (synced.bitcoinResources) {
|
|
347
|
+
synced.bitcoinResources.utxos = synced.bitcoinResources?.utxos.map(
|
|
348
|
+
(u) => ({
|
|
349
|
+
...u,
|
|
350
|
+
address: u.address && syncReplaceAddress(u.address),
|
|
351
|
+
})
|
|
352
|
+
);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
log("bitcoin/postSync", "bitcoinResources DONE");
|
|
358
|
+
return synced;
|
|
359
|
+
};
|
|
360
|
+
|
|
361
|
+
export const scanAccounts = makeScanAccounts(getAccountShape);
|
|
362
|
+
export const sync = makeSync(getAccountShape, postSync);
|