@ledgerhq/coin-cardano 0.1.0-next.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/.eslintrc.js +20 -0
- package/.turbo/turbo-build.log +4 -0
- package/.unimportedrc.json +35 -0
- package/CHANGELOG.md +19 -0
- package/LICENSE.txt +21 -0
- package/jest.config.js +8 -0
- package/lib/account.d.ts +7 -0
- package/lib/account.d.ts.map +1 -0
- package/lib/account.js +41 -0
- package/lib/account.js.map +1 -0
- package/lib/address.unit.test.d.ts +2 -0
- package/lib/address.unit.test.d.ts.map +1 -0
- package/lib/address.unit.test.js +21 -0
- package/lib/address.unit.test.js.map +1 -0
- package/lib/api/api-types.d.ts +106 -0
- package/lib/api/api-types.d.ts.map +1 -0
- package/lib/api/api-types.js +9 -0
- package/lib/api/api-types.js.map +1 -0
- package/lib/api/getDelegationInfo.d.ts +4 -0
- package/lib/api/getDelegationInfo.d.ts.map +1 -0
- package/lib/api/getDelegationInfo.js +48 -0
- package/lib/api/getDelegationInfo.js.map +1 -0
- package/lib/api/getNetworkInfo.d.ts +5 -0
- package/lib/api/getNetworkInfo.d.ts.map +1 -0
- package/lib/api/getNetworkInfo.js +46 -0
- package/lib/api/getNetworkInfo.js.map +1 -0
- package/lib/api/getPools.d.ts +5 -0
- package/lib/api/getPools.d.ts.map +1 -0
- package/lib/api/getPools.js +45 -0
- package/lib/api/getPools.js.map +1 -0
- package/lib/api/getTransactions.d.ts +10 -0
- package/lib/api/getTransactions.d.ts.map +1 -0
- package/lib/api/getTransactions.js +139 -0
- package/lib/api/getTransactions.js.map +1 -0
- package/lib/api/submitTransaction.d.ts +7 -0
- package/lib/api/submitTransaction.d.ts.map +1 -0
- package/lib/api/submitTransaction.js +34 -0
- package/lib/api/submitTransaction.js.map +1 -0
- package/lib/bridge/index.d.ts +11 -0
- package/lib/bridge/index.d.ts.map +1 -0
- package/lib/bridge/index.js +70 -0
- package/lib/bridge/index.js.map +1 -0
- package/lib/bridge.integration.test.d.ts +4 -0
- package/lib/bridge.integration.test.d.ts.map +1 -0
- package/lib/bridge.integration.test.js +113 -0
- package/lib/bridge.integration.test.js.map +1 -0
- package/lib/broadcast.d.ts +8 -0
- package/lib/broadcast.d.ts.map +1 -0
- package/lib/broadcast.js +28 -0
- package/lib/broadcast.js.map +1 -0
- package/lib/buildOptimisticOperation.d.ts +4 -0
- package/lib/buildOptimisticOperation.d.ts.map +1 -0
- package/lib/buildOptimisticOperation.js +125 -0
- package/lib/buildOptimisticOperation.js.map +1 -0
- package/lib/buildSubAccounts.d.ts +26 -0
- package/lib/buildSubAccounts.d.ts.map +1 -0
- package/lib/buildSubAccounts.js +124 -0
- package/lib/buildSubAccounts.js.map +1 -0
- package/lib/buildTransaction.d.ts +11 -0
- package/lib/buildTransaction.d.ts.map +1 -0
- package/lib/buildTransaction.js +342 -0
- package/lib/buildTransaction.js.map +1 -0
- package/lib/cli-transaction.d.ts +18 -0
- package/lib/cli-transaction.d.ts.map +1 -0
- package/lib/cli-transaction.js +66 -0
- package/lib/cli-transaction.js.map +1 -0
- package/lib/constants.d.ts +8 -0
- package/lib/constants.d.ts.map +1 -0
- package/lib/constants.js +12 -0
- package/lib/constants.js.map +1 -0
- package/lib/createTransaction.d.ts +9 -0
- package/lib/createTransaction.d.ts.map +1 -0
- package/lib/createTransaction.js +23 -0
- package/lib/createTransaction.js.map +1 -0
- package/lib/datasets/rawAccount.1.d.ts +3 -0
- package/lib/datasets/rawAccount.1.d.ts.map +1 -0
- package/lib/datasets/rawAccount.1.js +20 -0
- package/lib/datasets/rawAccount.1.js.map +1 -0
- package/lib/datasets/scanAccounts.d.ts +5 -0
- package/lib/datasets/scanAccounts.d.ts.map +1 -0
- package/lib/datasets/scanAccounts.js +27 -0
- package/lib/datasets/scanAccounts.js.map +1 -0
- package/lib/deviceTransactionConfig.d.ts +11 -0
- package/lib/deviceTransactionConfig.d.ts.map +1 -0
- package/lib/deviceTransactionConfig.js +106 -0
- package/lib/deviceTransactionConfig.js.map +1 -0
- package/lib/errors.d.ts +22 -0
- package/lib/errors.d.ts.map +1 -0
- package/lib/errors.js +18 -0
- package/lib/errors.js.map +1 -0
- package/lib/estimateMaxSpendable.d.ts +9 -0
- package/lib/estimateMaxSpendable.d.ts.map +1 -0
- package/lib/estimateMaxSpendable.js +53 -0
- package/lib/estimateMaxSpendable.js.map +1 -0
- package/lib/getTransactionStatus.d.ts +4 -0
- package/lib/getTransactionStatus.d.ts.map +1 -0
- package/lib/getTransactionStatus.js +214 -0
- package/lib/getTransactionStatus.js.map +1 -0
- package/lib/getTransactionStatus.unit.test.d.ts +2 -0
- package/lib/getTransactionStatus.unit.test.d.ts.map +1 -0
- package/lib/getTransactionStatus.unit.test.js +54 -0
- package/lib/getTransactionStatus.unit.test.js.map +1 -0
- package/lib/hw-getAddress.d.ts +6 -0
- package/lib/hw-getAddress.d.ts.map +1 -0
- package/lib/hw-getAddress.js +37 -0
- package/lib/hw-getAddress.js.map +1 -0
- package/lib/logic.d.ts +88 -0
- package/lib/logic.d.ts.map +1 -0
- package/lib/logic.js +264 -0
- package/lib/logic.js.map +1 -0
- package/lib/logic.unit.test.d.ts +2 -0
- package/lib/logic.unit.test.d.ts.map +1 -0
- package/lib/logic.unit.test.js +40 -0
- package/lib/logic.unit.test.js.map +1 -0
- package/lib/networks.d.ts +3 -0
- package/lib/networks.d.ts.map +1 -0
- package/lib/networks.js +36 -0
- package/lib/networks.js.map +1 -0
- package/lib/postSyncPatch.d.ts +6 -0
- package/lib/postSyncPatch.d.ts.map +1 -0
- package/lib/postSyncPatch.js +35 -0
- package/lib/postSyncPatch.js.map +1 -0
- package/lib/prepareTransaction.d.ts +10 -0
- package/lib/prepareTransaction.d.ts.map +1 -0
- package/lib/prepareTransaction.js +44 -0
- package/lib/prepareTransaction.js.map +1 -0
- package/lib/serialization.d.ts +7 -0
- package/lib/serialization.d.ts.map +1 -0
- package/lib/serialization.js +146 -0
- package/lib/serialization.js.map +1 -0
- package/lib/signOperation.d.ts +9 -0
- package/lib/signOperation.d.ts.map +1 -0
- package/lib/signOperation.js +71 -0
- package/lib/signOperation.js.map +1 -0
- package/lib/signer.d.ts +92 -0
- package/lib/signer.d.ts.map +1 -0
- package/lib/signer.js +3 -0
- package/lib/signer.js.map +1 -0
- package/lib/specs.d.ts +7 -0
- package/lib/specs.d.ts.map +1 -0
- package/lib/specs.js +218 -0
- package/lib/specs.js.map +1 -0
- package/lib/speculos-deviceActions.d.ts +4 -0
- package/lib/speculos-deviceActions.d.ts.map +1 -0
- package/lib/speculos-deviceActions.js +128 -0
- package/lib/speculos-deviceActions.js.map +1 -0
- package/lib/synchronisation.d.ts +6 -0
- package/lib/synchronisation.d.ts.map +1 -0
- package/lib/synchronisation.js +237 -0
- package/lib/synchronisation.js.map +1 -0
- package/lib/synchronisation.unit.test.d.ts +2 -0
- package/lib/synchronisation.unit.test.d.ts.map +1 -0
- package/lib/synchronisation.unit.test.js +129 -0
- package/lib/synchronisation.unit.test.js.map +1 -0
- package/lib/transaction.d.ts +15 -0
- package/lib/transaction.d.ts.map +1 -0
- package/lib/transaction.js +43 -0
- package/lib/transaction.js.map +1 -0
- package/lib/types.d.ts +188 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/types.js +18 -0
- package/lib/types.js.map +1 -0
- package/lib/typhonSerializer.d.ts +17 -0
- package/lib/typhonSerializer.d.ts.map +1 -0
- package/lib/typhonSerializer.js +213 -0
- package/lib/typhonSerializer.js.map +1 -0
- package/lib/utils.d.ts +2 -0
- package/lib/utils.d.ts.map +1 -0
- package/lib/utils.js +8 -0
- package/lib/utils.js.map +1 -0
- package/lib-es/account.d.ts +7 -0
- package/lib-es/account.d.ts.map +1 -0
- package/lib-es/account.js +34 -0
- package/lib-es/account.js.map +1 -0
- package/lib-es/address.unit.test.d.ts +2 -0
- package/lib-es/address.unit.test.d.ts.map +1 -0
- package/lib-es/address.unit.test.js +19 -0
- package/lib-es/address.unit.test.js.map +1 -0
- package/lib-es/api/api-types.d.ts +106 -0
- package/lib-es/api/api-types.d.ts.map +1 -0
- package/lib-es/api/api-types.js +6 -0
- package/lib-es/api/api-types.js.map +1 -0
- package/lib-es/api/getDelegationInfo.d.ts +4 -0
- package/lib-es/api/getDelegationInfo.d.ts.map +1 -0
- package/lib-es/api/getDelegationInfo.js +41 -0
- package/lib-es/api/getDelegationInfo.js.map +1 -0
- package/lib-es/api/getNetworkInfo.d.ts +5 -0
- package/lib-es/api/getNetworkInfo.d.ts.map +1 -0
- package/lib-es/api/getNetworkInfo.js +39 -0
- package/lib-es/api/getNetworkInfo.js.map +1 -0
- package/lib-es/api/getPools.d.ts +5 -0
- package/lib-es/api/getPools.d.ts.map +1 -0
- package/lib-es/api/getPools.js +37 -0
- package/lib-es/api/getPools.js.map +1 -0
- package/lib-es/api/getTransactions.d.ts +10 -0
- package/lib-es/api/getTransactions.d.ts.map +1 -0
- package/lib-es/api/getTransactions.js +132 -0
- package/lib-es/api/getTransactions.js.map +1 -0
- package/lib-es/api/submitTransaction.d.ts +7 -0
- package/lib-es/api/submitTransaction.d.ts.map +1 -0
- package/lib-es/api/submitTransaction.js +27 -0
- package/lib-es/api/submitTransaction.js.map +1 -0
- package/lib-es/bridge/index.d.ts +11 -0
- package/lib-es/bridge/index.d.ts.map +1 -0
- package/lib-es/bridge/index.js +61 -0
- package/lib-es/bridge/index.js.map +1 -0
- package/lib-es/bridge.integration.test.d.ts +4 -0
- package/lib-es/bridge.integration.test.d.ts.map +1 -0
- package/lib-es/bridge.integration.test.js +107 -0
- package/lib-es/bridge.integration.test.js.map +1 -0
- package/lib-es/broadcast.d.ts +8 -0
- package/lib-es/broadcast.d.ts.map +1 -0
- package/lib-es/broadcast.js +24 -0
- package/lib-es/broadcast.js.map +1 -0
- package/lib-es/buildOptimisticOperation.d.ts +4 -0
- package/lib-es/buildOptimisticOperation.d.ts.map +1 -0
- package/lib-es/buildOptimisticOperation.js +118 -0
- package/lib-es/buildOptimisticOperation.js.map +1 -0
- package/lib-es/buildSubAccounts.d.ts +26 -0
- package/lib-es/buildSubAccounts.d.ts.map +1 -0
- package/lib-es/buildSubAccounts.js +114 -0
- package/lib-es/buildSubAccounts.js.map +1 -0
- package/lib-es/buildTransaction.d.ts +11 -0
- package/lib-es/buildTransaction.d.ts.map +1 -0
- package/lib-es/buildTransaction.js +335 -0
- package/lib-es/buildTransaction.js.map +1 -0
- package/lib-es/cli-transaction.d.ts +18 -0
- package/lib-es/cli-transaction.d.ts.map +1 -0
- package/lib-es/cli-transaction.js +60 -0
- package/lib-es/cli-transaction.js.map +1 -0
- package/lib-es/constants.d.ts +8 -0
- package/lib-es/constants.d.ts.map +1 -0
- package/lib-es/constants.js +9 -0
- package/lib-es/constants.js.map +1 -0
- package/lib-es/createTransaction.d.ts +9 -0
- package/lib-es/createTransaction.d.ts.map +1 -0
- package/lib-es/createTransaction.js +16 -0
- package/lib-es/createTransaction.js.map +1 -0
- package/lib-es/datasets/rawAccount.1.d.ts +3 -0
- package/lib-es/datasets/rawAccount.1.d.ts.map +1 -0
- package/lib-es/datasets/rawAccount.1.js +17 -0
- package/lib-es/datasets/rawAccount.1.js.map +1 -0
- package/lib-es/datasets/scanAccounts.d.ts +5 -0
- package/lib-es/datasets/scanAccounts.d.ts.map +1 -0
- package/lib-es/datasets/scanAccounts.js +24 -0
- package/lib-es/datasets/scanAccounts.js.map +1 -0
- package/lib-es/deviceTransactionConfig.d.ts +11 -0
- package/lib-es/deviceTransactionConfig.d.ts.map +1 -0
- package/lib-es/deviceTransactionConfig.js +101 -0
- package/lib-es/deviceTransactionConfig.js.map +1 -0
- package/lib-es/errors.d.ts +22 -0
- package/lib-es/errors.d.ts.map +1 -0
- package/lib-es/errors.js +15 -0
- package/lib-es/errors.js.map +1 -0
- package/lib-es/estimateMaxSpendable.d.ts +9 -0
- package/lib-es/estimateMaxSpendable.d.ts.map +1 -0
- package/lib-es/estimateMaxSpendable.js +49 -0
- package/lib-es/estimateMaxSpendable.js.map +1 -0
- package/lib-es/getTransactionStatus.d.ts +4 -0
- package/lib-es/getTransactionStatus.d.ts.map +1 -0
- package/lib-es/getTransactionStatus.js +210 -0
- package/lib-es/getTransactionStatus.js.map +1 -0
- package/lib-es/getTransactionStatus.unit.test.d.ts +2 -0
- package/lib-es/getTransactionStatus.unit.test.d.ts.map +1 -0
- package/lib-es/getTransactionStatus.unit.test.js +49 -0
- package/lib-es/getTransactionStatus.unit.test.js.map +1 -0
- package/lib-es/hw-getAddress.d.ts +6 -0
- package/lib-es/hw-getAddress.d.ts.map +1 -0
- package/lib-es/hw-getAddress.js +35 -0
- package/lib-es/hw-getAddress.js.map +1 -0
- package/lib-es/logic.d.ts +88 -0
- package/lib-es/logic.d.ts.map +1 -0
- package/lib-es/logic.js +236 -0
- package/lib-es/logic.js.map +1 -0
- package/lib-es/logic.unit.test.d.ts +2 -0
- package/lib-es/logic.unit.test.d.ts.map +1 -0
- package/lib-es/logic.unit.test.js +35 -0
- package/lib-es/logic.unit.test.js.map +1 -0
- package/lib-es/networks.d.ts +3 -0
- package/lib-es/networks.d.ts.map +1 -0
- package/lib-es/networks.js +32 -0
- package/lib-es/networks.js.map +1 -0
- package/lib-es/postSyncPatch.d.ts +6 -0
- package/lib-es/postSyncPatch.d.ts.map +1 -0
- package/lib-es/postSyncPatch.js +31 -0
- package/lib-es/postSyncPatch.js.map +1 -0
- package/lib-es/prepareTransaction.d.ts +10 -0
- package/lib-es/prepareTransaction.d.ts.map +1 -0
- package/lib-es/prepareTransaction.js +40 -0
- package/lib-es/prepareTransaction.js.map +1 -0
- package/lib-es/serialization.d.ts +7 -0
- package/lib-es/serialization.d.ts.map +1 -0
- package/lib-es/serialization.js +139 -0
- package/lib-es/serialization.js.map +1 -0
- package/lib-es/signOperation.d.ts +9 -0
- package/lib-es/signOperation.d.ts.map +1 -0
- package/lib-es/signOperation.js +64 -0
- package/lib-es/signOperation.js.map +1 -0
- package/lib-es/signer.d.ts +92 -0
- package/lib-es/signer.d.ts.map +1 -0
- package/lib-es/signer.js +2 -0
- package/lib-es/signer.js.map +1 -0
- package/lib-es/specs.d.ts +7 -0
- package/lib-es/specs.d.ts.map +1 -0
- package/lib-es/specs.js +213 -0
- package/lib-es/specs.js.map +1 -0
- package/lib-es/speculos-deviceActions.d.ts +4 -0
- package/lib-es/speculos-deviceActions.d.ts.map +1 -0
- package/lib-es/speculos-deviceActions.js +125 -0
- package/lib-es/speculos-deviceActions.js.map +1 -0
- package/lib-es/synchronisation.d.ts +6 -0
- package/lib-es/synchronisation.d.ts.map +1 -0
- package/lib-es/synchronisation.js +230 -0
- package/lib-es/synchronisation.js.map +1 -0
- package/lib-es/synchronisation.unit.test.d.ts +2 -0
- package/lib-es/synchronisation.unit.test.d.ts.map +1 -0
- package/lib-es/synchronisation.unit.test.js +124 -0
- package/lib-es/synchronisation.unit.test.js.map +1 -0
- package/lib-es/transaction.d.ts +15 -0
- package/lib-es/transaction.d.ts.map +1 -0
- package/lib-es/transaction.js +37 -0
- package/lib-es/transaction.js.map +1 -0
- package/lib-es/types.d.ts +188 -0
- package/lib-es/types.d.ts.map +1 -0
- package/lib-es/types.js +15 -0
- package/lib-es/types.js.map +1 -0
- package/lib-es/typhonSerializer.d.ts +17 -0
- package/lib-es/typhonSerializer.d.ts.map +1 -0
- package/lib-es/typhonSerializer.js +207 -0
- package/lib-es/typhonSerializer.js.map +1 -0
- package/lib-es/utils.d.ts +2 -0
- package/lib-es/utils.d.ts.map +1 -0
- package/lib-es/utils.js +5 -0
- package/lib-es/utils.js.map +1 -0
- package/package.json +91 -0
- package/src/account.ts +39 -0
- package/src/address.unit.test.ts +32 -0
- package/src/api/api-types.ts +115 -0
- package/src/api/getDelegationInfo.ts +39 -0
- package/src/api/getNetworkInfo.ts +34 -0
- package/src/api/getPools.ts +35 -0
- package/src/api/getTransactions.ts +218 -0
- package/src/api/submitTransaction.ts +24 -0
- package/src/bridge/index.ts +69 -0
- package/src/bridge.integration.test.ts +113 -0
- package/src/broadcast.ts +21 -0
- package/src/buildOptimisticOperation.ts +181 -0
- package/src/buildSubAccounts.ts +170 -0
- package/src/buildTransaction.ts +435 -0
- package/src/cli-transaction.ts +82 -0
- package/src/constants.ts +10 -0
- package/src/createTransaction.ts +18 -0
- package/src/datasets/rawAccount.1.ts +19 -0
- package/src/datasets/scanAccounts.ts +23 -0
- package/src/deviceTransactionConfig.ts +128 -0
- package/src/errors.ts +18 -0
- package/src/estimateMaxSpendable.ts +61 -0
- package/src/getTransactionStatus.ts +251 -0
- package/src/getTransactionStatus.unit.test.ts +45 -0
- package/src/hw-getAddress.ts +37 -0
- package/src/logic.ts +330 -0
- package/src/logic.unit.test.ts +39 -0
- package/src/networks.ts +33 -0
- package/src/postSyncPatch.ts +42 -0
- package/src/prepareTransaction.ts +41 -0
- package/src/serialization.ts +219 -0
- package/src/signOperation.ts +82 -0
- package/src/signer.ts +98 -0
- package/src/specs.ts +253 -0
- package/src/speculos-deviceActions.ts +132 -0
- package/src/synchronisation.ts +347 -0
- package/src/synchronisation.unit.test.ts +137 -0
- package/src/transaction.ts +66 -0
- package/src/types.ts +227 -0
- package/src/typhonSerializer.ts +259 -0
- package/src/utils.ts +4 -0
- package/tsconfig.json +12 -0
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import BigNumber from "bignumber.js";
|
|
2
|
+
import { OperationType } from "@ledgerhq/types-live";
|
|
3
|
+
import { HashType } from "@stricahq/typhonjs/dist/types";
|
|
4
|
+
import { encodeOperationId } from "@ledgerhq/coin-framework/operation";
|
|
5
|
+
import { formatCurrencyUnit } from "@ledgerhq/coin-framework/currencies";
|
|
6
|
+
import ShelleyTypeAddress from "@stricahq/typhonjs/dist/address/ShelleyTypeAddress";
|
|
7
|
+
import { types as TyphonTypes, Transaction as TyphonTransaction } from "@stricahq/typhonjs";
|
|
8
|
+
import { getAccountStakeCredential, getOperationType } from "./logic";
|
|
9
|
+
import { MEMO_LABEL } from "./constants";
|
|
10
|
+
import {
|
|
11
|
+
CardanoAccount,
|
|
12
|
+
CardanoOperation,
|
|
13
|
+
CardanoOperationExtra,
|
|
14
|
+
CardanoResources,
|
|
15
|
+
Transaction,
|
|
16
|
+
} from "./types";
|
|
17
|
+
|
|
18
|
+
export const buildOptimisticOperation = (
|
|
19
|
+
account: CardanoAccount,
|
|
20
|
+
unsignedTransaction: TyphonTransaction,
|
|
21
|
+
transaction: Transaction,
|
|
22
|
+
): CardanoOperation => {
|
|
23
|
+
const cardanoResources = account.cardanoResources as CardanoResources;
|
|
24
|
+
const accountCreds = new Set(
|
|
25
|
+
[...cardanoResources.externalCredentials, ...cardanoResources.internalCredentials].map(
|
|
26
|
+
cred => cred.key,
|
|
27
|
+
),
|
|
28
|
+
);
|
|
29
|
+
const stakeCredential = getAccountStakeCredential(account.xpub as string, account.index);
|
|
30
|
+
const protocolParams = account.cardanoResources.protocolParams;
|
|
31
|
+
|
|
32
|
+
const accountInput = unsignedTransaction
|
|
33
|
+
.getInputs()
|
|
34
|
+
.reduce(
|
|
35
|
+
(total, i) =>
|
|
36
|
+
accountCreds.has(i.address.paymentCredential.hash) ? total.plus(i.amount) : total.plus(0),
|
|
37
|
+
new BigNumber(0),
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
const accountOutput = unsignedTransaction
|
|
41
|
+
.getOutputs()
|
|
42
|
+
.reduce(
|
|
43
|
+
(total, o) =>
|
|
44
|
+
o.address instanceof ShelleyTypeAddress &&
|
|
45
|
+
accountCreds.has(o.address.paymentCredential.hash)
|
|
46
|
+
? total.plus(o.amount)
|
|
47
|
+
: total.plus(0),
|
|
48
|
+
new BigNumber(0),
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
const txCertificates = unsignedTransaction.getCertificates();
|
|
52
|
+
const stakeRegistrationCertificates = txCertificates.filter(
|
|
53
|
+
c => c.certType === TyphonTypes.CertificateType.STAKE_REGISTRATION,
|
|
54
|
+
);
|
|
55
|
+
const stakeDeRegistrationCertificates = txCertificates.filter(
|
|
56
|
+
c => c.certType === TyphonTypes.CertificateType.STAKE_DE_REGISTRATION,
|
|
57
|
+
);
|
|
58
|
+
const txWithdrawals = unsignedTransaction.getWithdrawals();
|
|
59
|
+
|
|
60
|
+
const transactionHash = unsignedTransaction.getTransactionHash().toString("hex");
|
|
61
|
+
const auxiliaryData = unsignedTransaction.getAuxiliaryData();
|
|
62
|
+
const extra: CardanoOperationExtra = {};
|
|
63
|
+
if (auxiliaryData) {
|
|
64
|
+
const memoMetadata = auxiliaryData.metadata.find(m => m.label === MEMO_LABEL);
|
|
65
|
+
if (memoMetadata && memoMetadata.data instanceof Map) {
|
|
66
|
+
const msg = memoMetadata.data.get("msg");
|
|
67
|
+
if (Array.isArray(msg) && msg.length) {
|
|
68
|
+
extra.memo = msg.join(", ");
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
let operationValue = accountOutput.minus(accountInput);
|
|
74
|
+
|
|
75
|
+
if (stakeRegistrationCertificates.length) {
|
|
76
|
+
const walletRegistration = stakeRegistrationCertificates.find(
|
|
77
|
+
c =>
|
|
78
|
+
c.stakeCredential.type === HashType.ADDRESS &&
|
|
79
|
+
c.stakeCredential.hash === stakeCredential.key,
|
|
80
|
+
);
|
|
81
|
+
if (walletRegistration) {
|
|
82
|
+
extra.deposit = formatCurrencyUnit(
|
|
83
|
+
account.currency.units[0],
|
|
84
|
+
new BigNumber(protocolParams.stakeKeyDeposit),
|
|
85
|
+
{
|
|
86
|
+
showCode: true,
|
|
87
|
+
disableRounding: true,
|
|
88
|
+
},
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (stakeDeRegistrationCertificates.length) {
|
|
94
|
+
const walletDeRegistration = stakeDeRegistrationCertificates.find(
|
|
95
|
+
c =>
|
|
96
|
+
c.stakeCredential.type === HashType.ADDRESS &&
|
|
97
|
+
c.stakeCredential.hash === stakeCredential.key,
|
|
98
|
+
);
|
|
99
|
+
if (walletDeRegistration) {
|
|
100
|
+
operationValue = operationValue.minus(protocolParams.stakeKeyDeposit);
|
|
101
|
+
extra.refund = formatCurrencyUnit(
|
|
102
|
+
account.currency.units[0],
|
|
103
|
+
new BigNumber(protocolParams.stakeKeyDeposit),
|
|
104
|
+
{
|
|
105
|
+
showCode: true,
|
|
106
|
+
disableRounding: true,
|
|
107
|
+
},
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (txWithdrawals && txWithdrawals.length) {
|
|
113
|
+
const walletWithdraw = txWithdrawals.find(
|
|
114
|
+
w =>
|
|
115
|
+
w.rewardAccount.stakeCredential.type === HashType.ADDRESS &&
|
|
116
|
+
w.rewardAccount.stakeCredential.hash === stakeCredential.key,
|
|
117
|
+
);
|
|
118
|
+
if (walletWithdraw) {
|
|
119
|
+
operationValue = operationValue.minus(walletWithdraw.amount);
|
|
120
|
+
extra.rewards = formatCurrencyUnit(
|
|
121
|
+
account.currency.units[0],
|
|
122
|
+
new BigNumber(walletWithdraw.amount),
|
|
123
|
+
{
|
|
124
|
+
showCode: true,
|
|
125
|
+
disableRounding: true,
|
|
126
|
+
},
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const opType: OperationType = txCertificates.find(
|
|
132
|
+
c => c.certType === TyphonTypes.CertificateType.STAKE_DELEGATION,
|
|
133
|
+
)
|
|
134
|
+
? "DELEGATE"
|
|
135
|
+
: txCertificates.find(c => c.certType === TyphonTypes.CertificateType.STAKE_DE_REGISTRATION)
|
|
136
|
+
? "UNDELEGATE"
|
|
137
|
+
: getOperationType({
|
|
138
|
+
valueChange: operationValue,
|
|
139
|
+
fees: unsignedTransaction.getFee(),
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
const op: CardanoOperation = {
|
|
143
|
+
id: encodeOperationId(account.id, transactionHash, opType),
|
|
144
|
+
hash: transactionHash,
|
|
145
|
+
type: opType,
|
|
146
|
+
value: operationValue.absoluteValue(),
|
|
147
|
+
fee: unsignedTransaction.getFee(),
|
|
148
|
+
blockHash: undefined,
|
|
149
|
+
blockHeight: null,
|
|
150
|
+
senders: unsignedTransaction.getInputs().map(i => i.address.getBech32()),
|
|
151
|
+
recipients: unsignedTransaction.getOutputs().map(o => o.address.getBech32()),
|
|
152
|
+
accountId: account.id,
|
|
153
|
+
date: new Date(),
|
|
154
|
+
extra,
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
const tokenAccount = transaction.subAccountId
|
|
158
|
+
? account.subAccounts?.find(ta => ta.id === transaction.subAccountId)
|
|
159
|
+
: null;
|
|
160
|
+
|
|
161
|
+
if (tokenAccount && opType === "OUT") {
|
|
162
|
+
op.subOperations = [
|
|
163
|
+
{
|
|
164
|
+
id: encodeOperationId(tokenAccount.id, transactionHash, opType),
|
|
165
|
+
hash: transactionHash,
|
|
166
|
+
type: opType,
|
|
167
|
+
value: transaction.useAllAmount ? tokenAccount.balance : transaction.amount,
|
|
168
|
+
fee: transaction.fees as BigNumber,
|
|
169
|
+
blockHash: undefined,
|
|
170
|
+
blockHeight: null,
|
|
171
|
+
senders: unsignedTransaction.getInputs().map(i => i.address.getBech32()),
|
|
172
|
+
recipients: unsignedTransaction.getOutputs().map(o => o.address.getBech32()),
|
|
173
|
+
accountId: tokenAccount.id,
|
|
174
|
+
date: new Date(),
|
|
175
|
+
extra: {},
|
|
176
|
+
},
|
|
177
|
+
];
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
return op;
|
|
181
|
+
};
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import {
|
|
2
|
+
decodeTokenAccountId,
|
|
3
|
+
emptyHistoryCache,
|
|
4
|
+
encodeTokenAccountId,
|
|
5
|
+
} from "@ledgerhq/coin-framework/account/index";
|
|
6
|
+
import keyBy from "lodash/keyBy";
|
|
7
|
+
import groupBy from "lodash/groupBy";
|
|
8
|
+
import BigNumber from "bignumber.js";
|
|
9
|
+
import { findTokenById } from "@ledgerhq/cryptoassets";
|
|
10
|
+
import { utils as TyphonUtils } from "@stricahq/typhonjs";
|
|
11
|
+
import type { Account, TokenAccount } from "@ledgerhq/types-live";
|
|
12
|
+
import type { CryptoCurrency } from "@ledgerhq/types-cryptoassets";
|
|
13
|
+
import { mergeOps } from "@ledgerhq/coin-framework/bridge/jsHelpers";
|
|
14
|
+
import { encodeOperationId } from "@ledgerhq/coin-framework/operation";
|
|
15
|
+
import { CardanoOperation, CardanoOperationExtra, PaymentCredential, Token } from "./types";
|
|
16
|
+
import { getAccountChange, getMemoFromTx, isHexString } from "./logic";
|
|
17
|
+
import { APITransaction } from "./api/api-types";
|
|
18
|
+
|
|
19
|
+
export const getTokenAssetId = ({
|
|
20
|
+
policyId,
|
|
21
|
+
assetName,
|
|
22
|
+
}: {
|
|
23
|
+
policyId: string;
|
|
24
|
+
assetName: string;
|
|
25
|
+
}): string => `${policyId}${assetName}`;
|
|
26
|
+
|
|
27
|
+
export const decodeTokenAssetId = (id: string): { policyId: string; assetName: string } => {
|
|
28
|
+
const policyId = id.slice(0, 56);
|
|
29
|
+
const assetName = id.slice(56);
|
|
30
|
+
return { policyId, assetName };
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const encodeTokenCurrencyId = (parentCurrency: CryptoCurrency, assetId: string): string =>
|
|
34
|
+
`${parentCurrency.id}/native/${assetId}`;
|
|
35
|
+
|
|
36
|
+
export const decodeTokenCurrencyId = (
|
|
37
|
+
id: string,
|
|
38
|
+
): { parentCurrencyId: string; type: string; assetId: string } => {
|
|
39
|
+
const [parentCurrencyId, type, assetId] = id.split("/");
|
|
40
|
+
return {
|
|
41
|
+
parentCurrencyId,
|
|
42
|
+
type,
|
|
43
|
+
assetId,
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @returns operations of tokens that are defined in ledgerjs cryptoassets
|
|
49
|
+
*/
|
|
50
|
+
const mapTxToTokenAccountOperation = ({
|
|
51
|
+
parentAccountId,
|
|
52
|
+
parentCurrency,
|
|
53
|
+
newTransactions,
|
|
54
|
+
accountCredentialsMap,
|
|
55
|
+
}: {
|
|
56
|
+
parentAccountId: string;
|
|
57
|
+
parentCurrency: CryptoCurrency;
|
|
58
|
+
newTransactions: Array<APITransaction>;
|
|
59
|
+
accountCredentialsMap: Record<string, PaymentCredential>;
|
|
60
|
+
}): Array<CardanoOperation> => {
|
|
61
|
+
const operations: Array<CardanoOperation> = [];
|
|
62
|
+
|
|
63
|
+
newTransactions.forEach(tx => {
|
|
64
|
+
const accountChange = getAccountChange(tx, accountCredentialsMap);
|
|
65
|
+
accountChange.tokens.forEach(token => {
|
|
66
|
+
const assetId = getTokenAssetId({
|
|
67
|
+
policyId: token.policyId,
|
|
68
|
+
assetName: token.assetName,
|
|
69
|
+
});
|
|
70
|
+
const tokenCurrencyId = encodeTokenCurrencyId(parentCurrency, assetId);
|
|
71
|
+
const tokenCurrency = findTokenById(tokenCurrencyId);
|
|
72
|
+
// skip the unsupported tokens by ledger-live
|
|
73
|
+
if (tokenCurrency === null || tokenCurrency === undefined) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const tokenAccountId = encodeTokenAccountId(parentAccountId, tokenCurrency);
|
|
78
|
+
|
|
79
|
+
const tokenOperationType = token.amount.lt(0) ? "OUT" : "IN";
|
|
80
|
+
const memo = getMemoFromTx(tx);
|
|
81
|
+
const extra: CardanoOperationExtra = {};
|
|
82
|
+
if (memo) {
|
|
83
|
+
extra.memo = memo;
|
|
84
|
+
}
|
|
85
|
+
const operation: CardanoOperation = {
|
|
86
|
+
accountId: tokenAccountId,
|
|
87
|
+
id: encodeOperationId(tokenAccountId, tx.hash, tokenOperationType),
|
|
88
|
+
hash: tx.hash,
|
|
89
|
+
type: tokenOperationType,
|
|
90
|
+
fee: new BigNumber(tx.fees),
|
|
91
|
+
value: token.amount.absoluteValue(),
|
|
92
|
+
senders: tx.inputs.map(i =>
|
|
93
|
+
isHexString(i.address) ? TyphonUtils.getAddressFromHex(i.address).getBech32() : i.address,
|
|
94
|
+
),
|
|
95
|
+
recipients: tx.outputs.map(o =>
|
|
96
|
+
isHexString(o.address) ? TyphonUtils.getAddressFromHex(o.address).getBech32() : o.address,
|
|
97
|
+
),
|
|
98
|
+
blockHeight: tx.blockHeight,
|
|
99
|
+
date: new Date(tx.timestamp),
|
|
100
|
+
extra,
|
|
101
|
+
blockHash: undefined,
|
|
102
|
+
};
|
|
103
|
+
operations.push(operation);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
return operations;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
export function buildSubAccounts({
|
|
111
|
+
initialAccount,
|
|
112
|
+
parentAccountId,
|
|
113
|
+
parentCurrency,
|
|
114
|
+
newTransactions,
|
|
115
|
+
tokens,
|
|
116
|
+
accountCredentialsMap,
|
|
117
|
+
}: {
|
|
118
|
+
initialAccount: Account | undefined;
|
|
119
|
+
parentAccountId: string;
|
|
120
|
+
parentCurrency: CryptoCurrency;
|
|
121
|
+
newTransactions: Array<APITransaction>;
|
|
122
|
+
tokens: Array<Token>;
|
|
123
|
+
accountCredentialsMap: Record<string, PaymentCredential>;
|
|
124
|
+
}): Array<TokenAccount> {
|
|
125
|
+
const tokenAccountsById: Record<string, TokenAccount> = {};
|
|
126
|
+
|
|
127
|
+
if (initialAccount && initialAccount.subAccounts) {
|
|
128
|
+
for (const existingAccount of initialAccount.subAccounts) {
|
|
129
|
+
if (existingAccount.type === "TokenAccount") {
|
|
130
|
+
tokenAccountsById[existingAccount.id] = existingAccount;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const tokenOperations: Array<CardanoOperation> = mapTxToTokenAccountOperation({
|
|
136
|
+
parentAccountId,
|
|
137
|
+
parentCurrency,
|
|
138
|
+
newTransactions: newTransactions,
|
|
139
|
+
accountCredentialsMap,
|
|
140
|
+
});
|
|
141
|
+
const tokenOperationsByAccId = groupBy(tokenOperations, o => o.accountId);
|
|
142
|
+
const tokensBalanceByAssetId = keyBy(tokens, t => getTokenAssetId(t));
|
|
143
|
+
for (const tokenAccountId in tokenOperationsByAccId) {
|
|
144
|
+
const initialTokenAccount = tokenAccountsById[tokenAccountId];
|
|
145
|
+
const oldOperations = initialTokenAccount?.operations || [];
|
|
146
|
+
const newOperations = tokenOperationsByAccId[tokenAccountId] || [];
|
|
147
|
+
const operations = mergeOps(oldOperations, newOperations);
|
|
148
|
+
const { token: tokenCurrency } = decodeTokenAccountId(tokenAccountId);
|
|
149
|
+
if (tokenCurrency) {
|
|
150
|
+
const accountBalance =
|
|
151
|
+
tokensBalanceByAssetId[tokenCurrency.contractAddress]?.amount || new BigNumber(0);
|
|
152
|
+
const tokenAccount: TokenAccount = {
|
|
153
|
+
type: "TokenAccount",
|
|
154
|
+
id: tokenAccountId,
|
|
155
|
+
parentId: parentAccountId,
|
|
156
|
+
token: tokenCurrency,
|
|
157
|
+
balance: accountBalance,
|
|
158
|
+
spendableBalance: accountBalance,
|
|
159
|
+
creationDate: operations.length > 0 ? operations[operations.length - 1].date : new Date(),
|
|
160
|
+
operationsCount: operations.length,
|
|
161
|
+
operations,
|
|
162
|
+
pendingOperations: [],
|
|
163
|
+
balanceHistoryCache: emptyHistoryCache,
|
|
164
|
+
swapHistory: [],
|
|
165
|
+
};
|
|
166
|
+
tokenAccountsById[tokenAccountId] = tokenAccount;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return Object.values(tokenAccountsById);
|
|
170
|
+
}
|