Skip to content

Commit 8206dac

Browse files
authored
feat(ton): expose ton payload for all possible actions [LIVE-17777] (#447)
1 parent b3c199a commit 8206dac

File tree

7 files changed

+912
-220
lines changed

7 files changed

+912
-220
lines changed

.changeset/thin-jeans-trade.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@ledgerhq/wallet-api-core": minor
3+
---
4+
5+
feat(ton): expose ton payload for all possible actions

packages/core/package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,22 @@
2020
},
2121
"devDependencies": {
2222
"@ledgerhq/jest-shared-config": "workspace:*",
23-
"@stacks/transactions": "^6.13.0",
2423
"@types/jest": "^29.5.12",
2524
"@types/node": "^20.11.19",
2625
"@types/uuid": "^9.0.8",
2726
"eslint": "^8.56.0",
2827
"jest": "^29.7.0",
2928
"jest-environment-jsdom": "^29.7.0",
30-
"thor-devkit": "^2.0.9",
3129
"ts-jest": "^29.1.2",
3230
"ts-node": "^10.9.2",
3331
"typescript": "^5.3.3"
3432
},
3533
"dependencies": {
3634
"@ledgerhq/errors": "^6.16.2",
35+
"@stacks/transactions": "^6.13.0",
36+
"@ton/core": "^0.60.1",
3737
"bignumber.js": "^9.1.2",
38+
"thor-devkit": "^2.0.9",
3839
"uuid": "^9.0.1",
3940
"zod": "^3.22.4"
4041
}
+237-1
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
1+
import { Address, Cell } from "@ton/core";
12
import BigNumber from "bignumber.js";
2-
import type { RawTonTransaction, TonTransaction } from "./types";
3+
import type {
4+
RawTonTransaction,
5+
TonPayloadFormat,
6+
TonPayloadFormatRaw,
7+
TonTransaction,
8+
} from "./types";
39

410
export const serializeTonTransaction = ({
511
amount,
612
recipient,
713
family,
814
fees,
915
comment,
16+
payload,
1017
}: TonTransaction): RawTonTransaction => ({
1118
amount: amount.toString(),
1219
recipient,
1320
family,
1421
fees: fees.toString(),
1522
comment,
23+
payload: payload ? toTransactionPayloadRaw(payload) : undefined,
1624
});
1725

1826
export const deserializeTonTransaction = ({
@@ -21,10 +29,238 @@ export const deserializeTonTransaction = ({
2129
family,
2230
fees,
2331
comment,
32+
payload,
2433
}: RawTonTransaction): TonTransaction => ({
2534
amount: new BigNumber(amount),
2635
recipient,
2736
family,
2837
fees: new BigNumber(fees),
2938
comment,
39+
payload: payload ? fromTransactionPayloadRaw(payload) : undefined,
3040
});
41+
42+
const fromTransactionPayloadRaw = (
43+
payload: TonPayloadFormatRaw,
44+
): TonPayloadFormat => {
45+
switch (payload.type) {
46+
case "unsafe":
47+
return {
48+
type: payload.type,
49+
message: Cell.fromBase64(payload.message),
50+
};
51+
case "jetton-transfer":
52+
return {
53+
type: payload.type,
54+
queryId: payload.queryId ? BigInt(payload.queryId) : null,
55+
amount: BigInt(payload.amount),
56+
destination: Address.parse(payload.destination),
57+
responseDestination: Address.parse(payload.responseDestination),
58+
customPayload: payload.customPayload
59+
? Cell.fromBase64(payload.customPayload)
60+
: null,
61+
forwardAmount: BigInt(payload.forwardAmount),
62+
forwardPayload: payload.forwardPayload
63+
? Cell.fromBase64(payload.forwardPayload)
64+
: null,
65+
knownJetton: payload.knownJetton,
66+
};
67+
case "nft-transfer":
68+
return {
69+
type: payload.type,
70+
queryId: payload.queryId ? BigInt(payload.queryId) : null,
71+
newOwner: Address.parse(payload.newOwner),
72+
responseDestination: Address.parse(payload.responseDestination),
73+
customPayload: payload.customPayload
74+
? Cell.fromBase64(payload.customPayload)
75+
: null,
76+
forwardAmount: BigInt(payload.forwardAmount),
77+
forwardPayload: payload.forwardPayload
78+
? Cell.fromBase64(payload.forwardPayload)
79+
: null,
80+
};
81+
case "jetton-burn":
82+
return {
83+
type: payload.type,
84+
queryId: payload.queryId ? BigInt(payload.queryId) : null,
85+
amount: BigInt(payload.amount),
86+
responseDestination: Address.parse(payload.responseDestination),
87+
// Handle customPayload could be a buffer, we should try/catch the fromBase64
88+
customPayload: payload.customPayload
89+
? Cell.fromBase64(payload.customPayload)
90+
: null,
91+
};
92+
case "add-whitelist":
93+
case "single-nominator-change-validator":
94+
return {
95+
type: payload.type,
96+
queryId: payload.queryId ? BigInt(payload.queryId) : null,
97+
address: Address.parse(payload.address),
98+
};
99+
case "single-nominator-withdraw":
100+
return {
101+
type: payload.type,
102+
queryId: payload.queryId ? BigInt(payload.queryId) : null,
103+
amount: BigInt(payload.amount),
104+
};
105+
case "tonstakers-deposit":
106+
return {
107+
type: payload.type,
108+
queryId: payload.queryId ? BigInt(payload.queryId) : null,
109+
appId: payload.appId ? BigInt(payload.appId) : null,
110+
};
111+
case "vote-for-proposal":
112+
return {
113+
type: payload.type,
114+
queryId: payload.queryId ? BigInt(payload.queryId) : null,
115+
votingAddress: Address.parse(payload.votingAddress),
116+
expirationDate: payload.expirationDate,
117+
vote: payload.vote,
118+
needConfirmation: payload.needConfirmation,
119+
};
120+
case "change-dns-record":
121+
return {
122+
type: payload.type,
123+
queryId: payload.queryId ? BigInt(payload.queryId) : null,
124+
record:
125+
payload.record.type === "wallet"
126+
? {
127+
type: payload.record.type,
128+
value: payload.record.value
129+
? {
130+
address: Address.parse(payload.record.value.address),
131+
capabilities: payload.record.value.capabilities,
132+
}
133+
: null,
134+
}
135+
: {
136+
type: payload.record.type,
137+
key: Buffer.from(payload.record.key, "hex"),
138+
value: payload.record.value
139+
? Cell.fromBase64(payload.record.value)
140+
: null,
141+
},
142+
};
143+
case "token-bridge-pay-swap":
144+
return {
145+
type: payload.type,
146+
queryId: payload.queryId ? BigInt(payload.queryId) : null,
147+
swapId: Buffer.from(payload.swapId, "hex"),
148+
};
149+
case "comment":
150+
return payload;
151+
}
152+
};
153+
154+
const toTransactionPayloadRaw = (
155+
payload: TonPayloadFormat,
156+
): TonPayloadFormatRaw => {
157+
switch (payload.type) {
158+
case "unsafe":
159+
return {
160+
type: payload.type,
161+
message: payload.message.toBoc().toString("base64"),
162+
};
163+
case "jetton-transfer":
164+
return {
165+
type: payload.type,
166+
queryId: payload.queryId ? payload.queryId.toString() : null,
167+
amount: payload.amount.toString(),
168+
destination: payload.destination.toRawString(),
169+
responseDestination: payload.responseDestination.toRawString(),
170+
customPayload: payload.customPayload
171+
? payload.customPayload.toBoc().toString("base64")
172+
: null,
173+
forwardAmount: payload.forwardAmount.toString(),
174+
forwardPayload: payload.forwardPayload
175+
? payload.forwardPayload.toBoc().toString("base64")
176+
: null,
177+
knownJetton: payload.knownJetton,
178+
};
179+
case "nft-transfer":
180+
return {
181+
type: payload.type,
182+
queryId: payload.queryId ? payload.queryId.toString() : null,
183+
newOwner: payload.newOwner.toRawString(),
184+
responseDestination: payload.responseDestination.toRawString(),
185+
customPayload: payload.customPayload
186+
? payload.customPayload.toBoc().toString("base64")
187+
: null,
188+
forwardAmount: payload.forwardAmount.toString(),
189+
forwardPayload: payload.forwardPayload
190+
? payload.forwardPayload.toBoc().toString("base64")
191+
: null,
192+
};
193+
case "jetton-burn":
194+
return {
195+
type: payload.type,
196+
queryId: payload.queryId ? payload.queryId.toString() : null,
197+
amount: payload.amount.toString(),
198+
responseDestination: payload.responseDestination.toRawString(),
199+
customPayload:
200+
payload.customPayload && "toBoc" in payload.customPayload
201+
? payload.customPayload.toBoc().toString("base64")
202+
: payload.customPayload
203+
? payload.customPayload.toString("hex")
204+
: null,
205+
};
206+
case "add-whitelist":
207+
case "single-nominator-change-validator":
208+
return {
209+
type: payload.type,
210+
queryId: payload.queryId ? payload.queryId.toString() : null,
211+
address: payload.address.toRawString(),
212+
};
213+
case "single-nominator-withdraw":
214+
return {
215+
type: payload.type,
216+
queryId: payload.queryId ? payload.queryId.toString() : null,
217+
amount: payload.amount.toString(),
218+
};
219+
case "tonstakers-deposit":
220+
return {
221+
type: payload.type,
222+
queryId: payload.queryId ? payload.queryId.toString() : null,
223+
appId: payload.appId ? payload.appId.toString() : null,
224+
};
225+
case "vote-for-proposal":
226+
return {
227+
type: payload.type,
228+
queryId: payload.queryId ? payload.queryId.toString() : null,
229+
votingAddress: payload.votingAddress.toRawString(),
230+
expirationDate: payload.expirationDate,
231+
vote: payload.vote,
232+
needConfirmation: payload.needConfirmation,
233+
};
234+
case "change-dns-record":
235+
return {
236+
type: payload.type,
237+
queryId: payload.queryId ? payload.queryId.toString() : null,
238+
record:
239+
payload.record.type === "wallet"
240+
? {
241+
type: payload.record.type,
242+
value: payload.record.value
243+
? {
244+
address: payload.record.value.address.toRawString(),
245+
capabilities: payload.record.value.capabilities,
246+
}
247+
: null,
248+
}
249+
: {
250+
type: payload.record.type,
251+
key: payload.record.key.toString("hex"),
252+
value: payload.record.value
253+
? payload.record.value.toBoc().toString("base64")
254+
: null,
255+
},
256+
};
257+
case "token-bridge-pay-swap":
258+
return {
259+
type: payload.type,
260+
queryId: payload.queryId ? payload.queryId.toString() : null,
261+
swapId: payload.swapId.toString("hex"),
262+
};
263+
case "comment":
264+
return payload;
265+
}
266+
};

0 commit comments

Comments
 (0)