Skip to content

Commit

Permalink
fix: Tezos dapp: payload for sign prep
Browse files Browse the repository at this point in the history
  • Loading branch information
dianasavvatina committed Jan 6, 2025
1 parent 3845977 commit f193232
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
32 changes: 23 additions & 9 deletions dapps/universal-provider-tezos/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { WalletConnectModal } from "@walletconnect/modal";
import { useEffect, useState, useCallback } from "react";
import { SAMPLES, SAMPLE_KINDS } from "./utils/samples";
import { SAMPLES, SAMPLE_KINDS, getBakerAddress } from "./utils/samples";
import {
TezosProvider,
TezosChainDataTestnet,
TezosGetAccountResponse,
TezosSendResponse,
TezosSignResponse,
ChainData,
TezosChainDataMainnet,
} from "@trili/tezos-provider";
import { ErrorObject } from "@walletconnect/utils";
Expand Down Expand Up @@ -123,7 +122,8 @@ const App = () => {
} catch (error) {
console.error("Error connecting to Tezos:", error);
}
}, [provider],
},
[provider],
);

// Disconnect from Tezos
Expand Down Expand Up @@ -156,9 +156,12 @@ const App = () => {
break;
case SAMPLE_KINDS.SIGN: {
const formattedInput = ` Payload from TezosProvider dapp generated at ${new Date().toISOString()}`;
// build payload following https://taquito.io/docs/signing/#generating-a-signature-with-beacon-sdk
const bytes = stringToBytes(formattedInput);
const payload =
"05" + "0100" + stringToBytes(bytes.length.toString()) + bytes;
const bytesLength = (bytes.length / 2).toString(16);
const addPadding = `00000000${bytesLength}`;
const paddedBytesLength = addPadding.slice(-8);
const payload = "05" + "01" + paddedBytesLength + bytes;
res = await provider.sign(payload);
break;
}
Expand All @@ -168,9 +171,10 @@ const App = () => {
);
break;
case SAMPLE_KINDS.SEND_DELEGATION:
res = await provider.sendDelegation(
SAMPLES[SAMPLE_KINDS.SEND_DELEGATION],
);
res = await provider.sendDelegation({
...SAMPLES[SAMPLE_KINDS.SEND_DELEGATION],
delegate: getBakerAddress(provider?.getChainId()),
});
break;
case SAMPLE_KINDS.SEND_UNDELEGATION:
res = await provider.sendUndelegation();
Expand Down Expand Up @@ -248,10 +252,16 @@ const App = () => {
(kind: SAMPLE_KINDS) => {
switch (kind) {
case SAMPLE_KINDS.SEND_TRANSACTION:
case SAMPLE_KINDS.SEND_DELEGATION:
case SAMPLE_KINDS.SEND_UNDELEGATION:
setDescription(SAMPLES[kind]);
break;
case SAMPLE_KINDS.SEND_DELEGATION:
// provider address depends on the chain
setDescription({
...SAMPLES[kind],
delegate: getBakerAddress(provider?.getChainId()),
});
break;
case SAMPLE_KINDS.SEND_ORGINATION:
setDescription(SAMPLES[kind] as unknown as Record<string, unknown>);
break;
Expand Down Expand Up @@ -298,6 +308,10 @@ const App = () => {
<b>Public Key: </b>
{provider?.connection?.address ?? "No account connected"}
</p>
<p>
<b>Chain: </b>
{provider?.getChainId() ?? "No chain connected"}
</p>
<p>
<b>Balance: </b>
{balance}
Expand Down
13 changes: 12 additions & 1 deletion dapps/universal-provider-tezos/src/utils/samples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,20 @@ const tezosContractCallOperation: PartialTezosTransactionOperation = {
parameters: { entrypoint: "default", value: { int: "20" } }, // Add 20 to the current storage value
};

export const SAMPLE_BAKER_MAINNET = "tz3ZmB8oWUmi8YZXgeRpgAcPnEMD8VgUa4Ve";
export const SAMPLE_BAKER_TESTNET = "tz3cqThj23Feu55KDynm7Vg81mCMpWDgzQZq";

export const getBakerAddress = (network?: string) => {
return network === "tezos:mainnet"
? SAMPLE_BAKER_MAINNET
: network === "tezos:ghostnet"
? SAMPLE_BAKER_TESTNET
: "[no baker found]";
};

const tezosDelegationOperation: PartialTezosDelegationOperation = {
kind: TezosOperationType.DELEGATION,
delegate: "tz3ZmB8oWUmi8YZXgeRpgAcPnEMD8VgUa4Ve", // Tezos Foundation Ghost Baker. Cannot delegate to ourself as that would block undelegation
delegate: SAMPLE_BAKER_TESTNET,
};

const tezosUndelegationOperation: PartialTezosDelegationOperation = {
Expand Down

0 comments on commit f193232

Please sign in to comment.