Skip to content

Commit f193232

Browse files
fix: Tezos dapp: payload for sign prep
1 parent 3845977 commit f193232

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

dapps/universal-provider-tezos/src/App.tsx

+23-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { WalletConnectModal } from "@walletconnect/modal";
22
import { useEffect, useState, useCallback } from "react";
3-
import { SAMPLES, SAMPLE_KINDS } from "./utils/samples";
3+
import { SAMPLES, SAMPLE_KINDS, getBakerAddress } from "./utils/samples";
44
import {
55
TezosProvider,
66
TezosChainDataTestnet,
77
TezosGetAccountResponse,
88
TezosSendResponse,
99
TezosSignResponse,
10-
ChainData,
1110
TezosChainDataMainnet,
1211
} from "@trili/tezos-provider";
1312
import { ErrorObject } from "@walletconnect/utils";
@@ -123,7 +122,8 @@ const App = () => {
123122
} catch (error) {
124123
console.error("Error connecting to Tezos:", error);
125124
}
126-
}, [provider],
125+
},
126+
[provider],
127127
);
128128

129129
// Disconnect from Tezos
@@ -156,9 +156,12 @@ const App = () => {
156156
break;
157157
case SAMPLE_KINDS.SIGN: {
158158
const formattedInput = ` Payload from TezosProvider dapp generated at ${new Date().toISOString()}`;
159+
// build payload following https://taquito.io/docs/signing/#generating-a-signature-with-beacon-sdk
159160
const bytes = stringToBytes(formattedInput);
160-
const payload =
161-
"05" + "0100" + stringToBytes(bytes.length.toString()) + bytes;
161+
const bytesLength = (bytes.length / 2).toString(16);
162+
const addPadding = `00000000${bytesLength}`;
163+
const paddedBytesLength = addPadding.slice(-8);
164+
const payload = "05" + "01" + paddedBytesLength + bytes;
162165
res = await provider.sign(payload);
163166
break;
164167
}
@@ -168,9 +171,10 @@ const App = () => {
168171
);
169172
break;
170173
case SAMPLE_KINDS.SEND_DELEGATION:
171-
res = await provider.sendDelegation(
172-
SAMPLES[SAMPLE_KINDS.SEND_DELEGATION],
173-
);
174+
res = await provider.sendDelegation({
175+
...SAMPLES[SAMPLE_KINDS.SEND_DELEGATION],
176+
delegate: getBakerAddress(provider?.getChainId()),
177+
});
174178
break;
175179
case SAMPLE_KINDS.SEND_UNDELEGATION:
176180
res = await provider.sendUndelegation();
@@ -248,10 +252,16 @@ const App = () => {
248252
(kind: SAMPLE_KINDS) => {
249253
switch (kind) {
250254
case SAMPLE_KINDS.SEND_TRANSACTION:
251-
case SAMPLE_KINDS.SEND_DELEGATION:
252255
case SAMPLE_KINDS.SEND_UNDELEGATION:
253256
setDescription(SAMPLES[kind]);
254257
break;
258+
case SAMPLE_KINDS.SEND_DELEGATION:
259+
// provider address depends on the chain
260+
setDescription({
261+
...SAMPLES[kind],
262+
delegate: getBakerAddress(provider?.getChainId()),
263+
});
264+
break;
255265
case SAMPLE_KINDS.SEND_ORGINATION:
256266
setDescription(SAMPLES[kind] as unknown as Record<string, unknown>);
257267
break;
@@ -298,6 +308,10 @@ const App = () => {
298308
<b>Public Key: </b>
299309
{provider?.connection?.address ?? "No account connected"}
300310
</p>
311+
<p>
312+
<b>Chain: </b>
313+
{provider?.getChainId() ?? "No chain connected"}
314+
</p>
301315
<p>
302316
<b>Balance: </b>
303317
{balance}

dapps/universal-provider-tezos/src/utils/samples.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,20 @@ const tezosContractCallOperation: PartialTezosTransactionOperation = {
6767
parameters: { entrypoint: "default", value: { int: "20" } }, // Add 20 to the current storage value
6868
};
6969

70+
export const SAMPLE_BAKER_MAINNET = "tz3ZmB8oWUmi8YZXgeRpgAcPnEMD8VgUa4Ve";
71+
export const SAMPLE_BAKER_TESTNET = "tz3cqThj23Feu55KDynm7Vg81mCMpWDgzQZq";
72+
73+
export const getBakerAddress = (network?: string) => {
74+
return network === "tezos:mainnet"
75+
? SAMPLE_BAKER_MAINNET
76+
: network === "tezos:ghostnet"
77+
? SAMPLE_BAKER_TESTNET
78+
: "[no baker found]";
79+
};
80+
7081
const tezosDelegationOperation: PartialTezosDelegationOperation = {
7182
kind: TezosOperationType.DELEGATION,
72-
delegate: "tz3ZmB8oWUmi8YZXgeRpgAcPnEMD8VgUa4Ve", // Tezos Foundation Ghost Baker. Cannot delegate to ourself as that would block undelegation
83+
delegate: SAMPLE_BAKER_TESTNET,
7384
};
7485

7586
const tezosUndelegationOperation: PartialTezosDelegationOperation = {

0 commit comments

Comments
 (0)