@ledgerhq/coin-internet_computer 1.4.0-nightly.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/.turbo/turbo-build.log +4 -0
- package/.unimportedrc.json +38 -0
- package/CHANGELOG.md +7 -0
- package/LICENSE.txt +21 -0
- package/jest.config.js +9 -0
- package/lib/api/api.d.ts +12 -0
- package/lib/api/api.d.ts.map +1 -0
- package/lib/api/api.js +82 -0
- package/lib/api/api.js.map +1 -0
- package/lib/api/index.d.ts +2 -0
- package/lib/api/index.d.ts.map +1 -0
- package/lib/api/index.js +18 -0
- package/lib/api/index.js.map +1 -0
- package/lib/bridge/bridgeHelpers/account.d.ts +3 -0
- package/lib/bridge/bridgeHelpers/account.d.ts.map +1 -0
- package/lib/bridge/bridgeHelpers/account.js +127 -0
- package/lib/bridge/bridgeHelpers/account.js.map +1 -0
- package/lib/bridge/bridgeHelpers/addresses.d.ts +12 -0
- package/lib/bridge/bridgeHelpers/addresses.d.ts.map +1 -0
- package/lib/bridge/bridgeHelpers/addresses.js +46 -0
- package/lib/bridge/bridgeHelpers/addresses.js.map +1 -0
- package/lib/bridge/bridgeHelpers/fee.d.ts +3 -0
- package/lib/bridge/bridgeHelpers/fee.d.ts.map +1 -0
- package/lib/bridge/bridgeHelpers/fee.js +13 -0
- package/lib/bridge/bridgeHelpers/fee.js.map +1 -0
- package/lib/bridge/bridgeHelpers/icpRosetta/index.d.ts +30 -0
- package/lib/bridge/bridgeHelpers/icpRosetta/index.d.ts.map +1 -0
- package/lib/bridge/bridgeHelpers/icpRosetta/index.js +87 -0
- package/lib/bridge/bridgeHelpers/icpRosetta/index.js.map +1 -0
- package/lib/bridge/bridgeHelpers/icpRosetta/types.d.ts +145 -0
- package/lib/bridge/bridgeHelpers/icpRosetta/types.d.ts.map +1 -0
- package/lib/bridge/bridgeHelpers/icpRosetta/types.js +3 -0
- package/lib/bridge/bridgeHelpers/icpRosetta/types.js.map +1 -0
- package/lib/bridge/bridgeHelpers/icpRosetta/utils.d.ts +17 -0
- package/lib/bridge/bridgeHelpers/icpRosetta/utils.d.ts.map +1 -0
- package/lib/bridge/bridgeHelpers/icpRosetta/utils.js +154 -0
- package/lib/bridge/bridgeHelpers/icpRosetta/utils.js.map +1 -0
- package/lib/bridge/broadcast.d.ts +4 -0
- package/lib/bridge/broadcast.d.ts.map +1 -0
- package/lib/bridge/broadcast.js +21 -0
- package/lib/bridge/broadcast.js.map +1 -0
- package/lib/bridge/buildOptimisticOperation.d.ts +4 -0
- package/lib/bridge/buildOptimisticOperation.d.ts.map +1 -0
- package/lib/bridge/buildOptimisticOperation.js +37 -0
- package/lib/bridge/buildOptimisticOperation.js.map +1 -0
- package/lib/bridge/createTransaction.d.ts +4 -0
- package/lib/bridge/createTransaction.d.ts.map +1 -0
- package/lib/bridge/createTransaction.js +20 -0
- package/lib/bridge/createTransaction.js.map +1 -0
- package/lib/bridge/deviceTransactionConfig.d.ts +11 -0
- package/lib/bridge/deviceTransactionConfig.d.ts.map +1 -0
- package/lib/bridge/deviceTransactionConfig.js +41 -0
- package/lib/bridge/deviceTransactionConfig.js.map +1 -0
- package/lib/bridge/estimateMaxSpendable.d.ts +4 -0
- package/lib/bridge/estimateMaxSpendable.d.ts.map +1 -0
- package/lib/bridge/estimateMaxSpendable.js +28 -0
- package/lib/bridge/estimateMaxSpendable.js.map +1 -0
- package/lib/bridge/getTransactionStatus.d.ts +4 -0
- package/lib/bridge/getTransactionStatus.d.ts.map +1 -0
- package/lib/bridge/getTransactionStatus.js +71 -0
- package/lib/bridge/getTransactionStatus.js.map +1 -0
- package/lib/bridge/index.d.ts +8 -0
- package/lib/bridge/index.d.ts.map +1 -0
- package/lib/bridge/index.js +54 -0
- package/lib/bridge/index.js.map +1 -0
- package/lib/bridge/prepareTransaction.d.ts +4 -0
- package/lib/bridge/prepareTransaction.d.ts.map +1 -0
- package/lib/bridge/prepareTransaction.js +32 -0
- package/lib/bridge/prepareTransaction.js.map +1 -0
- package/lib/bridge/signOperation.d.ts +6 -0
- package/lib/bridge/signOperation.d.ts.map +1 -0
- package/lib/bridge/signOperation.js +53 -0
- package/lib/bridge/signOperation.js.map +1 -0
- package/lib/bridge/transaction.d.ts +14 -0
- package/lib/bridge/transaction.d.ts.map +1 -0
- package/lib/bridge/transaction.js +41 -0
- package/lib/bridge/transaction.js.map +1 -0
- package/lib/common-logic/index.d.ts +2 -0
- package/lib/common-logic/index.d.ts.map +1 -0
- package/lib/common-logic/index.js +12 -0
- package/lib/common-logic/index.js.map +1 -0
- package/lib/common-logic/utils.d.ts +9 -0
- package/lib/common-logic/utils.d.ts.map +1 -0
- package/lib/common-logic/utils.js +44 -0
- package/lib/common-logic/utils.js.map +1 -0
- package/lib/consts.d.ts +6 -0
- package/lib/consts.d.ts.map +1 -0
- package/lib/consts.js +12 -0
- package/lib/consts.js.map +1 -0
- package/lib/errors.d.ts +4 -0
- package/lib/errors.d.ts.map +1 -0
- package/lib/errors.js +9 -0
- package/lib/errors.js.map +1 -0
- package/lib/hw-signMessage.d.ts +12 -0
- package/lib/hw-signMessage.d.ts.map +1 -0
- package/lib/hw-signMessage.js +43 -0
- package/lib/hw-signMessage.js.map +1 -0
- package/lib/signer/getAddress.d.ts +6 -0
- package/lib/signer/getAddress.d.ts.map +1 -0
- package/lib/signer/getAddress.js +35 -0
- package/lib/signer/getAddress.js.map +1 -0
- package/lib/signer/index.d.ts +3 -0
- package/lib/signer/index.d.ts.map +1 -0
- package/lib/signer/index.js +8 -0
- package/lib/signer/index.js.map +1 -0
- package/lib/test/bot-specs.d.ts +7 -0
- package/lib/test/bot-specs.d.ts.map +1 -0
- package/lib/test/bot-specs.js +103 -0
- package/lib/test/bot-specs.js.map +1 -0
- package/lib/test/bridgeDatasetTest.d.ts +4 -0
- package/lib/test/bridgeDatasetTest.d.ts.map +1 -0
- package/lib/test/bridgeDatasetTest.js +141 -0
- package/lib/test/bridgeDatasetTest.js.map +1 -0
- package/lib/test/cli.d.ts +15 -0
- package/lib/test/cli.d.ts.map +1 -0
- package/lib/test/cli.js +27 -0
- package/lib/test/cli.js.map +1 -0
- package/lib/test/index.d.ts +4 -0
- package/lib/test/index.d.ts.map +1 -0
- package/lib/test/index.js +20 -0
- package/lib/test/index.js.map +1 -0
- package/lib/test/speculos-deviceActions.d.ts +4 -0
- package/lib/test/speculos-deviceActions.d.ts.map +1 -0
- package/lib/test/speculos-deviceActions.js +55 -0
- package/lib/test/speculos-deviceActions.js.map +1 -0
- package/lib/types/common.d.ts +21 -0
- package/lib/types/common.d.ts.map +1 -0
- package/lib/types/common.js +3 -0
- package/lib/types/common.js.map +1 -0
- package/lib/types/index.d.ts +3 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index.js +19 -0
- package/lib/types/index.js.map +1 -0
- package/lib/types/signer.d.ts +22 -0
- package/lib/types/signer.d.ts.map +1 -0
- package/lib/types/signer.js +3 -0
- package/lib/types/signer.js.map +1 -0
- package/lib-es/api/api.d.ts +12 -0
- package/lib-es/api/api.d.ts.map +1 -0
- package/lib-es/api/api.js +71 -0
- package/lib-es/api/api.js.map +1 -0
- package/lib-es/api/index.d.ts +2 -0
- package/lib-es/api/index.d.ts.map +1 -0
- package/lib-es/api/index.js +2 -0
- package/lib-es/api/index.js.map +1 -0
- package/lib-es/bridge/bridgeHelpers/account.d.ts +3 -0
- package/lib-es/bridge/bridgeHelpers/account.d.ts.map +1 -0
- package/lib-es/bridge/bridgeHelpers/account.js +120 -0
- package/lib-es/bridge/bridgeHelpers/account.js.map +1 -0
- package/lib-es/bridge/bridgeHelpers/addresses.d.ts +12 -0
- package/lib-es/bridge/bridgeHelpers/addresses.d.ts.map +1 -0
- package/lib-es/bridge/bridgeHelpers/addresses.js +37 -0
- package/lib-es/bridge/bridgeHelpers/addresses.js.map +1 -0
- package/lib-es/bridge/bridgeHelpers/fee.d.ts +3 -0
- package/lib-es/bridge/bridgeHelpers/fee.d.ts.map +1 -0
- package/lib-es/bridge/bridgeHelpers/fee.js +6 -0
- package/lib-es/bridge/bridgeHelpers/fee.js.map +1 -0
- package/lib-es/bridge/bridgeHelpers/icpRosetta/index.d.ts +30 -0
- package/lib-es/bridge/bridgeHelpers/icpRosetta/index.d.ts.map +1 -0
- package/lib-es/bridge/bridgeHelpers/icpRosetta/index.js +75 -0
- package/lib-es/bridge/bridgeHelpers/icpRosetta/index.js.map +1 -0
- package/lib-es/bridge/bridgeHelpers/icpRosetta/types.d.ts +145 -0
- package/lib-es/bridge/bridgeHelpers/icpRosetta/types.d.ts.map +1 -0
- package/lib-es/bridge/bridgeHelpers/icpRosetta/types.js +2 -0
- package/lib-es/bridge/bridgeHelpers/icpRosetta/types.js.map +1 -0
- package/lib-es/bridge/bridgeHelpers/icpRosetta/utils.d.ts +17 -0
- package/lib-es/bridge/bridgeHelpers/icpRosetta/utils.d.ts.map +1 -0
- package/lib-es/bridge/bridgeHelpers/icpRosetta/utils.js +122 -0
- package/lib-es/bridge/bridgeHelpers/icpRosetta/utils.js.map +1 -0
- package/lib-es/bridge/broadcast.d.ts +4 -0
- package/lib-es/bridge/broadcast.d.ts.map +1 -0
- package/lib-es/bridge/broadcast.js +17 -0
- package/lib-es/bridge/broadcast.js.map +1 -0
- package/lib-es/bridge/buildOptimisticOperation.d.ts +4 -0
- package/lib-es/bridge/buildOptimisticOperation.d.ts.map +1 -0
- package/lib-es/bridge/buildOptimisticOperation.js +33 -0
- package/lib-es/bridge/buildOptimisticOperation.js.map +1 -0
- package/lib-es/bridge/createTransaction.d.ts +4 -0
- package/lib-es/bridge/createTransaction.d.ts.map +1 -0
- package/lib-es/bridge/createTransaction.js +13 -0
- package/lib-es/bridge/createTransaction.js.map +1 -0
- package/lib-es/bridge/deviceTransactionConfig.d.ts +11 -0
- package/lib-es/bridge/deviceTransactionConfig.d.ts.map +1 -0
- package/lib-es/bridge/deviceTransactionConfig.js +39 -0
- package/lib-es/bridge/deviceTransactionConfig.js.map +1 -0
- package/lib-es/bridge/estimateMaxSpendable.d.ts +4 -0
- package/lib-es/bridge/estimateMaxSpendable.d.ts.map +1 -0
- package/lib-es/bridge/estimateMaxSpendable.js +21 -0
- package/lib-es/bridge/estimateMaxSpendable.js.map +1 -0
- package/lib-es/bridge/getTransactionStatus.d.ts +4 -0
- package/lib-es/bridge/getTransactionStatus.d.ts.map +1 -0
- package/lib-es/bridge/getTransactionStatus.js +67 -0
- package/lib-es/bridge/getTransactionStatus.js.map +1 -0
- package/lib-es/bridge/index.d.ts +8 -0
- package/lib-es/bridge/index.d.ts.map +1 -0
- package/lib-es/bridge/index.js +47 -0
- package/lib-es/bridge/index.js.map +1 -0
- package/lib-es/bridge/prepareTransaction.d.ts +4 -0
- package/lib-es/bridge/prepareTransaction.d.ts.map +1 -0
- package/lib-es/bridge/prepareTransaction.js +28 -0
- package/lib-es/bridge/prepareTransaction.js.map +1 -0
- package/lib-es/bridge/signOperation.d.ts +6 -0
- package/lib-es/bridge/signOperation.d.ts.map +1 -0
- package/lib-es/bridge/signOperation.js +49 -0
- package/lib-es/bridge/signOperation.js.map +1 -0
- package/lib-es/bridge/transaction.d.ts +14 -0
- package/lib-es/bridge/transaction.d.ts.map +1 -0
- package/lib-es/bridge/transaction.js +33 -0
- package/lib-es/bridge/transaction.js.map +1 -0
- package/lib-es/common-logic/index.d.ts +2 -0
- package/lib-es/common-logic/index.d.ts.map +1 -0
- package/lib-es/common-logic/index.js +2 -0
- package/lib-es/common-logic/index.js.map +1 -0
- package/lib-es/common-logic/utils.d.ts +9 -0
- package/lib-es/common-logic/utils.d.ts.map +1 -0
- package/lib-es/common-logic/utils.js +34 -0
- package/lib-es/common-logic/utils.js.map +1 -0
- package/lib-es/consts.d.ts +6 -0
- package/lib-es/consts.d.ts.map +1 -0
- package/lib-es/consts.js +9 -0
- package/lib-es/consts.js.map +1 -0
- package/lib-es/errors.d.ts +4 -0
- package/lib-es/errors.d.ts.map +1 -0
- package/lib-es/errors.js +6 -0
- package/lib-es/errors.js.map +1 -0
- package/lib-es/hw-signMessage.d.ts +12 -0
- package/lib-es/hw-signMessage.d.ts.map +1 -0
- package/lib-es/hw-signMessage.js +39 -0
- package/lib-es/hw-signMessage.js.map +1 -0
- package/lib-es/signer/getAddress.d.ts +6 -0
- package/lib-es/signer/getAddress.d.ts.map +1 -0
- package/lib-es/signer/getAddress.js +33 -0
- package/lib-es/signer/getAddress.js.map +1 -0
- package/lib-es/signer/index.d.ts +3 -0
- package/lib-es/signer/index.d.ts.map +1 -0
- package/lib-es/signer/index.js +3 -0
- package/lib-es/signer/index.js.map +1 -0
- package/lib-es/test/bot-specs.d.ts +7 -0
- package/lib-es/test/bot-specs.d.ts.map +1 -0
- package/lib-es/test/bot-specs.js +98 -0
- package/lib-es/test/bot-specs.js.map +1 -0
- package/lib-es/test/bridgeDatasetTest.d.ts +4 -0
- package/lib-es/test/bridgeDatasetTest.d.ts.map +1 -0
- package/lib-es/test/bridgeDatasetTest.js +135 -0
- package/lib-es/test/bridgeDatasetTest.js.map +1 -0
- package/lib-es/test/cli.d.ts +15 -0
- package/lib-es/test/cli.d.ts.map +1 -0
- package/lib-es/test/cli.js +21 -0
- package/lib-es/test/cli.js.map +1 -0
- package/lib-es/test/index.d.ts +4 -0
- package/lib-es/test/index.d.ts.map +1 -0
- package/lib-es/test/index.js +4 -0
- package/lib-es/test/index.js.map +1 -0
- package/lib-es/test/speculos-deviceActions.d.ts +4 -0
- package/lib-es/test/speculos-deviceActions.d.ts.map +1 -0
- package/lib-es/test/speculos-deviceActions.js +52 -0
- package/lib-es/test/speculos-deviceActions.js.map +1 -0
- package/lib-es/types/common.d.ts +21 -0
- package/lib-es/types/common.d.ts.map +1 -0
- package/lib-es/types/common.js +2 -0
- package/lib-es/types/common.js.map +1 -0
- package/lib-es/types/index.d.ts +3 -0
- package/lib-es/types/index.d.ts.map +1 -0
- package/lib-es/types/index.js +3 -0
- package/lib-es/types/index.js.map +1 -0
- package/lib-es/types/signer.d.ts +22 -0
- package/lib-es/types/signer.d.ts.map +1 -0
- package/lib-es/types/signer.js +2 -0
- package/lib-es/types/signer.js.map +1 -0
- package/package.json +128 -0
- package/src/api/api.ts +92 -0
- package/src/api/index.ts +1 -0
- package/src/bridge/bridgeHelpers/account.ts +134 -0
- package/src/bridge/bridgeHelpers/addresses.ts +33 -0
- package/src/bridge/bridgeHelpers/fee.ts +6 -0
- package/src/bridge/bridgeHelpers/icpRosetta/index.ts +154 -0
- package/src/bridge/bridgeHelpers/icpRosetta/types.ts +166 -0
- package/src/bridge/bridgeHelpers/icpRosetta/utils.ts +151 -0
- package/src/bridge/broadcast.ts +15 -0
- package/src/bridge/buildOptimisticOperation.ts +32 -0
- package/src/bridge/createTransaction.ts +15 -0
- package/src/bridge/deviceTransactionConfig.ts +53 -0
- package/src/bridge/estimateMaxSpendable.ts +19 -0
- package/src/bridge/getTransactionStatus.ts +75 -0
- package/src/bridge/index.ts +63 -0
- package/src/bridge/prepareTransaction.ts +28 -0
- package/src/bridge/signOperation.ts +59 -0
- package/src/bridge/transaction.ts +61 -0
- package/src/common-logic/index.ts +9 -0
- package/src/common-logic/utils.ts +47 -0
- package/src/consts.ts +11 -0
- package/src/errors.ts +6 -0
- package/src/hw-signMessage.ts +42 -0
- package/src/signer/getAddress.ts +34 -0
- package/src/signer/index.ts +3 -0
- package/src/test/bot-specs.ts +120 -0
- package/src/test/bridgeDatasetTest.ts +141 -0
- package/src/test/cli.ts +36 -0
- package/src/test/index.ts +3 -0
- package/src/test/speculos-deviceActions.ts +61 -0
- package/src/types/common.ts +31 -0
- package/src/types/index.ts +2 -0
- package/src/types/signer.ts +22 -0
- package/tsconfig.json +14 -0
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { Account } from "@ledgerhq/types-live";
|
|
2
|
+
import { Transaction } from "../../../types";
|
|
3
|
+
import { constructionInvoke, getICPRosettaNetworkIdentifier } from "../../../api";
|
|
4
|
+
import {
|
|
5
|
+
ICPRosettaConstructionCombineRequest,
|
|
6
|
+
ICPRosettaConstructionCombineResponse,
|
|
7
|
+
ICPRosettaConstructionHashRequest,
|
|
8
|
+
ICPRosettaConstructionHashResponse,
|
|
9
|
+
ICPRosettaConstructionPayloadsRequest,
|
|
10
|
+
ICPRosettaConstructionPayloadsResponse,
|
|
11
|
+
ICPRosettaConstructionSubmitRequest,
|
|
12
|
+
ICPRosettaConstructionSubmitResponse,
|
|
13
|
+
ICPRosettaConstructionDeriveRequest,
|
|
14
|
+
ICPRosettaConstructionDeriveResponse,
|
|
15
|
+
} from "./types";
|
|
16
|
+
import { ingressExpiry, generateOperations, generateSignaturesPayload } from "./utils";
|
|
17
|
+
import { Cbor } from "@dfinity/agent";
|
|
18
|
+
import { Principal } from "@dfinity/principal";
|
|
19
|
+
import BigNumber from "bignumber.js";
|
|
20
|
+
import { ICP_SEND_TXN_TYPE } from "../../../consts";
|
|
21
|
+
import { SignerContext } from "@ledgerhq/coin-framework/signer";
|
|
22
|
+
import { ICPSigner } from "../../../types";
|
|
23
|
+
|
|
24
|
+
export const getUnsignedTransaction = async (
|
|
25
|
+
transaction: Transaction,
|
|
26
|
+
account: Account,
|
|
27
|
+
): Promise<{
|
|
28
|
+
unsignedTxn: string;
|
|
29
|
+
payloads: ICPRosettaConstructionPayloadsResponse["payloads"];
|
|
30
|
+
}> => {
|
|
31
|
+
const ops = generateOperations(transaction, account);
|
|
32
|
+
const pubkeys = [
|
|
33
|
+
{
|
|
34
|
+
hex_bytes: account.xpub ?? "",
|
|
35
|
+
curve_type: "secp256k1",
|
|
36
|
+
},
|
|
37
|
+
];
|
|
38
|
+
|
|
39
|
+
const reqOpts: ICPRosettaConstructionPayloadsRequest = {
|
|
40
|
+
...getICPRosettaNetworkIdentifier(),
|
|
41
|
+
operations: ops,
|
|
42
|
+
public_keys: pubkeys,
|
|
43
|
+
metadata: {
|
|
44
|
+
memo: parseInt(transaction.memo ?? "0"),
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
const { payloads, unsigned_transaction } = await constructionInvoke<
|
|
48
|
+
ICPRosettaConstructionPayloadsRequest,
|
|
49
|
+
ICPRosettaConstructionPayloadsResponse
|
|
50
|
+
>(reqOpts, "payloads");
|
|
51
|
+
|
|
52
|
+
return { unsignedTxn: unsigned_transaction, payloads };
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export const signICPTransaction = async ({
|
|
56
|
+
signerContext,
|
|
57
|
+
deviceId,
|
|
58
|
+
unsignedTxn,
|
|
59
|
+
path,
|
|
60
|
+
payloads,
|
|
61
|
+
pubkey,
|
|
62
|
+
}: {
|
|
63
|
+
signerContext: SignerContext<ICPSigner>;
|
|
64
|
+
deviceId: string;
|
|
65
|
+
unsignedTxn: string;
|
|
66
|
+
path: string;
|
|
67
|
+
payloads: ICPRosettaConstructionPayloadsResponse["payloads"];
|
|
68
|
+
pubkey: string;
|
|
69
|
+
}): Promise<{
|
|
70
|
+
signatures: { txnSig: string; readSig: string };
|
|
71
|
+
signedTxn: string;
|
|
72
|
+
}> => {
|
|
73
|
+
const decodedTxn: any = Cbor.decode(Buffer.from(unsignedTxn, "hex"));
|
|
74
|
+
const txnReqFromCbor = decodedTxn.updates[0][1];
|
|
75
|
+
const expiry = new ingressExpiry(BigNumber(decodedTxn.ingress_expiries[0].toString()));
|
|
76
|
+
|
|
77
|
+
const submitReq = {
|
|
78
|
+
request_type: "call",
|
|
79
|
+
canister_id: Principal.fromUint8Array(txnReqFromCbor.canister_id),
|
|
80
|
+
method_name: txnReqFromCbor.method_name,
|
|
81
|
+
arg: txnReqFromCbor.arg,
|
|
82
|
+
sender: Principal.fromUint8Array(txnReqFromCbor.sender),
|
|
83
|
+
ingress_expiry: expiry,
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const txnBlobToSign = Cbor.encode({
|
|
87
|
+
content: submitReq,
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
const { r } = await signerContext(deviceId, async signer => {
|
|
91
|
+
const r = await signer.sign(path, Buffer.from(txnBlobToSign), ICP_SEND_TXN_TYPE);
|
|
92
|
+
return { r };
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
const result = {
|
|
96
|
+
signatures: {
|
|
97
|
+
readSig: "",
|
|
98
|
+
txnSig: Buffer.from(r.signatureRS ?? "").toString("hex"),
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
const signaturesPayload = generateSignaturesPayload(result.signatures, payloads, pubkey);
|
|
103
|
+
|
|
104
|
+
const { signed_transaction: signedTxn } = await constructionInvoke<
|
|
105
|
+
ICPRosettaConstructionCombineRequest,
|
|
106
|
+
ICPRosettaConstructionCombineResponse
|
|
107
|
+
>(
|
|
108
|
+
{
|
|
109
|
+
...getICPRosettaNetworkIdentifier(),
|
|
110
|
+
signatures: signaturesPayload,
|
|
111
|
+
unsigned_transaction: unsignedTxn,
|
|
112
|
+
},
|
|
113
|
+
"combine",
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
return { ...result, signedTxn };
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
export const getTxnMetadata = async (signedTxn: string): Promise<{ hash: string }> => {
|
|
120
|
+
const {
|
|
121
|
+
transaction_identifier: { hash },
|
|
122
|
+
} = await constructionInvoke<
|
|
123
|
+
ICPRosettaConstructionHashRequest,
|
|
124
|
+
ICPRosettaConstructionHashResponse
|
|
125
|
+
>({ ...getICPRosettaNetworkIdentifier(), signed_transaction: signedTxn }, "hash");
|
|
126
|
+
|
|
127
|
+
return { hash };
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
export const getTxnExpirationDate = (_unsignedTxn: string): Date => {
|
|
131
|
+
return new Date();
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
export const broadcastTxn = async (signedTxn: string) => {
|
|
135
|
+
await constructionInvoke<
|
|
136
|
+
ICPRosettaConstructionSubmitRequest,
|
|
137
|
+
ICPRosettaConstructionSubmitResponse
|
|
138
|
+
>({ ...getICPRosettaNetworkIdentifier(), signed_transaction: signedTxn }, "submit");
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
export const deriveAddressFromPubkey = async (pubkey: string) => {
|
|
142
|
+
const res = await constructionInvoke<
|
|
143
|
+
ICPRosettaConstructionDeriveRequest,
|
|
144
|
+
ICPRosettaConstructionDeriveResponse
|
|
145
|
+
>(
|
|
146
|
+
{
|
|
147
|
+
...getICPRosettaNetworkIdentifier(),
|
|
148
|
+
public_key: { curve_type: "secp256k1", hex_bytes: pubkey },
|
|
149
|
+
},
|
|
150
|
+
"derive",
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
return res.account_identifier.address;
|
|
154
|
+
};
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { SubmitResponse } from "@dfinity/agent";
|
|
2
|
+
|
|
3
|
+
export interface ICPRosettaBlockHeightResponse {
|
|
4
|
+
current_block_identifier: ICPRosettaBlockIdentifier;
|
|
5
|
+
current_block_timestamp: number;
|
|
6
|
+
genesis_block_identifier: ICPRosettaBlockIdentifier;
|
|
7
|
+
sync_status: ICPRosettaSyncstatus;
|
|
8
|
+
peers: any[];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface ICPRosettaBlockIdentifier {
|
|
12
|
+
index: number;
|
|
13
|
+
hash: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface ICPRosettaGetBalancesResponse {
|
|
17
|
+
block_identifier: ICPRosettaBlockIdentifier;
|
|
18
|
+
balances: ICPRosettaBalance[];
|
|
19
|
+
details?: { error_message: string };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface ICPRosettaBalance {
|
|
23
|
+
value: string;
|
|
24
|
+
currency: ICPRosettaCurrency;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
interface ICPRosettaCurrency {
|
|
28
|
+
symbol: string;
|
|
29
|
+
decimals: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
interface ICPRosettaSyncstatus {
|
|
33
|
+
current_index: number;
|
|
34
|
+
target_index: number;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface ICPRosettaGetTxnsHistoryResponse {
|
|
38
|
+
transactions: {
|
|
39
|
+
block_identifier: ICPRosettaBlockIdentifier;
|
|
40
|
+
transaction: {
|
|
41
|
+
transaction_identifier: ICPRosettaTransactionidentifier;
|
|
42
|
+
operations: ICPRosettaICPRosettaOperation[];
|
|
43
|
+
metadata: {
|
|
44
|
+
block_height: number;
|
|
45
|
+
memo: number;
|
|
46
|
+
timestamp: number;
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
}[];
|
|
50
|
+
total_count: number;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface ICPRosettaICPRosettaOperation {
|
|
54
|
+
operation_identifier: {
|
|
55
|
+
index: number;
|
|
56
|
+
};
|
|
57
|
+
type: string;
|
|
58
|
+
status?: string;
|
|
59
|
+
account: ICPRosettaAccountIdentifier;
|
|
60
|
+
amount: ICPRosettaBalance;
|
|
61
|
+
metadata?: {
|
|
62
|
+
block_index: number;
|
|
63
|
+
transaction_identifier: ICPRosettaTransactionidentifier;
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface ICPRosettaConstructionPayloadsRequest {
|
|
68
|
+
network_identifier: ICPRosettaNetworkIdentifier;
|
|
69
|
+
operations: ICPRosettaICPRosettaOperation[];
|
|
70
|
+
metadata?: ICPRosettaMetadata;
|
|
71
|
+
public_keys: ICPRosettaPublickey[];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export interface ICPRosettaPublickey {
|
|
75
|
+
hex_bytes: string;
|
|
76
|
+
curve_type: string;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export interface ICPRosettaMetadata {
|
|
80
|
+
memo?: number;
|
|
81
|
+
created_at?: number;
|
|
82
|
+
ingress_end?: number;
|
|
83
|
+
ingress_start?: number;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
interface ICPRosettaCurrency {
|
|
87
|
+
symbol: string;
|
|
88
|
+
decimals: number;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
interface ICPRosettaNetworkIdentifier {
|
|
92
|
+
blockchain: string;
|
|
93
|
+
network: string;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export interface ICPRosettaConstructionPayloadsResponse {
|
|
97
|
+
unsigned_transaction: string;
|
|
98
|
+
payloads: {
|
|
99
|
+
account_identifier: ICPRosettaAccountIdentifier;
|
|
100
|
+
hex_bytes: string;
|
|
101
|
+
signature_type: string;
|
|
102
|
+
}[];
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
interface ICPRosettaAccountIdentifier {
|
|
106
|
+
address: string;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export interface ICPRosettaConstructionCombineRequest {
|
|
110
|
+
network_identifier: ICPRosettaNetworkIdentifier;
|
|
111
|
+
unsigned_transaction: string; // cbor
|
|
112
|
+
signatures: ICPRosettaSignature[];
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export interface ICPRosettaSignature {
|
|
116
|
+
signing_payload: {
|
|
117
|
+
account_identifier: ICPRosettaAccountIdentifier;
|
|
118
|
+
hex_bytes: string;
|
|
119
|
+
signature_type: string;
|
|
120
|
+
};
|
|
121
|
+
public_key: ICPRosettaPublickey;
|
|
122
|
+
signature_type: string;
|
|
123
|
+
hex_bytes: string;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export interface ICPRosettaConstructionCombineResponse {
|
|
127
|
+
signed_transaction: string;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export interface ICPRosettaConstructionSubmitRequest {
|
|
131
|
+
network_identifier: ICPRosettaNetworkIdentifier;
|
|
132
|
+
signed_transaction: string;
|
|
133
|
+
}
|
|
134
|
+
export interface ICPRosettaConstructionSubmitResponse {
|
|
135
|
+
transaction_identifier: ICPRosettaTransactionidentifier;
|
|
136
|
+
metadata: {
|
|
137
|
+
operations: ICPRosettaICPRosettaOperation[];
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
interface ICPRosettaTransactionidentifier {
|
|
142
|
+
hash: string;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export interface ICPRosettaConstructionHashRequest {
|
|
146
|
+
network_identifier: ICPRosettaNetworkIdentifier;
|
|
147
|
+
signed_transaction: string;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export interface ICPRosettaConstructionHashResponse {
|
|
151
|
+
transaction_identifier: ICPRosettaTransactionidentifier;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export interface ICPRosettaBroadcastResult extends SubmitResponse {
|
|
155
|
+
txnHash?: string;
|
|
156
|
+
blockHeight?: string;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export interface ICPRosettaConstructionDeriveRequest {
|
|
160
|
+
network_identifier: ICPRosettaNetworkIdentifier;
|
|
161
|
+
public_key: ICPRosettaPublickey;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export interface ICPRosettaConstructionDeriveResponse {
|
|
165
|
+
account_identifier: ICPRosettaAccountIdentifier;
|
|
166
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import * as cbor from "simple-cbor";
|
|
2
|
+
import {
|
|
3
|
+
ICPRosettaConstructionCombineRequest,
|
|
4
|
+
ICPRosettaConstructionPayloadsResponse,
|
|
5
|
+
ICPRosettaICPRosettaOperation,
|
|
6
|
+
} from "./types";
|
|
7
|
+
import { PipeArrayBuffer } from "@dfinity/candid";
|
|
8
|
+
import BigInteger from "big-integer";
|
|
9
|
+
import { Transaction } from "../../../types";
|
|
10
|
+
import { Account } from "@ledgerhq/types-live";
|
|
11
|
+
import { getAddress } from "../addresses";
|
|
12
|
+
import BigNumber from "bignumber.js";
|
|
13
|
+
|
|
14
|
+
export const generateOperations = (
|
|
15
|
+
tr: Transaction,
|
|
16
|
+
a: Account,
|
|
17
|
+
): ICPRosettaICPRosettaOperation[] => {
|
|
18
|
+
const { address } = getAddress(a);
|
|
19
|
+
const currency = {
|
|
20
|
+
symbol: "ICP",
|
|
21
|
+
decimals: 8,
|
|
22
|
+
};
|
|
23
|
+
const type = "TRANSACTION";
|
|
24
|
+
const operations: ICPRosettaICPRosettaOperation[] = [];
|
|
25
|
+
operations.push({
|
|
26
|
+
operation_identifier: {
|
|
27
|
+
index: 0,
|
|
28
|
+
},
|
|
29
|
+
type,
|
|
30
|
+
account: {
|
|
31
|
+
address,
|
|
32
|
+
},
|
|
33
|
+
amount: {
|
|
34
|
+
value: `-${tr.amount}`,
|
|
35
|
+
currency,
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
operations.push({
|
|
40
|
+
operation_identifier: {
|
|
41
|
+
index: 1,
|
|
42
|
+
},
|
|
43
|
+
type,
|
|
44
|
+
account: {
|
|
45
|
+
address: tr.recipient,
|
|
46
|
+
},
|
|
47
|
+
amount: {
|
|
48
|
+
value: `${tr.amount}`,
|
|
49
|
+
currency,
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
operations.push({
|
|
54
|
+
operation_identifier: {
|
|
55
|
+
index: 2,
|
|
56
|
+
},
|
|
57
|
+
type: "FEE",
|
|
58
|
+
account: {
|
|
59
|
+
address,
|
|
60
|
+
},
|
|
61
|
+
amount: {
|
|
62
|
+
value: `-${tr.fees}`,
|
|
63
|
+
currency,
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
return operations;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
function expiryEncode(val: BigNumber): ArrayBuffer {
|
|
71
|
+
let value = BigInteger(val.toString());
|
|
72
|
+
|
|
73
|
+
if (value < BigInteger(0)) {
|
|
74
|
+
throw new Error("Cannot leb encode negative values.");
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const byteLength = (value === BigInteger(0) ? 0 : Math.ceil(Math.log2(Number(value)))) + 1;
|
|
78
|
+
const pipe = new PipeArrayBuffer(new ArrayBuffer(byteLength), 0);
|
|
79
|
+
// eslint-disable-next-line no-constant-condition
|
|
80
|
+
while (true) {
|
|
81
|
+
const i = Number(value.and(BigInteger(0x7f)));
|
|
82
|
+
value = value.divide(BigInteger(0x80));
|
|
83
|
+
if (value.eq(BigInteger(0))) {
|
|
84
|
+
pipe.write(new Uint8Array([i]));
|
|
85
|
+
break;
|
|
86
|
+
} else {
|
|
87
|
+
pipe.write(new Uint8Array([i | 0x80]));
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return pipe.buffer;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export class ingressExpiry {
|
|
95
|
+
value: BigNumber;
|
|
96
|
+
|
|
97
|
+
constructor(value: BigNumber) {
|
|
98
|
+
// Use bigint because it can overflow the maximum number allowed in a double float.
|
|
99
|
+
this.value = value;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
public toCBOR(): cbor.CborValue {
|
|
103
|
+
return cbor.value.u64(this.value.toString(16), 16);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
public toHash(): ArrayBuffer {
|
|
107
|
+
return expiryEncode(this.value);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export const generateSignaturesPayload = (
|
|
112
|
+
signs: { txnSig: string; readSig: string },
|
|
113
|
+
payloads: ICPRosettaConstructionPayloadsResponse["payloads"],
|
|
114
|
+
pubkey: string,
|
|
115
|
+
): ICPRosettaConstructionCombineRequest["signatures"] => {
|
|
116
|
+
const signatures: ICPRosettaConstructionCombineRequest["signatures"] = [];
|
|
117
|
+
const [txnPayload, readStatePayload] = payloads;
|
|
118
|
+
signatures.push({
|
|
119
|
+
signing_payload: {
|
|
120
|
+
account_identifier: {
|
|
121
|
+
address: txnPayload.account_identifier.address,
|
|
122
|
+
},
|
|
123
|
+
hex_bytes: txnPayload.hex_bytes,
|
|
124
|
+
signature_type: txnPayload.signature_type,
|
|
125
|
+
},
|
|
126
|
+
public_key: {
|
|
127
|
+
hex_bytes: pubkey,
|
|
128
|
+
curve_type: "secp256k1",
|
|
129
|
+
},
|
|
130
|
+
signature_type: "ecdsa",
|
|
131
|
+
hex_bytes: signs.txnSig,
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
signatures.push({
|
|
135
|
+
signing_payload: {
|
|
136
|
+
account_identifier: {
|
|
137
|
+
address: readStatePayload.account_identifier.address,
|
|
138
|
+
},
|
|
139
|
+
hex_bytes: readStatePayload.hex_bytes,
|
|
140
|
+
signature_type: readStatePayload.signature_type,
|
|
141
|
+
},
|
|
142
|
+
public_key: {
|
|
143
|
+
hex_bytes: pubkey,
|
|
144
|
+
curve_type: "secp256k1",
|
|
145
|
+
},
|
|
146
|
+
signature_type: "ecdsa",
|
|
147
|
+
hex_bytes: signs.readSig,
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
return signatures;
|
|
151
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { AccountBridge } from "@ledgerhq/types-live";
|
|
2
|
+
import { broadcastTxn } from "./bridgeHelpers/icpRosetta";
|
|
3
|
+
import { Transaction } from "../types";
|
|
4
|
+
|
|
5
|
+
export const broadcast: AccountBridge<Transaction>["broadcast"] = async ({
|
|
6
|
+
signedOperation: { signature, operation },
|
|
7
|
+
}) => {
|
|
8
|
+
// log("debug", "[broadcast] start fn");
|
|
9
|
+
|
|
10
|
+
await broadcastTxn(signature);
|
|
11
|
+
|
|
12
|
+
const result = { ...operation };
|
|
13
|
+
|
|
14
|
+
return result;
|
|
15
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Account, OperationType } from "@ledgerhq/types-live";
|
|
2
|
+
import { encodeOperationId } from "@ledgerhq/coin-framework/operation";
|
|
3
|
+
import { InternetComputerOperation, Transaction } from "../types";
|
|
4
|
+
import { getAddress } from "./bridgeHelpers/addresses";
|
|
5
|
+
|
|
6
|
+
export const buildOptimisticOperation = async (
|
|
7
|
+
account: Account,
|
|
8
|
+
transaction: Transaction,
|
|
9
|
+
hash: string,
|
|
10
|
+
operationType: OperationType = "OUT",
|
|
11
|
+
): Promise<InternetComputerOperation> => {
|
|
12
|
+
const { id: accountId } = account;
|
|
13
|
+
const { recipient, amount } = transaction;
|
|
14
|
+
const { address } = getAddress(account);
|
|
15
|
+
|
|
16
|
+
return {
|
|
17
|
+
id: encodeOperationId(accountId, hash, operationType),
|
|
18
|
+
hash,
|
|
19
|
+
type: "OUT",
|
|
20
|
+
senders: [address],
|
|
21
|
+
recipients: [recipient],
|
|
22
|
+
accountId,
|
|
23
|
+
value: amount.plus(transaction.fees),
|
|
24
|
+
fee: transaction.fees,
|
|
25
|
+
blockHash: null,
|
|
26
|
+
blockHeight: null,
|
|
27
|
+
date: new Date(),
|
|
28
|
+
extra: {
|
|
29
|
+
memo: transaction.memo,
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { AccountBridge } from "@ledgerhq/types-live";
|
|
2
|
+
import { Transaction } from "../types";
|
|
3
|
+
import BigNumber from "bignumber.js";
|
|
4
|
+
import { getEstimatedFees } from "./bridgeHelpers/fee";
|
|
5
|
+
|
|
6
|
+
export const createTransaction: AccountBridge<Transaction>["createTransaction"] = () => {
|
|
7
|
+
// log("debug", "[createTransaction] creating base tx");
|
|
8
|
+
return {
|
|
9
|
+
family: "internet_computer",
|
|
10
|
+
amount: new BigNumber(0),
|
|
11
|
+
fees: getEstimatedFees(),
|
|
12
|
+
recipient: "",
|
|
13
|
+
useAllAmount: false,
|
|
14
|
+
};
|
|
15
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { getCryptoCurrencyById } from "@ledgerhq/cryptoassets/currencies";
|
|
2
|
+
import { log } from "@ledgerhq/logs";
|
|
3
|
+
import { Account, AccountLike } from "@ledgerhq/types-live";
|
|
4
|
+
import { formatCurrencyUnit } from "@ledgerhq/coin-framework/currencies/index";
|
|
5
|
+
import type { CommonDeviceTransactionField } from "@ledgerhq/coin-framework/transaction/common";
|
|
6
|
+
|
|
7
|
+
import { Transaction, TransactionStatus } from "../types";
|
|
8
|
+
import { methodToString } from "../common-logic/utils";
|
|
9
|
+
|
|
10
|
+
const currency = getCryptoCurrencyById("internet_computer");
|
|
11
|
+
|
|
12
|
+
function getDeviceTransactionConfig({
|
|
13
|
+
transaction,
|
|
14
|
+
}: {
|
|
15
|
+
account: AccountLike;
|
|
16
|
+
parentAccount: Account | null | undefined;
|
|
17
|
+
transaction: Transaction;
|
|
18
|
+
status: TransactionStatus;
|
|
19
|
+
}): Array<CommonDeviceTransactionField> {
|
|
20
|
+
const fields: Array<CommonDeviceTransactionField> = [];
|
|
21
|
+
fields.push({
|
|
22
|
+
type: "text",
|
|
23
|
+
label: "Transaction Type",
|
|
24
|
+
value: methodToString(0),
|
|
25
|
+
});
|
|
26
|
+
fields.push({
|
|
27
|
+
type: "text",
|
|
28
|
+
label: "Payment (ICP)",
|
|
29
|
+
value: formatCurrencyUnit(currency.units[0], transaction.amount, {
|
|
30
|
+
showCode: false,
|
|
31
|
+
disableRounding: true,
|
|
32
|
+
}),
|
|
33
|
+
});
|
|
34
|
+
fields.push({
|
|
35
|
+
type: "text",
|
|
36
|
+
label: "Maximum fee (ICP)",
|
|
37
|
+
value: formatCurrencyUnit(currency.units[0], transaction.fees, {
|
|
38
|
+
showCode: false,
|
|
39
|
+
disableRounding: true,
|
|
40
|
+
}),
|
|
41
|
+
});
|
|
42
|
+
fields.push({
|
|
43
|
+
type: "text",
|
|
44
|
+
label: "Memo",
|
|
45
|
+
value: transaction.memo ?? "0",
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
log("debug", `Transaction config ${JSON.stringify(fields)}`);
|
|
49
|
+
|
|
50
|
+
return fields;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export default getDeviceTransactionConfig;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import BigNumber from "bignumber.js";
|
|
2
|
+
import { AccountBridge } from "@ledgerhq/types-live";
|
|
3
|
+
import { getEstimatedFees } from "./bridgeHelpers/fee";
|
|
4
|
+
import { Transaction } from "../types";
|
|
5
|
+
|
|
6
|
+
export const estimateMaxSpendable: AccountBridge<Transaction>["estimateMaxSpendable"] = async ({
|
|
7
|
+
account,
|
|
8
|
+
transaction,
|
|
9
|
+
}) => {
|
|
10
|
+
const balance = account.balance;
|
|
11
|
+
|
|
12
|
+
let maxSpendable = balance.minus(transaction?.fees ?? getEstimatedFees());
|
|
13
|
+
|
|
14
|
+
if (maxSpendable.isLessThan(0)) {
|
|
15
|
+
maxSpendable = new BigNumber(0);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return maxSpendable;
|
|
19
|
+
};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AmountRequired,
|
|
3
|
+
InvalidAddress,
|
|
4
|
+
InvalidAddressBecauseDestinationIsAlsoSource,
|
|
5
|
+
NotEnoughBalance,
|
|
6
|
+
RecipientRequired,
|
|
7
|
+
} from "@ledgerhq/errors";
|
|
8
|
+
import BigNumber from "bignumber.js";
|
|
9
|
+
import { AccountBridge } from "@ledgerhq/types-live";
|
|
10
|
+
import { getAddress, validateAddress, validateMemo } from "./bridgeHelpers/addresses";
|
|
11
|
+
import { Transaction, TransactionStatus } from "../types";
|
|
12
|
+
import { InvalidMemoICP } from "../errors";
|
|
13
|
+
|
|
14
|
+
export const getTransactionStatus: AccountBridge<Transaction>["getTransactionStatus"] = async (
|
|
15
|
+
account,
|
|
16
|
+
transaction,
|
|
17
|
+
) => {
|
|
18
|
+
const errors: TransactionStatus["errors"] = {};
|
|
19
|
+
const warnings: TransactionStatus["warnings"] = {};
|
|
20
|
+
|
|
21
|
+
const { balance } = account;
|
|
22
|
+
const { address } = getAddress(account);
|
|
23
|
+
const { recipient, useAllAmount } = transaction;
|
|
24
|
+
let { amount } = transaction;
|
|
25
|
+
|
|
26
|
+
if (!recipient) {
|
|
27
|
+
errors.recipient = new RecipientRequired();
|
|
28
|
+
} else if (!(await validateAddress(recipient)).isValid) {
|
|
29
|
+
errors.recipient = new InvalidAddress("", {
|
|
30
|
+
currencyName: account.currency.name,
|
|
31
|
+
});
|
|
32
|
+
} else if (recipient.toLowerCase() === address.toLowerCase()) {
|
|
33
|
+
errors.recipient = new InvalidAddressBecauseDestinationIsAlsoSource();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (!(await validateAddress(address)).isValid) {
|
|
37
|
+
errors.sender = new InvalidAddress("", {
|
|
38
|
+
currencyName: account.currency.name,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (!validateMemo(transaction.memo).isValid) {
|
|
43
|
+
errors.transaction = new InvalidMemoICP();
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// This is the worst case scenario (the tx won't cost more than this value)
|
|
47
|
+
const estimatedFees = transaction.fees;
|
|
48
|
+
|
|
49
|
+
let totalSpent: BigNumber;
|
|
50
|
+
|
|
51
|
+
if (useAllAmount) {
|
|
52
|
+
totalSpent = account.spendableBalance;
|
|
53
|
+
amount = totalSpent.minus(estimatedFees);
|
|
54
|
+
if (amount.lte(0) || totalSpent.gt(balance)) {
|
|
55
|
+
errors.amount = new NotEnoughBalance();
|
|
56
|
+
}
|
|
57
|
+
} else {
|
|
58
|
+
totalSpent = amount.plus(estimatedFees);
|
|
59
|
+
if (amount.eq(0)) {
|
|
60
|
+
errors.amount = new AmountRequired();
|
|
61
|
+
} else if (totalSpent.gt(account.spendableBalance)) {
|
|
62
|
+
errors.amount = new NotEnoughBalance();
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// log("debug", "[getTransactionStatus] finish fn");
|
|
67
|
+
|
|
68
|
+
return {
|
|
69
|
+
errors,
|
|
70
|
+
warnings,
|
|
71
|
+
estimatedFees,
|
|
72
|
+
amount,
|
|
73
|
+
totalSpent,
|
|
74
|
+
};
|
|
75
|
+
};
|