Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BRO-13] Payload decoding in the browser wallet of the CIS3 standard #83

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
101 changes: 47 additions & 54 deletions sponsoredTransactions/frontend/src/SponsoredTransactions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import {
deserializeTypeValue,
DOBEN marked this conversation as resolved.
Show resolved Hide resolved
AccountAddress,
DOBEN marked this conversation as resolved.
Show resolved Hide resolved
ConcordiumGRPCClient,
AccountTransactionSignature,
} from '@concordium/web-sdk';
import {
useGrpcClient,
WalletConnectionProps,
useConnection,
useConnect,
typeSchemaFromBase64,
TESTNET,
WalletConnection,
} from '@concordium/react-components';
import { version } from '../package.json';

Expand All @@ -25,7 +26,6 @@ import {
SPONSORED_TX_CONTRACT_NAME,
NONCE_OF_PARAMETER_SCHEMA,
NONCE_OF_RETURN_VALUE_SCHEMA,
SERIALIZATION_HELPER_SCHEMA,
DOBEN marked this conversation as resolved.
Show resolved Hide resolved
CONTRACT_SUB_INDEX,
BROWSER_WALLET,
WALLET_CONNECT,
Expand Down Expand Up @@ -83,13 +83,7 @@ const InputFieldStyle = {
padding: '10px 20px',
};

function generateTransferMessage(
expiryTimeSignature: string,
nonce: string,
tokenID: string,
from: string,
to: string,
) {
function generateTransferMessage(nonce: string, tokenID: string, from: string, to: string) {
DOBEN marked this conversation as resolved.
Show resolved Hide resolved
if (nonce === '') {
alert('Insert a nonce.');
return '';
Expand Down Expand Up @@ -145,30 +139,13 @@ function generateTransferMessage(
},
];

const payload = serializeTypeValue(transfer, toBuffer(TRANSFER_SCHEMA, 'base64'));

const message = {
contract_address: {
index: Number(process.env.SMART_CONTRACT_INDEX),
subindex: 0,
},
nonce: Number(nonce),
timestamp: expiryTimeSignature,
entry_point: 'transfer',
payload: Array.from(payload),
};

const serializedMessage = serializeTypeValue(message, toBuffer(SERIALIZATION_HELPER_SCHEMA, 'base64'));
// eslint-disable-next-line @typescript-eslint/no-base-to-string
const payload = serializeTypeValue(transfer, toBuffer(TRANSFER_SCHEMA, 'base64')).toString('hex');

return serializedMessage;
return payload;
}

function generateUpdateOperatorMessage(
expiryTimeSignature: string,
nonce: string,
operator: string,
addOperator: boolean,
) {
function generateUpdateOperatorMessage(nonce: string, operator: string, addOperator: boolean) {
if (nonce === '') {
alert('Insert a nonce.');
return '';
Expand Down Expand Up @@ -207,22 +184,35 @@ function generateUpdateOperatorMessage(
},
];

const payload = serializeTypeValue(updateOperator, toBuffer(UPDATE_OPERATOR_SCHEMA, 'base64'));

const message = {
contract_address: {
index: Number(process.env.SMART_CONTRACT_INDEX),
subindex: 0,
},
nonce: Number(nonce),
timestamp: expiryTimeSignature,
entry_point: 'updateOperator',
payload: Array.from(payload),
};
// eslint-disable-next-line @typescript-eslint/no-base-to-string
DOBEN marked this conversation as resolved.
Show resolved Hide resolved
const payload = serializeTypeValue(updateOperator, toBuffer(UPDATE_OPERATOR_SCHEMA, 'base64')).toString('hex');

const serializedMessage = serializeTypeValue(message, toBuffer(SERIALIZATION_HELPER_SCHEMA, 'base64'));
return payload;
}

return serializedMessage;
function signCIS3Message(
connection: WalletConnection,
isUpdateOperatorTab: boolean,
nonce: string,
expiryTimeSignature: string,
account: string,
payload: string,
): Promise<AccountTransactionSignature> {
const contractAddress = { index: Number(process.env.SMART_CONTRACT_INDEX), subindex: 0 };
const contractName = { value: SPONSORED_TX_CONTRACT_NAME };
const entrypointName = { value: isUpdateOperatorTab ? 'updateOperator' : 'transfer' };
const _nonce = Number(nonce);
const payloadMessage = { data: payload, schema: isUpdateOperatorTab ? UPDATE_OPERATOR_SCHEMA : TRANSFER_SCHEMA };
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-return
return connection.client.signCIS3Message(
contractAddress,
contractName,
entrypointName,
_nonce,
expiryTimeSignature,
account,
payloadMessage,
);
}

async function getPublicKey(rpcClient: ConcordiumGRPCClient, account: string) {
Expand Down Expand Up @@ -638,16 +628,19 @@ export default function SponsoredTransactions(props: WalletConnectionProps) {
const expiryTimeSignature = date.toISOString();
setExpiryTime(expiryTimeSignature);

const serializedMessage = isUpdateOperatorTab
? generateUpdateOperatorMessage(expiryTimeSignature, nonce, operator, addOperator)
: generateTransferMessage(expiryTimeSignature, nonce, tokenID, from, to);

if (serializedMessage !== '') {
const promise = connection.signMessage(account, {
type: 'BinaryMessage',
value: serializedMessage,
schema: typeSchemaFromBase64(SERIALIZATION_HELPER_SCHEMA),
});
const payload = isUpdateOperatorTab
? generateUpdateOperatorMessage(nonce, operator, addOperator)
: generateTransferMessage(nonce, tokenID, from, to);

if (payload !== '') {
const promise = signCIS3Message(
connection,
isUpdateOperatorTab,
nonce,
expiryTimeSignature,
account,
payload,
);

promise
.then((permitSignature) => {
Expand Down
81 changes: 45 additions & 36 deletions sponsoredTransactionsAuction/frontend/src/components/Bid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ import { useState } from 'react';
import { useForm, useWatch } from 'react-hook-form';
import { Alert, Button, Form } from 'react-bootstrap';

import { TransactionHash, serializeTypeValue, toBuffer } from '@concordium/web-sdk';
import { WalletConnection, typeSchemaFromBase64 } from '@concordium/react-components';

import { Buffer } from 'buffer/';

import {
SERIALIZATION_HELPER_SCHEMA_ADDITIONAL_DATA,
SERIALIZATION_HELPER_SCHEMA_PERMIT_MESSAGE,
DOBEN marked this conversation as resolved.
Show resolved Hide resolved
TRANSFER_SCHEMA,
} from '../constants';
AccountTransactionSignature,
EntrypointName,
Parameter,
serializeTypeValue,
toBuffer,
TransactionHash,
} from '@concordium/web-sdk';
import { WalletConnection } from '@concordium/react-components';

import { SERIALIZATION_HELPER_SCHEMA_ADDITIONAL_DATA, SPONSORED_TX_CONTRACT_NAME, TRANSFER_SCHEMA } from '../constants';
import { submitBid, validateAccountAddress } from '../utils';

import { viewItemState } from '../auction_contract';
Expand All @@ -22,9 +23,7 @@ import * as AuctionContract from '../../generated/sponsored_tx_enabled_auction_s
*/
async function generateTransferMessage(
DOBEN marked this conversation as resolved.
Show resolved Hide resolved
setTokenID: (arg0: string) => void,
expiryTimeSignature: string,
account: string,
nonce: string,
amount: string | undefined,
itemIndexAuction: string,
) {
Expand Down Expand Up @@ -70,30 +69,40 @@ async function generateTransferMessage(
},
];

const payload = serializeTypeValue(transfer, toBuffer(TRANSFER_SCHEMA, 'base64'));

const message = {
contract_address: {
index: Number(process.env.CIS2_TOKEN_CONTRACT_INDEX),
subindex: 0,
},
nonce: Number(nonce),
timestamp: expiryTimeSignature,
entry_point: 'transfer',
payload: Array.from(payload.buffer),
};

const serializedMessage = serializeTypeValue(
message,
toBuffer(SERIALIZATION_HELPER_SCHEMA_PERMIT_MESSAGE, 'base64'),
);
const payload = Parameter.toHexString(serializeTypeValue(transfer, toBuffer(TRANSFER_SCHEMA, 'base64')));

return serializedMessage;
return payload;
} catch (error) {
throw new Error(`Generating transfer message failed. Orginal error: ${(error as Error).message}`);
}
}

function signCIS3Message(
connection: WalletConnection,
nonce: string,
expiryTimeSignature: string,
account: string,
payload: string,
): Promise<AccountTransactionSignature> {
const contractAddress = { index: Number(process.env.CIS2_TOKEN_CONTRACT_INDEX), subindex: 0 };
const contractName = SPONSORED_TX_CONTRACT_NAME;
const entrypointName = EntrypointName.fromString('transfer');
const _nonce = Number(nonce);
const payloadMessage = { data: payload, schema: TRANSFER_SCHEMA };

// @ts-expect-error Temporary expected ts-error, will be fixed with WalletConnection update
// eslint-disable-next-line @typescript-eslint/no-unsafe-return,@typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access
DOBEN marked this conversation as resolved.
Show resolved Hide resolved
return connection.client.signCIS3Message(
contractAddress,
contractName,
entrypointName,
_nonce,
expiryTimeSignature,
account,
payloadMessage,
);
}

interface ConnectionProps {
account: string | undefined;
connection: WalletConnection;
Expand Down Expand Up @@ -150,18 +159,18 @@ export default function Bid(props: ConnectionProps) {
try {
const serializedMessage = await generateTransferMessage(
setTokenID,
expiryTimeSignature,
account,
data.nonce,
data.tokenAmount,
data.itemIndex,
);

const permitSignature = await connection.signMessage(account, {
type: 'BinaryMessage',
value: Buffer.from(serializedMessage.buffer),
schema: typeSchemaFromBase64(SERIALIZATION_HELPER_SCHEMA_PERMIT_MESSAGE),
});
const permitSignature = await signCIS3Message(
connection,
data.nonce,
expiryTimeSignature,
account,
serializedMessage,
);

setSignature(permitSignature[0][0]);
} catch (err) {
Expand Down