@ledgerhq/wallet-api-exchange-module 0.1.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/.eslintignore ADDED
@@ -0,0 +1,2 @@
1
+ lib/
2
+ lib-es/
package/.eslintrc.js ADDED
@@ -0,0 +1,20 @@
1
+ module.exports = {
2
+ env: {
3
+ browser: true,
4
+ es6: true,
5
+ },
6
+ overrides: [
7
+ {
8
+ files: ["src/**/*.test.{ts,tsx}"],
9
+ env: {
10
+ "jest/globals": true,
11
+ },
12
+ plugins: ["jest"],
13
+ },
14
+ ],
15
+ rules: {
16
+ "no-console": ["error", { allow: ["warn", "error"] }],
17
+ "@typescript-eslint/no-empty-function": "off",
18
+ "@typescript-eslint/no-explicit-any": "warn",
19
+ },
20
+ };
@@ -0,0 +1,4 @@
1
+
2
+ > @ledgerhq/wallet-api-exchange-module@0.1.0 build /home/runner/work/ledger-live/ledger-live/libs/exchange-module
3
+ > tsc && tsc -m ES6 --outDir lib-es
4
+
@@ -0,0 +1,5 @@
1
+ {
2
+ "entry": ["src/index.ts"],
3
+ "ignoreUnimported": [],
4
+ "ignoreUnresolved": []
5
+ }
package/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2017-present Ledger https://www.ledger.com/
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/jest.config.js ADDED
@@ -0,0 +1,10 @@
1
+ module.exports = {
2
+ preset: "ts-jest",
3
+ testEnvironment: "jsdom",
4
+ testPathIgnorePatterns: ["lib/", "lib-es/"],
5
+ globals: {
6
+ "ts-jest": {
7
+ isolatedModules: true,
8
+ },
9
+ },
10
+ };
package/lib/index.d.ts ADDED
@@ -0,0 +1,86 @@
1
+ /// <reference types="node" />
2
+ import { CustomModule, Transaction } from "@ledgerhq/wallet-api-client";
3
+ import { ExchangeCompleteParams, ExchangeStartParams } from "./types";
4
+ export * from "./types";
5
+ export declare class ExchangeModule extends CustomModule {
6
+ /**
7
+ * Start the exchange process by generating a nonce on Ledger device
8
+ * @param exchangeType - used by the exchange transport to discern between swap/sell/fund
9
+ *
10
+ * @returns - A transaction ID used to complete the exchange process
11
+ */
12
+ start(exchangeType: ExchangeStartParams["exchangeType"]): Promise<string>;
13
+ /**
14
+ * Complete an exchange swap process by passing by the exchange content and its signature.
15
+ * User will be prompted on its device to approve the swap exchange operation.
16
+ * If the exchange is validated, the transaction is then signed and broadcasted to the network.
17
+ * @param provider - Used to verify the signature
18
+ * @param fromAccountId - Identifier of the account used as a source for the tx or parent account (for "new token")
19
+ * @param toAccountId - Identifier of the account or parent account (for "new token") used as a destination
20
+ * @param swapId - Identifier of the swap used by backend
21
+ * @param rate - Swap rate in the transaction
22
+ * @param tokenCurrency - "new token" used in the transaction, not listed yet in wallet-api list
23
+ * @param transaction - Transaction containing the recipient and amount
24
+ * @param binaryPayload - Blueprint of the data that we'll allow signing
25
+ * @param signature - Ensures the source of the payload
26
+ * @param feesStrategy - Slow / Medium / Fast
27
+ *
28
+ * @returns - The broadcasted transaction hash.
29
+ */
30
+ completeSwap({ provider, fromAccountId, toAccountId, swapId, rate, transaction, binaryPayload, signature, feeStrategy, tokenCurrency, }: {
31
+ provider: string;
32
+ fromAccountId: string;
33
+ toAccountId: string;
34
+ swapId: string;
35
+ rate: number;
36
+ transaction: Transaction;
37
+ binaryPayload: Buffer;
38
+ signature: Buffer;
39
+ feeStrategy: ExchangeCompleteParams["feeStrategy"];
40
+ tokenCurrency?: string;
41
+ }): Promise<string>;
42
+ /**
43
+ * Complete an exchange sell process by passing by the exchange content and its signature.
44
+ * User will be prompted on its device to approve the sell exchange operation.
45
+ * If the exchange is validated, the transaction is then signed and broadcasted to the network.
46
+ * @param provider - Used to verify the signature
47
+ * @param fromAccountId - Identifier of the account used as a source for the tx
48
+ * @param transaction - Transaction containing the recipient and amount
49
+ * @param binaryPayload - Blueprint of the data that we'll allow signing
50
+ * @param signature - Ensures the source of the payload
51
+ * @param feesStrategy - Slow / Medium / Fast
52
+ *
53
+ * @returns - The broadcasted transaction hash.
54
+ */
55
+ completeSell({ provider, fromAccountId, transaction, binaryPayload, signature, feeStrategy, }: {
56
+ provider: string;
57
+ fromAccountId: string;
58
+ transaction: Transaction;
59
+ binaryPayload: Buffer;
60
+ signature: Buffer;
61
+ feeStrategy: ExchangeCompleteParams["feeStrategy"];
62
+ }): Promise<string>;
63
+ /**
64
+ * Complete an exchange fund process by passing by the exchange content and its signature.
65
+ * User will be prompted on its device to approve the fund exchange operation.
66
+ * If the exchange is validated, the transaction is then signed and broadcasted to the network.
67
+ * @param provider - Used to verify the signature
68
+ * @param fromAccountId - Identifier of the account used as a source for the tx
69
+ * @param transaction - Transaction containing the recipient and amount
70
+ * @param binaryPayload - Blueprint of the data that we'll allow signing
71
+ * @param signature - Ensures the source of the payload
72
+ * @param feesStrategy - Slow / Medium / Fast
73
+ *
74
+ * @returns - The broadcasted transaction hash.
75
+ */
76
+ completeFund({ provider, fromAccountId, transaction, binaryPayload, signature, feeStrategy, tokenCurrency, }: {
77
+ provider: string;
78
+ fromAccountId: string;
79
+ transaction: Transaction;
80
+ binaryPayload: Buffer;
81
+ signature: Buffer;
82
+ feeStrategy: ExchangeCompleteParams["feeStrategy"];
83
+ tokenCurrency?: string;
84
+ }): Promise<string>;
85
+ }
86
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,YAAY,EAAE,WAAW,EAAwB,MAAM,6BAA6B,CAAC;AAC9F,OAAO,EACL,sBAAsB,EAEtB,mBAAmB,EAEpB,MAAM,SAAS,CAAC;AAEjB,cAAc,SAAS,CAAC;AAGxB,qBAAa,cAAe,SAAQ,YAAY;IAC9C;;;;;OAKG;IACG,KAAK,CAAC,YAAY,EAAE,mBAAmB,CAAC,cAAc,CAAC;IAW7D;;;;;;;;;;;;;;;;OAgBG;IACG,YAAY,CAAC,EACjB,QAAQ,EACR,aAAa,EACb,WAAW,EACX,MAAM,EACN,IAAI,EACJ,WAAW,EACX,aAAa,EACb,SAAS,EACT,WAAW,EACX,aAAa,GACd,EAAE;QACD,QAAQ,EAAE,MAAM,CAAC;QACjB,aAAa,EAAE,MAAM,CAAC;QACtB,WAAW,EAAE,MAAM,CAAC;QACpB,MAAM,EAAE,MAAM,CAAC;QACf,IAAI,EAAE,MAAM,CAAC;QACb,WAAW,EAAE,WAAW,CAAC;QACzB,aAAa,EAAE,MAAM,CAAC;QACtB,SAAS,EAAE,MAAM,CAAC;QAClB,WAAW,EAAE,sBAAsB,CAAC,aAAa,CAAC,CAAC;QACnD,aAAa,CAAC,EAAE,MAAM,CAAC;KACxB;IAqBD;;;;;;;;;;;;OAYG;IACG,YAAY,CAAC,EACjB,QAAQ,EACR,aAAa,EACb,WAAW,EACX,aAAa,EACb,SAAS,EACT,WAAW,GACZ,EAAE;QACD,QAAQ,EAAE,MAAM,CAAC;QACjB,aAAa,EAAE,MAAM,CAAC;QACtB,WAAW,EAAE,WAAW,CAAC;QACzB,aAAa,EAAE,MAAM,CAAC;QACtB,SAAS,EAAE,MAAM,CAAC;QAClB,WAAW,EAAE,sBAAsB,CAAC,aAAa,CAAC,CAAC;KACpD,GAAG,OAAO,CAAC,MAAM,CAAC;IAiBnB;;;;;;;;;;;;OAYG;IACG,YAAY,CAAC,EACjB,QAAQ,EACR,aAAa,EACb,WAAW,EACX,aAAa,EACb,SAAS,EACT,WAAW,EACX,aAAa,GACd,EAAE;QACD,QAAQ,EAAE,MAAM,CAAC;QACjB,aAAa,EAAE,MAAM,CAAC;QACtB,WAAW,EAAE,WAAW,CAAC;QACzB,aAAa,EAAE,MAAM,CAAC;QACtB,SAAS,EAAE,MAAM,CAAC;QAClB,WAAW,EAAE,sBAAsB,CAAC,aAAa,CAAC,CAAC;QACnD,aAAa,CAAC,EAAE,MAAM,CAAC;KACxB,GAAG,OAAO,CAAC,MAAM,CAAC;CAiBpB"}
package/lib/index.js ADDED
@@ -0,0 +1,137 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
17
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
18
+ return new (P || (P = Promise))(function (resolve, reject) {
19
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
20
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
21
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
22
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
23
+ });
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.ExchangeModule = void 0;
27
+ const wallet_api_client_1 = require("@ledgerhq/wallet-api-client");
28
+ __exportStar(require("./types"), exports);
29
+ // TODO maybe find a better way to type the available custom requests with correct types
30
+ class ExchangeModule extends wallet_api_client_1.CustomModule {
31
+ /**
32
+ * Start the exchange process by generating a nonce on Ledger device
33
+ * @param exchangeType - used by the exchange transport to discern between swap/sell/fund
34
+ *
35
+ * @returns - A transaction ID used to complete the exchange process
36
+ */
37
+ start(exchangeType) {
38
+ return __awaiter(this, void 0, void 0, function* () {
39
+ const result = yield this.request("custom.exchange.start", {
40
+ exchangeType,
41
+ });
42
+ return result.transactionId;
43
+ });
44
+ }
45
+ /**
46
+ * Complete an exchange swap process by passing by the exchange content and its signature.
47
+ * User will be prompted on its device to approve the swap exchange operation.
48
+ * If the exchange is validated, the transaction is then signed and broadcasted to the network.
49
+ * @param provider - Used to verify the signature
50
+ * @param fromAccountId - Identifier of the account used as a source for the tx or parent account (for "new token")
51
+ * @param toAccountId - Identifier of the account or parent account (for "new token") used as a destination
52
+ * @param swapId - Identifier of the swap used by backend
53
+ * @param rate - Swap rate in the transaction
54
+ * @param tokenCurrency - "new token" used in the transaction, not listed yet in wallet-api list
55
+ * @param transaction - Transaction containing the recipient and amount
56
+ * @param binaryPayload - Blueprint of the data that we'll allow signing
57
+ * @param signature - Ensures the source of the payload
58
+ * @param feesStrategy - Slow / Medium / Fast
59
+ *
60
+ * @returns - The broadcasted transaction hash.
61
+ */
62
+ completeSwap({ provider, fromAccountId, toAccountId, swapId, rate, transaction, binaryPayload, signature, feeStrategy, tokenCurrency, }) {
63
+ return __awaiter(this, void 0, void 0, function* () {
64
+ const result = yield this.request("custom.exchange.complete", {
65
+ exchangeType: "SWAP",
66
+ provider,
67
+ fromAccountId,
68
+ toAccountId,
69
+ swapId,
70
+ rate,
71
+ rawTransaction: (0, wallet_api_client_1.serializeTransaction)(transaction),
72
+ hexBinaryPayload: binaryPayload.toString("hex"),
73
+ hexSignature: signature.toString("hex"),
74
+ feeStrategy,
75
+ tokenCurrency,
76
+ });
77
+ return result.transactionHash;
78
+ });
79
+ }
80
+ /**
81
+ * Complete an exchange sell process by passing by the exchange content and its signature.
82
+ * User will be prompted on its device to approve the sell exchange operation.
83
+ * If the exchange is validated, the transaction is then signed and broadcasted to the network.
84
+ * @param provider - Used to verify the signature
85
+ * @param fromAccountId - Identifier of the account used as a source for the tx
86
+ * @param transaction - Transaction containing the recipient and amount
87
+ * @param binaryPayload - Blueprint of the data that we'll allow signing
88
+ * @param signature - Ensures the source of the payload
89
+ * @param feesStrategy - Slow / Medium / Fast
90
+ *
91
+ * @returns - The broadcasted transaction hash.
92
+ */
93
+ completeSell({ provider, fromAccountId, transaction, binaryPayload, signature, feeStrategy, }) {
94
+ return __awaiter(this, void 0, void 0, function* () {
95
+ const result = yield this.request("custom.exchange.complete", {
96
+ exchangeType: "SELL",
97
+ provider,
98
+ fromAccountId,
99
+ rawTransaction: (0, wallet_api_client_1.serializeTransaction)(transaction),
100
+ hexBinaryPayload: binaryPayload.toString("hex"),
101
+ hexSignature: signature.toString("hex"),
102
+ feeStrategy,
103
+ });
104
+ return result.transactionHash;
105
+ });
106
+ }
107
+ /**
108
+ * Complete an exchange fund process by passing by the exchange content and its signature.
109
+ * User will be prompted on its device to approve the fund exchange operation.
110
+ * If the exchange is validated, the transaction is then signed and broadcasted to the network.
111
+ * @param provider - Used to verify the signature
112
+ * @param fromAccountId - Identifier of the account used as a source for the tx
113
+ * @param transaction - Transaction containing the recipient and amount
114
+ * @param binaryPayload - Blueprint of the data that we'll allow signing
115
+ * @param signature - Ensures the source of the payload
116
+ * @param feesStrategy - Slow / Medium / Fast
117
+ *
118
+ * @returns - The broadcasted transaction hash.
119
+ */
120
+ completeFund({ provider, fromAccountId, transaction, binaryPayload, signature, feeStrategy, tokenCurrency, }) {
121
+ return __awaiter(this, void 0, void 0, function* () {
122
+ const result = yield this.request("custom.exchange.complete", {
123
+ exchangeType: "FUND",
124
+ provider,
125
+ fromAccountId,
126
+ rawTransaction: (0, wallet_api_client_1.serializeTransaction)(transaction),
127
+ hexBinaryPayload: binaryPayload.toString("hex"),
128
+ hexSignature: signature.toString("hex"),
129
+ feeStrategy,
130
+ tokenCurrency,
131
+ });
132
+ return result.transactionHash;
133
+ });
134
+ }
135
+ }
136
+ exports.ExchangeModule = ExchangeModule;
137
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,mEAA8F;AAQ9F,0CAAwB;AAExB,wFAAwF;AACxF,MAAa,cAAe,SAAQ,gCAAY;IAC9C;;;;;OAKG;IACG,KAAK,CAAC,YAAiD;;YAC3D,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,OAAO,CAC/B,uBAAuB,EACvB;gBACE,YAAY;aACb,CACF,CAAC;YAEF,OAAO,MAAM,CAAC,aAAa,CAAC;QAC9B,CAAC;KAAA;IAED;;;;;;;;;;;;;;;;OAgBG;IACG,YAAY,CAAC,EACjB,QAAQ,EACR,aAAa,EACb,WAAW,EACX,MAAM,EACN,IAAI,EACJ,WAAW,EACX,aAAa,EACb,SAAS,EACT,WAAW,EACX,aAAa,GAYd;;YACC,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,OAAO,CAC/B,0BAA0B,EAC1B;gBACE,YAAY,EAAE,MAAM;gBACpB,QAAQ;gBACR,aAAa;gBACb,WAAW;gBACX,MAAM;gBACN,IAAI;gBACJ,cAAc,EAAE,IAAA,wCAAoB,EAAC,WAAW,CAAC;gBACjD,gBAAgB,EAAE,aAAa,CAAC,QAAQ,CAAC,KAAK,CAAC;gBAC/C,YAAY,EAAE,SAAS,CAAC,QAAQ,CAAC,KAAK,CAAC;gBACvC,WAAW;gBACX,aAAa;aACd,CACF,CAAC;YAEF,OAAO,MAAM,CAAC,eAAe,CAAC;QAChC,CAAC;KAAA;IAED;;;;;;;;;;;;OAYG;IACG,YAAY,CAAC,EACjB,QAAQ,EACR,aAAa,EACb,WAAW,EACX,aAAa,EACb,SAAS,EACT,WAAW,GAQZ;;YACC,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,OAAO,CAC/B,0BAA0B,EAC1B;gBACE,YAAY,EAAE,MAAM;gBACpB,QAAQ;gBACR,aAAa;gBACb,cAAc,EAAE,IAAA,wCAAoB,EAAC,WAAW,CAAC;gBACjD,gBAAgB,EAAE,aAAa,CAAC,QAAQ,CAAC,KAAK,CAAC;gBAC/C,YAAY,EAAE,SAAS,CAAC,QAAQ,CAAC,KAAK,CAAC;gBACvC,WAAW;aACZ,CACF,CAAC;YAEF,OAAO,MAAM,CAAC,eAAe,CAAC;QAChC,CAAC;KAAA;IAED;;;;;;;;;;;;OAYG;IACG,YAAY,CAAC,EACjB,QAAQ,EACR,aAAa,EACb,WAAW,EACX,aAAa,EACb,SAAS,EACT,WAAW,EACX,aAAa,GASd;;YACC,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,OAAO,CAC/B,0BAA0B,EAC1B;gBACE,YAAY,EAAE,MAAM;gBACpB,QAAQ;gBACR,aAAa;gBACb,cAAc,EAAE,IAAA,wCAAoB,EAAC,WAAW,CAAC;gBACjD,gBAAgB,EAAE,aAAa,CAAC,QAAQ,CAAC,KAAK,CAAC;gBAC/C,YAAY,EAAE,SAAS,CAAC,QAAQ,CAAC,KAAK,CAAC;gBACvC,WAAW;gBACX,aAAa;aACd,CACF,CAAC;YAEF,OAAO,MAAM,CAAC,eAAe,CAAC;QAChC,CAAC;KAAA;CACF;AAxKD,wCAwKC"}
package/lib/types.d.ts ADDED
@@ -0,0 +1,41 @@
1
+ import { RawTransaction } from "@ledgerhq/wallet-api-core";
2
+ export declare enum ExchangeType {
3
+ SWAP = 0,
4
+ SELL = 1,
5
+ FUND = 2,
6
+ SWAP_NG = 3,
7
+ SELL_NG = 4,
8
+ FUND_NG = 5
9
+ }
10
+ export type ExchangeStartParams = {
11
+ exchangeType: "FUND" | "SELL" | "SWAP" | "FUND_NG" | "SELL_NG" | "SWAP_NG";
12
+ };
13
+ export type ExchangeStartResult = {
14
+ transactionId: string;
15
+ };
16
+ export type ExchangeCompleteBaseParams = {
17
+ provider: string;
18
+ fromAccountId: string;
19
+ rawTransaction: RawTransaction;
20
+ hexBinaryPayload: string;
21
+ hexSignature: string;
22
+ feeStrategy: "SLOW" | "MEDIUM" | "FAST" | "CUSTOM";
23
+ tokenCurrency?: string;
24
+ };
25
+ export type ExchangeCompleteFundParams = ExchangeCompleteBaseParams & {
26
+ exchangeType: "FUND";
27
+ };
28
+ export type ExchangeCompleteSellParams = ExchangeCompleteBaseParams & {
29
+ exchangeType: "SELL";
30
+ };
31
+ export type ExchangeCompleteSwapParams = ExchangeCompleteBaseParams & {
32
+ exchangeType: "SWAP";
33
+ toAccountId: string;
34
+ swapId: string;
35
+ rate: number;
36
+ };
37
+ export type ExchangeCompleteParams = ExchangeCompleteFundParams | ExchangeCompleteSellParams | ExchangeCompleteSwapParams;
38
+ export type ExchangeCompleteResult = {
39
+ transactionHash: string;
40
+ };
41
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAE3D,oBAAY,YAAY;IACtB,IAAI,IAAO;IACX,IAAI,IAAO;IACX,IAAI,IAAO;IACX,OAAO,IAAO;IACd,OAAO,IAAO;IACd,OAAO,IAAO;CACf;AAED,MAAM,MAAM,mBAAmB,GAAG;IAChC,YAAY,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,SAAS,GAAG,SAAS,GAAG,SAAS,CAAC;CAC5E,CAAC;AAEF,MAAM,MAAM,mBAAmB,GAAG;IAChC,aAAa,EAAE,MAAM,CAAC;CACvB,CAAC;AAEF,MAAM,MAAM,0BAA0B,GAAG;IACvC,QAAQ,EAAE,MAAM,CAAC;IACjB,aAAa,EAAE,MAAM,CAAC;IACtB,cAAc,EAAE,cAAc,CAAC;IAC/B,gBAAgB,EAAE,MAAM,CAAC;IACzB,YAAY,EAAE,MAAM,CAAC;IACrB,WAAW,EAAE,MAAM,GAAG,QAAQ,GAAG,MAAM,GAAG,QAAQ,CAAC;IACnD,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB,CAAC;AAEF,MAAM,MAAM,0BAA0B,GAAG,0BAA0B,GAAG;IACpE,YAAY,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,0BAA0B,GAAG,0BAA0B,GAAG;IACpE,YAAY,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,0BAA0B,GAAG,0BAA0B,GAAG;IACpE,YAAY,EAAE,MAAM,CAAC;IACrB,WAAW,EAAE,MAAM,CAAC;IACpB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,MAAM,MAAM,sBAAsB,GAC9B,0BAA0B,GAC1B,0BAA0B,GAC1B,0BAA0B,CAAC;AAE/B,MAAM,MAAM,sBAAsB,GAAG;IACnC,eAAe,EAAE,MAAM,CAAC;CACzB,CAAC"}
package/lib/types.js ADDED
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ExchangeType = void 0;
4
+ var ExchangeType;
5
+ (function (ExchangeType) {
6
+ ExchangeType[ExchangeType["SWAP"] = 0] = "SWAP";
7
+ ExchangeType[ExchangeType["SELL"] = 1] = "SELL";
8
+ ExchangeType[ExchangeType["FUND"] = 2] = "FUND";
9
+ ExchangeType[ExchangeType["SWAP_NG"] = 3] = "SWAP_NG";
10
+ ExchangeType[ExchangeType["SELL_NG"] = 4] = "SELL_NG";
11
+ ExchangeType[ExchangeType["FUND_NG"] = 5] = "FUND_NG";
12
+ })(ExchangeType || (exports.ExchangeType = ExchangeType = {}));
13
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";;;AAEA,IAAY,YAOX;AAPD,WAAY,YAAY;IACtB,+CAAW,CAAA;IACX,+CAAW,CAAA;IACX,+CAAW,CAAA;IACX,qDAAc,CAAA;IACd,qDAAc,CAAA;IACd,qDAAc,CAAA;AAChB,CAAC,EAPW,YAAY,4BAAZ,YAAY,QAOvB"}
@@ -0,0 +1,86 @@
1
+ /// <reference types="node" />
2
+ import { CustomModule, Transaction } from "@ledgerhq/wallet-api-client";
3
+ import { ExchangeCompleteParams, ExchangeStartParams } from "./types";
4
+ export * from "./types";
5
+ export declare class ExchangeModule extends CustomModule {
6
+ /**
7
+ * Start the exchange process by generating a nonce on Ledger device
8
+ * @param exchangeType - used by the exchange transport to discern between swap/sell/fund
9
+ *
10
+ * @returns - A transaction ID used to complete the exchange process
11
+ */
12
+ start(exchangeType: ExchangeStartParams["exchangeType"]): Promise<string>;
13
+ /**
14
+ * Complete an exchange swap process by passing by the exchange content and its signature.
15
+ * User will be prompted on its device to approve the swap exchange operation.
16
+ * If the exchange is validated, the transaction is then signed and broadcasted to the network.
17
+ * @param provider - Used to verify the signature
18
+ * @param fromAccountId - Identifier of the account used as a source for the tx or parent account (for "new token")
19
+ * @param toAccountId - Identifier of the account or parent account (for "new token") used as a destination
20
+ * @param swapId - Identifier of the swap used by backend
21
+ * @param rate - Swap rate in the transaction
22
+ * @param tokenCurrency - "new token" used in the transaction, not listed yet in wallet-api list
23
+ * @param transaction - Transaction containing the recipient and amount
24
+ * @param binaryPayload - Blueprint of the data that we'll allow signing
25
+ * @param signature - Ensures the source of the payload
26
+ * @param feesStrategy - Slow / Medium / Fast
27
+ *
28
+ * @returns - The broadcasted transaction hash.
29
+ */
30
+ completeSwap({ provider, fromAccountId, toAccountId, swapId, rate, transaction, binaryPayload, signature, feeStrategy, tokenCurrency, }: {
31
+ provider: string;
32
+ fromAccountId: string;
33
+ toAccountId: string;
34
+ swapId: string;
35
+ rate: number;
36
+ transaction: Transaction;
37
+ binaryPayload: Buffer;
38
+ signature: Buffer;
39
+ feeStrategy: ExchangeCompleteParams["feeStrategy"];
40
+ tokenCurrency?: string;
41
+ }): Promise<string>;
42
+ /**
43
+ * Complete an exchange sell process by passing by the exchange content and its signature.
44
+ * User will be prompted on its device to approve the sell exchange operation.
45
+ * If the exchange is validated, the transaction is then signed and broadcasted to the network.
46
+ * @param provider - Used to verify the signature
47
+ * @param fromAccountId - Identifier of the account used as a source for the tx
48
+ * @param transaction - Transaction containing the recipient and amount
49
+ * @param binaryPayload - Blueprint of the data that we'll allow signing
50
+ * @param signature - Ensures the source of the payload
51
+ * @param feesStrategy - Slow / Medium / Fast
52
+ *
53
+ * @returns - The broadcasted transaction hash.
54
+ */
55
+ completeSell({ provider, fromAccountId, transaction, binaryPayload, signature, feeStrategy, }: {
56
+ provider: string;
57
+ fromAccountId: string;
58
+ transaction: Transaction;
59
+ binaryPayload: Buffer;
60
+ signature: Buffer;
61
+ feeStrategy: ExchangeCompleteParams["feeStrategy"];
62
+ }): Promise<string>;
63
+ /**
64
+ * Complete an exchange fund process by passing by the exchange content and its signature.
65
+ * User will be prompted on its device to approve the fund exchange operation.
66
+ * If the exchange is validated, the transaction is then signed and broadcasted to the network.
67
+ * @param provider - Used to verify the signature
68
+ * @param fromAccountId - Identifier of the account used as a source for the tx
69
+ * @param transaction - Transaction containing the recipient and amount
70
+ * @param binaryPayload - Blueprint of the data that we'll allow signing
71
+ * @param signature - Ensures the source of the payload
72
+ * @param feesStrategy - Slow / Medium / Fast
73
+ *
74
+ * @returns - The broadcasted transaction hash.
75
+ */
76
+ completeFund({ provider, fromAccountId, transaction, binaryPayload, signature, feeStrategy, tokenCurrency, }: {
77
+ provider: string;
78
+ fromAccountId: string;
79
+ transaction: Transaction;
80
+ binaryPayload: Buffer;
81
+ signature: Buffer;
82
+ feeStrategy: ExchangeCompleteParams["feeStrategy"];
83
+ tokenCurrency?: string;
84
+ }): Promise<string>;
85
+ }
86
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,YAAY,EAAE,WAAW,EAAwB,MAAM,6BAA6B,CAAC;AAC9F,OAAO,EACL,sBAAsB,EAEtB,mBAAmB,EAEpB,MAAM,SAAS,CAAC;AAEjB,cAAc,SAAS,CAAC;AAGxB,qBAAa,cAAe,SAAQ,YAAY;IAC9C;;;;;OAKG;IACG,KAAK,CAAC,YAAY,EAAE,mBAAmB,CAAC,cAAc,CAAC;IAW7D;;;;;;;;;;;;;;;;OAgBG;IACG,YAAY,CAAC,EACjB,QAAQ,EACR,aAAa,EACb,WAAW,EACX,MAAM,EACN,IAAI,EACJ,WAAW,EACX,aAAa,EACb,SAAS,EACT,WAAW,EACX,aAAa,GACd,EAAE;QACD,QAAQ,EAAE,MAAM,CAAC;QACjB,aAAa,EAAE,MAAM,CAAC;QACtB,WAAW,EAAE,MAAM,CAAC;QACpB,MAAM,EAAE,MAAM,CAAC;QACf,IAAI,EAAE,MAAM,CAAC;QACb,WAAW,EAAE,WAAW,CAAC;QACzB,aAAa,EAAE,MAAM,CAAC;QACtB,SAAS,EAAE,MAAM,CAAC;QAClB,WAAW,EAAE,sBAAsB,CAAC,aAAa,CAAC,CAAC;QACnD,aAAa,CAAC,EAAE,MAAM,CAAC;KACxB;IAqBD;;;;;;;;;;;;OAYG;IACG,YAAY,CAAC,EACjB,QAAQ,EACR,aAAa,EACb,WAAW,EACX,aAAa,EACb,SAAS,EACT,WAAW,GACZ,EAAE;QACD,QAAQ,EAAE,MAAM,CAAC;QACjB,aAAa,EAAE,MAAM,CAAC;QACtB,WAAW,EAAE,WAAW,CAAC;QACzB,aAAa,EAAE,MAAM,CAAC;QACtB,SAAS,EAAE,MAAM,CAAC;QAClB,WAAW,EAAE,sBAAsB,CAAC,aAAa,CAAC,CAAC;KACpD,GAAG,OAAO,CAAC,MAAM,CAAC;IAiBnB;;;;;;;;;;;;OAYG;IACG,YAAY,CAAC,EACjB,QAAQ,EACR,aAAa,EACb,WAAW,EACX,aAAa,EACb,SAAS,EACT,WAAW,EACX,aAAa,GACd,EAAE;QACD,QAAQ,EAAE,MAAM,CAAC;QACjB,aAAa,EAAE,MAAM,CAAC;QACtB,WAAW,EAAE,WAAW,CAAC;QACzB,aAAa,EAAE,MAAM,CAAC;QACtB,SAAS,EAAE,MAAM,CAAC;QAClB,WAAW,EAAE,sBAAsB,CAAC,aAAa,CAAC,CAAC;QACnD,aAAa,CAAC,EAAE,MAAM,CAAC;KACxB,GAAG,OAAO,CAAC,MAAM,CAAC;CAiBpB"}
@@ -0,0 +1,119 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { CustomModule, serializeTransaction } from "@ledgerhq/wallet-api-client";
11
+ export * from "./types";
12
+ // TODO maybe find a better way to type the available custom requests with correct types
13
+ export class ExchangeModule extends CustomModule {
14
+ /**
15
+ * Start the exchange process by generating a nonce on Ledger device
16
+ * @param exchangeType - used by the exchange transport to discern between swap/sell/fund
17
+ *
18
+ * @returns - A transaction ID used to complete the exchange process
19
+ */
20
+ start(exchangeType) {
21
+ return __awaiter(this, void 0, void 0, function* () {
22
+ const result = yield this.request("custom.exchange.start", {
23
+ exchangeType,
24
+ });
25
+ return result.transactionId;
26
+ });
27
+ }
28
+ /**
29
+ * Complete an exchange swap process by passing by the exchange content and its signature.
30
+ * User will be prompted on its device to approve the swap exchange operation.
31
+ * If the exchange is validated, the transaction is then signed and broadcasted to the network.
32
+ * @param provider - Used to verify the signature
33
+ * @param fromAccountId - Identifier of the account used as a source for the tx or parent account (for "new token")
34
+ * @param toAccountId - Identifier of the account or parent account (for "new token") used as a destination
35
+ * @param swapId - Identifier of the swap used by backend
36
+ * @param rate - Swap rate in the transaction
37
+ * @param tokenCurrency - "new token" used in the transaction, not listed yet in wallet-api list
38
+ * @param transaction - Transaction containing the recipient and amount
39
+ * @param binaryPayload - Blueprint of the data that we'll allow signing
40
+ * @param signature - Ensures the source of the payload
41
+ * @param feesStrategy - Slow / Medium / Fast
42
+ *
43
+ * @returns - The broadcasted transaction hash.
44
+ */
45
+ completeSwap({ provider, fromAccountId, toAccountId, swapId, rate, transaction, binaryPayload, signature, feeStrategy, tokenCurrency, }) {
46
+ return __awaiter(this, void 0, void 0, function* () {
47
+ const result = yield this.request("custom.exchange.complete", {
48
+ exchangeType: "SWAP",
49
+ provider,
50
+ fromAccountId,
51
+ toAccountId,
52
+ swapId,
53
+ rate,
54
+ rawTransaction: serializeTransaction(transaction),
55
+ hexBinaryPayload: binaryPayload.toString("hex"),
56
+ hexSignature: signature.toString("hex"),
57
+ feeStrategy,
58
+ tokenCurrency,
59
+ });
60
+ return result.transactionHash;
61
+ });
62
+ }
63
+ /**
64
+ * Complete an exchange sell process by passing by the exchange content and its signature.
65
+ * User will be prompted on its device to approve the sell exchange operation.
66
+ * If the exchange is validated, the transaction is then signed and broadcasted to the network.
67
+ * @param provider - Used to verify the signature
68
+ * @param fromAccountId - Identifier of the account used as a source for the tx
69
+ * @param transaction - Transaction containing the recipient and amount
70
+ * @param binaryPayload - Blueprint of the data that we'll allow signing
71
+ * @param signature - Ensures the source of the payload
72
+ * @param feesStrategy - Slow / Medium / Fast
73
+ *
74
+ * @returns - The broadcasted transaction hash.
75
+ */
76
+ completeSell({ provider, fromAccountId, transaction, binaryPayload, signature, feeStrategy, }) {
77
+ return __awaiter(this, void 0, void 0, function* () {
78
+ const result = yield this.request("custom.exchange.complete", {
79
+ exchangeType: "SELL",
80
+ provider,
81
+ fromAccountId,
82
+ rawTransaction: serializeTransaction(transaction),
83
+ hexBinaryPayload: binaryPayload.toString("hex"),
84
+ hexSignature: signature.toString("hex"),
85
+ feeStrategy,
86
+ });
87
+ return result.transactionHash;
88
+ });
89
+ }
90
+ /**
91
+ * Complete an exchange fund process by passing by the exchange content and its signature.
92
+ * User will be prompted on its device to approve the fund exchange operation.
93
+ * If the exchange is validated, the transaction is then signed and broadcasted to the network.
94
+ * @param provider - Used to verify the signature
95
+ * @param fromAccountId - Identifier of the account used as a source for the tx
96
+ * @param transaction - Transaction containing the recipient and amount
97
+ * @param binaryPayload - Blueprint of the data that we'll allow signing
98
+ * @param signature - Ensures the source of the payload
99
+ * @param feesStrategy - Slow / Medium / Fast
100
+ *
101
+ * @returns - The broadcasted transaction hash.
102
+ */
103
+ completeFund({ provider, fromAccountId, transaction, binaryPayload, signature, feeStrategy, tokenCurrency, }) {
104
+ return __awaiter(this, void 0, void 0, function* () {
105
+ const result = yield this.request("custom.exchange.complete", {
106
+ exchangeType: "FUND",
107
+ provider,
108
+ fromAccountId,
109
+ rawTransaction: serializeTransaction(transaction),
110
+ hexBinaryPayload: binaryPayload.toString("hex"),
111
+ hexSignature: signature.toString("hex"),
112
+ feeStrategy,
113
+ tokenCurrency,
114
+ });
115
+ return result.transactionHash;
116
+ });
117
+ }
118
+ }
119
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,YAAY,EAAe,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AAQ9F,cAAc,SAAS,CAAC;AAExB,wFAAwF;AACxF,MAAM,OAAO,cAAe,SAAQ,YAAY;IAC9C;;;;;OAKG;IACG,KAAK,CAAC,YAAiD;;YAC3D,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,OAAO,CAC/B,uBAAuB,EACvB;gBACE,YAAY;aACb,CACF,CAAC;YAEF,OAAO,MAAM,CAAC,aAAa,CAAC;QAC9B,CAAC;KAAA;IAED;;;;;;;;;;;;;;;;OAgBG;IACG,YAAY,CAAC,EACjB,QAAQ,EACR,aAAa,EACb,WAAW,EACX,MAAM,EACN,IAAI,EACJ,WAAW,EACX,aAAa,EACb,SAAS,EACT,WAAW,EACX,aAAa,GAYd;;YACC,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,OAAO,CAC/B,0BAA0B,EAC1B;gBACE,YAAY,EAAE,MAAM;gBACpB,QAAQ;gBACR,aAAa;gBACb,WAAW;gBACX,MAAM;gBACN,IAAI;gBACJ,cAAc,EAAE,oBAAoB,CAAC,WAAW,CAAC;gBACjD,gBAAgB,EAAE,aAAa,CAAC,QAAQ,CAAC,KAAK,CAAC;gBAC/C,YAAY,EAAE,SAAS,CAAC,QAAQ,CAAC,KAAK,CAAC;gBACvC,WAAW;gBACX,aAAa;aACd,CACF,CAAC;YAEF,OAAO,MAAM,CAAC,eAAe,CAAC;QAChC,CAAC;KAAA;IAED;;;;;;;;;;;;OAYG;IACG,YAAY,CAAC,EACjB,QAAQ,EACR,aAAa,EACb,WAAW,EACX,aAAa,EACb,SAAS,EACT,WAAW,GAQZ;;YACC,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,OAAO,CAC/B,0BAA0B,EAC1B;gBACE,YAAY,EAAE,MAAM;gBACpB,QAAQ;gBACR,aAAa;gBACb,cAAc,EAAE,oBAAoB,CAAC,WAAW,CAAC;gBACjD,gBAAgB,EAAE,aAAa,CAAC,QAAQ,CAAC,KAAK,CAAC;gBAC/C,YAAY,EAAE,SAAS,CAAC,QAAQ,CAAC,KAAK,CAAC;gBACvC,WAAW;aACZ,CACF,CAAC;YAEF,OAAO,MAAM,CAAC,eAAe,CAAC;QAChC,CAAC;KAAA;IAED;;;;;;;;;;;;OAYG;IACG,YAAY,CAAC,EACjB,QAAQ,EACR,aAAa,EACb,WAAW,EACX,aAAa,EACb,SAAS,EACT,WAAW,EACX,aAAa,GASd;;YACC,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,OAAO,CAC/B,0BAA0B,EAC1B;gBACE,YAAY,EAAE,MAAM;gBACpB,QAAQ;gBACR,aAAa;gBACb,cAAc,EAAE,oBAAoB,CAAC,WAAW,CAAC;gBACjD,gBAAgB,EAAE,aAAa,CAAC,QAAQ,CAAC,KAAK,CAAC;gBAC/C,YAAY,EAAE,SAAS,CAAC,QAAQ,CAAC,KAAK,CAAC;gBACvC,WAAW;gBACX,aAAa;aACd,CACF,CAAC;YAEF,OAAO,MAAM,CAAC,eAAe,CAAC;QAChC,CAAC;KAAA;CACF"}
@@ -0,0 +1,41 @@
1
+ import { RawTransaction } from "@ledgerhq/wallet-api-core";
2
+ export declare enum ExchangeType {
3
+ SWAP = 0,
4
+ SELL = 1,
5
+ FUND = 2,
6
+ SWAP_NG = 3,
7
+ SELL_NG = 4,
8
+ FUND_NG = 5
9
+ }
10
+ export type ExchangeStartParams = {
11
+ exchangeType: "FUND" | "SELL" | "SWAP" | "FUND_NG" | "SELL_NG" | "SWAP_NG";
12
+ };
13
+ export type ExchangeStartResult = {
14
+ transactionId: string;
15
+ };
16
+ export type ExchangeCompleteBaseParams = {
17
+ provider: string;
18
+ fromAccountId: string;
19
+ rawTransaction: RawTransaction;
20
+ hexBinaryPayload: string;
21
+ hexSignature: string;
22
+ feeStrategy: "SLOW" | "MEDIUM" | "FAST" | "CUSTOM";
23
+ tokenCurrency?: string;
24
+ };
25
+ export type ExchangeCompleteFundParams = ExchangeCompleteBaseParams & {
26
+ exchangeType: "FUND";
27
+ };
28
+ export type ExchangeCompleteSellParams = ExchangeCompleteBaseParams & {
29
+ exchangeType: "SELL";
30
+ };
31
+ export type ExchangeCompleteSwapParams = ExchangeCompleteBaseParams & {
32
+ exchangeType: "SWAP";
33
+ toAccountId: string;
34
+ swapId: string;
35
+ rate: number;
36
+ };
37
+ export type ExchangeCompleteParams = ExchangeCompleteFundParams | ExchangeCompleteSellParams | ExchangeCompleteSwapParams;
38
+ export type ExchangeCompleteResult = {
39
+ transactionHash: string;
40
+ };
41
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAE3D,oBAAY,YAAY;IACtB,IAAI,IAAO;IACX,IAAI,IAAO;IACX,IAAI,IAAO;IACX,OAAO,IAAO;IACd,OAAO,IAAO;IACd,OAAO,IAAO;CACf;AAED,MAAM,MAAM,mBAAmB,GAAG;IAChC,YAAY,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,SAAS,GAAG,SAAS,GAAG,SAAS,CAAC;CAC5E,CAAC;AAEF,MAAM,MAAM,mBAAmB,GAAG;IAChC,aAAa,EAAE,MAAM,CAAC;CACvB,CAAC;AAEF,MAAM,MAAM,0BAA0B,GAAG;IACvC,QAAQ,EAAE,MAAM,CAAC;IACjB,aAAa,EAAE,MAAM,CAAC;IACtB,cAAc,EAAE,cAAc,CAAC;IAC/B,gBAAgB,EAAE,MAAM,CAAC;IACzB,YAAY,EAAE,MAAM,CAAC;IACrB,WAAW,EAAE,MAAM,GAAG,QAAQ,GAAG,MAAM,GAAG,QAAQ,CAAC;IACnD,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB,CAAC;AAEF,MAAM,MAAM,0BAA0B,GAAG,0BAA0B,GAAG;IACpE,YAAY,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,0BAA0B,GAAG,0BAA0B,GAAG;IACpE,YAAY,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,0BAA0B,GAAG,0BAA0B,GAAG;IACpE,YAAY,EAAE,MAAM,CAAC;IACrB,WAAW,EAAE,MAAM,CAAC;IACpB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,MAAM,MAAM,sBAAsB,GAC9B,0BAA0B,GAC1B,0BAA0B,GAC1B,0BAA0B,CAAC;AAE/B,MAAM,MAAM,sBAAsB,GAAG;IACnC,eAAe,EAAE,MAAM,CAAC;CACzB,CAAC"}
@@ -0,0 +1,10 @@
1
+ export var ExchangeType;
2
+ (function (ExchangeType) {
3
+ ExchangeType[ExchangeType["SWAP"] = 0] = "SWAP";
4
+ ExchangeType[ExchangeType["SELL"] = 1] = "SELL";
5
+ ExchangeType[ExchangeType["FUND"] = 2] = "FUND";
6
+ ExchangeType[ExchangeType["SWAP_NG"] = 3] = "SWAP_NG";
7
+ ExchangeType[ExchangeType["SELL_NG"] = 4] = "SELL_NG";
8
+ ExchangeType[ExchangeType["FUND_NG"] = 5] = "FUND_NG";
9
+ })(ExchangeType || (ExchangeType = {}));
10
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAEA,MAAM,CAAN,IAAY,YAOX;AAPD,WAAY,YAAY;IACtB,+CAAW,CAAA;IACX,+CAAW,CAAA;IACX,+CAAW,CAAA;IACX,qDAAc,CAAA;IACd,qDAAc,CAAA;IACd,qDAAc,CAAA;AAChB,CAAC,EAPW,YAAY,KAAZ,YAAY,QAOvB"}
package/package.json ADDED
@@ -0,0 +1,71 @@
1
+ {
2
+ "name": "@ledgerhq/wallet-api-exchange-module",
3
+ "version": "0.1.0",
4
+ "description": "Wallet-API Exchange Module for Ledger Live",
5
+ "license": "Apache-2.0",
6
+ "keywords": [
7
+ "Ledger"
8
+ ],
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/LedgerHQ/ledger-live.git"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/LedgerHQ/ledger-live/issues"
15
+ },
16
+ "homepage": "https://github.com/LedgerHQ/ledger-live/tree/develop/libs/exchange-module",
17
+ "publishConfig": {
18
+ "access": "public"
19
+ },
20
+ "dependencies": {
21
+ "@ledgerhq/wallet-api-client": "^1.5.0",
22
+ "@ledgerhq/wallet-api-core": "^1.6.0"
23
+ },
24
+ "devDependencies": {
25
+ "@types/jest": "^29.5.4",
26
+ "@types/node": "^20.2.5",
27
+ "jest": "^28.1.1",
28
+ "jest-environment-jsdom": "28",
29
+ "ts-jest": "^28.0.5"
30
+ },
31
+ "typesVersions": {
32
+ "*": {
33
+ "*.json": [
34
+ "*.json"
35
+ ],
36
+ "*": [
37
+ "lib/*"
38
+ ],
39
+ "lib/*": [
40
+ "lib/*"
41
+ ],
42
+ "lib-es/*": [
43
+ "lib-es/*"
44
+ ]
45
+ }
46
+ },
47
+ "exports": {
48
+ "./lib/*": "./lib/*.js",
49
+ "./lib/*.js": "./lib/*.js",
50
+ "./lib-es/*": "./lib-es/*.js",
51
+ "./lib-es/*.js": "./lib-es/*.js",
52
+ "./*": {
53
+ "require": "./lib/*.js",
54
+ "default": "./lib-es/*.js"
55
+ },
56
+ "./*.js": {
57
+ "require": "./lib/*.js",
58
+ "default": "./lib-es/*.js"
59
+ },
60
+ "./package.json": "./package.json"
61
+ },
62
+ "scripts": {
63
+ "clean": "rimraf lib lib-es",
64
+ "build": "tsc && tsc -m ES6 --outDir lib-es",
65
+ "prewatch": "pnpm build",
66
+ "watch": "tsc --watch",
67
+ "lint": "eslint ./src --no-error-on-unmatched-pattern --ext .ts,.tsx",
68
+ "lint:fix": "pnpm lint --fix",
69
+ "test": "jest"
70
+ }
71
+ }
package/src/index.ts ADDED
@@ -0,0 +1,180 @@
1
+ import { CustomModule, Transaction, serializeTransaction } from "@ledgerhq/wallet-api-client";
2
+ import {
3
+ ExchangeCompleteParams,
4
+ ExchangeCompleteResult,
5
+ ExchangeStartParams,
6
+ ExchangeStartResult,
7
+ } from "./types";
8
+
9
+ export * from "./types";
10
+
11
+ // TODO maybe find a better way to type the available custom requests with correct types
12
+ export class ExchangeModule extends CustomModule {
13
+ /**
14
+ * Start the exchange process by generating a nonce on Ledger device
15
+ * @param exchangeType - used by the exchange transport to discern between swap/sell/fund
16
+ *
17
+ * @returns - A transaction ID used to complete the exchange process
18
+ */
19
+ async start(exchangeType: ExchangeStartParams["exchangeType"]) {
20
+ const result = await this.request<ExchangeStartParams, ExchangeStartResult>(
21
+ "custom.exchange.start",
22
+ {
23
+ exchangeType,
24
+ },
25
+ );
26
+
27
+ return result.transactionId;
28
+ }
29
+
30
+ /**
31
+ * Complete an exchange swap process by passing by the exchange content and its signature.
32
+ * User will be prompted on its device to approve the swap exchange operation.
33
+ * If the exchange is validated, the transaction is then signed and broadcasted to the network.
34
+ * @param provider - Used to verify the signature
35
+ * @param fromAccountId - Identifier of the account used as a source for the tx or parent account (for "new token")
36
+ * @param toAccountId - Identifier of the account or parent account (for "new token") used as a destination
37
+ * @param swapId - Identifier of the swap used by backend
38
+ * @param rate - Swap rate in the transaction
39
+ * @param tokenCurrency - "new token" used in the transaction, not listed yet in wallet-api list
40
+ * @param transaction - Transaction containing the recipient and amount
41
+ * @param binaryPayload - Blueprint of the data that we'll allow signing
42
+ * @param signature - Ensures the source of the payload
43
+ * @param feesStrategy - Slow / Medium / Fast
44
+ *
45
+ * @returns - The broadcasted transaction hash.
46
+ */
47
+ async completeSwap({
48
+ provider,
49
+ fromAccountId,
50
+ toAccountId,
51
+ swapId,
52
+ rate,
53
+ transaction,
54
+ binaryPayload,
55
+ signature,
56
+ feeStrategy,
57
+ tokenCurrency,
58
+ }: {
59
+ provider: string;
60
+ fromAccountId: string;
61
+ toAccountId: string;
62
+ swapId: string;
63
+ rate: number;
64
+ transaction: Transaction;
65
+ binaryPayload: Buffer;
66
+ signature: Buffer;
67
+ feeStrategy: ExchangeCompleteParams["feeStrategy"];
68
+ tokenCurrency?: string;
69
+ }) {
70
+ const result = await this.request<ExchangeCompleteParams, ExchangeCompleteResult>(
71
+ "custom.exchange.complete",
72
+ {
73
+ exchangeType: "SWAP",
74
+ provider,
75
+ fromAccountId,
76
+ toAccountId,
77
+ swapId,
78
+ rate,
79
+ rawTransaction: serializeTransaction(transaction),
80
+ hexBinaryPayload: binaryPayload.toString("hex"),
81
+ hexSignature: signature.toString("hex"),
82
+ feeStrategy,
83
+ tokenCurrency,
84
+ },
85
+ );
86
+
87
+ return result.transactionHash;
88
+ }
89
+
90
+ /**
91
+ * Complete an exchange sell process by passing by the exchange content and its signature.
92
+ * User will be prompted on its device to approve the sell exchange operation.
93
+ * If the exchange is validated, the transaction is then signed and broadcasted to the network.
94
+ * @param provider - Used to verify the signature
95
+ * @param fromAccountId - Identifier of the account used as a source for the tx
96
+ * @param transaction - Transaction containing the recipient and amount
97
+ * @param binaryPayload - Blueprint of the data that we'll allow signing
98
+ * @param signature - Ensures the source of the payload
99
+ * @param feesStrategy - Slow / Medium / Fast
100
+ *
101
+ * @returns - The broadcasted transaction hash.
102
+ */
103
+ async completeSell({
104
+ provider,
105
+ fromAccountId,
106
+ transaction,
107
+ binaryPayload,
108
+ signature,
109
+ feeStrategy,
110
+ }: {
111
+ provider: string;
112
+ fromAccountId: string;
113
+ transaction: Transaction;
114
+ binaryPayload: Buffer;
115
+ signature: Buffer;
116
+ feeStrategy: ExchangeCompleteParams["feeStrategy"];
117
+ }): Promise<string> {
118
+ const result = await this.request<ExchangeCompleteParams, ExchangeCompleteResult>(
119
+ "custom.exchange.complete",
120
+ {
121
+ exchangeType: "SELL",
122
+ provider,
123
+ fromAccountId,
124
+ rawTransaction: serializeTransaction(transaction),
125
+ hexBinaryPayload: binaryPayload.toString("hex"),
126
+ hexSignature: signature.toString("hex"),
127
+ feeStrategy,
128
+ },
129
+ );
130
+
131
+ return result.transactionHash;
132
+ }
133
+
134
+ /**
135
+ * Complete an exchange fund process by passing by the exchange content and its signature.
136
+ * User will be prompted on its device to approve the fund exchange operation.
137
+ * If the exchange is validated, the transaction is then signed and broadcasted to the network.
138
+ * @param provider - Used to verify the signature
139
+ * @param fromAccountId - Identifier of the account used as a source for the tx
140
+ * @param transaction - Transaction containing the recipient and amount
141
+ * @param binaryPayload - Blueprint of the data that we'll allow signing
142
+ * @param signature - Ensures the source of the payload
143
+ * @param feesStrategy - Slow / Medium / Fast
144
+ *
145
+ * @returns - The broadcasted transaction hash.
146
+ */
147
+ async completeFund({
148
+ provider,
149
+ fromAccountId,
150
+ transaction,
151
+ binaryPayload,
152
+ signature,
153
+ feeStrategy,
154
+ tokenCurrency,
155
+ }: {
156
+ provider: string;
157
+ fromAccountId: string;
158
+ transaction: Transaction;
159
+ binaryPayload: Buffer;
160
+ signature: Buffer;
161
+ feeStrategy: ExchangeCompleteParams["feeStrategy"];
162
+ tokenCurrency?: string;
163
+ }): Promise<string> {
164
+ const result = await this.request<ExchangeCompleteParams, ExchangeCompleteResult>(
165
+ "custom.exchange.complete",
166
+ {
167
+ exchangeType: "FUND",
168
+ provider,
169
+ fromAccountId,
170
+ rawTransaction: serializeTransaction(transaction),
171
+ hexBinaryPayload: binaryPayload.toString("hex"),
172
+ hexSignature: signature.toString("hex"),
173
+ feeStrategy,
174
+ tokenCurrency,
175
+ },
176
+ );
177
+
178
+ return result.transactionHash;
179
+ }
180
+ }
package/src/types.ts ADDED
@@ -0,0 +1,52 @@
1
+ import { RawTransaction } from "@ledgerhq/wallet-api-core";
2
+
3
+ export enum ExchangeType {
4
+ SWAP = 0x00,
5
+ SELL = 0x01,
6
+ FUND = 0x02,
7
+ SWAP_NG = 0x03,
8
+ SELL_NG = 0x04,
9
+ FUND_NG = 0x05,
10
+ }
11
+
12
+ export type ExchangeStartParams = {
13
+ exchangeType: "FUND" | "SELL" | "SWAP" | "FUND_NG" | "SELL_NG" | "SWAP_NG";
14
+ };
15
+
16
+ export type ExchangeStartResult = {
17
+ transactionId: string;
18
+ };
19
+
20
+ export type ExchangeCompleteBaseParams = {
21
+ provider: string;
22
+ fromAccountId: string;
23
+ rawTransaction: RawTransaction;
24
+ hexBinaryPayload: string;
25
+ hexSignature: string;
26
+ feeStrategy: "SLOW" | "MEDIUM" | "FAST" | "CUSTOM";
27
+ tokenCurrency?: string;
28
+ };
29
+
30
+ export type ExchangeCompleteFundParams = ExchangeCompleteBaseParams & {
31
+ exchangeType: "FUND";
32
+ };
33
+
34
+ export type ExchangeCompleteSellParams = ExchangeCompleteBaseParams & {
35
+ exchangeType: "SELL";
36
+ };
37
+
38
+ export type ExchangeCompleteSwapParams = ExchangeCompleteBaseParams & {
39
+ exchangeType: "SWAP";
40
+ toAccountId: string;
41
+ swapId: string;
42
+ rate: number;
43
+ };
44
+
45
+ export type ExchangeCompleteParams =
46
+ | ExchangeCompleteFundParams
47
+ | ExchangeCompleteSellParams
48
+ | ExchangeCompleteSwapParams;
49
+
50
+ export type ExchangeCompleteResult = {
51
+ transactionHash: string;
52
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "extends": "../../tsconfig.base",
3
+ "compilerOptions": {
4
+ "declaration": true,
5
+ "declarationMap": true,
6
+ "noImplicitAny": false,
7
+ "noImplicitThis": false,
8
+ "downlevelIteration": true,
9
+ "module": "commonjs",
10
+ "lib": ["es2020", "dom"],
11
+ "jsx": "react",
12
+ "outDir": "lib"
13
+ },
14
+ "include": ["src/**/*"]
15
+ }