-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #460 from Concordium/BRO-13-payload-decoding-in-th…
…e-browser-wallet-of-the-cis3-standard [BRO-13] Payload decoding in the browser wallet of the CIS3 standard
- Loading branch information
Showing
14 changed files
with
322 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
packages/browser-wallet/src/popup/pages/SignCIS3Message/SignCIS3Message.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
$message-details-horizontal-padding: rem(20px); | ||
|
||
.sign-cis3-message { | ||
&__details { | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
background-color: $color-bg; | ||
height: 100%; | ||
border: rem(1px) solid $color-grey; | ||
border-radius: rem(10px); | ||
padding: rem(10px) $message-details-horizontal-padding; | ||
|
||
:where(&) h5 { | ||
margin-top: rem(10px); | ||
margin-bottom: 0; | ||
} | ||
} | ||
|
||
&__details-text-area textarea { | ||
min-height: 10rem; | ||
border-top-left-radius: 0; | ||
border-top-right-radius: 0; | ||
font-family: $font-family-mono; | ||
padding: rem(10px); | ||
} | ||
} |
167 changes: 167 additions & 0 deletions
167
packages/browser-wallet/src/popup/pages/SignCIS3Message/SignCIS3Message.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
import React, { useCallback, useContext, useEffect, useState } from 'react'; | ||
import { useLocation } from 'react-router-dom'; | ||
import clsx from 'clsx'; | ||
import { stringify } from 'json-bigint'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { useSetAtom } from 'jotai'; | ||
import { Buffer } from 'buffer/'; | ||
import { | ||
AccountAddress, | ||
AccountTransactionSignature, | ||
buildBasicAccountSigner, | ||
ContractAddress, | ||
ContractName, | ||
deserializeTypeValue, | ||
EntrypointName, | ||
serializeTypeValue, | ||
signMessage, | ||
} from '@concordium/web-sdk'; | ||
import { fullscreenPromptContext } from '@popup/page-layouts/FullscreenPromptLayout'; | ||
import { usePrivateKey } from '@popup/shared/utils/account-helpers'; | ||
import { displayUrl } from '@popup/shared/utils/string-helpers'; | ||
import { TextArea } from '@popup/shared/Form/TextArea'; | ||
import ConnectedBox from '@popup/pages/Account/ConnectedBox'; | ||
import Button from '@popup/shared/Button'; | ||
import { addToastAtom } from '@popup/state'; | ||
import ExternalRequestLayout from '@popup/page-layouts/ExternalRequestLayout'; | ||
import { SignMessageObject } from '@concordium/browser-wallet-api-helpers'; | ||
|
||
const SERIALIZATION_HELPER_SCHEMA = | ||
'FAAFAAAAEAAAAGNvbnRyYWN0X2FkZHJlc3MMBQAAAG5vbmNlBQkAAAB0aW1lc3RhbXANCwAAAGVudHJ5X3BvaW50FgEHAAAAcGF5bG9hZBABAg=='; | ||
|
||
type Props = { | ||
onSubmit(signature: AccountTransactionSignature): void; | ||
onReject(): void; | ||
}; | ||
|
||
interface Location { | ||
state: { | ||
payload: { | ||
accountAddress: string; | ||
message: SignMessageObject; | ||
url: string; | ||
cis3ContractDetails: Cis3ContractDetailsObject; | ||
}; | ||
}; | ||
} | ||
|
||
type Cis3ContractDetailsObject = { | ||
contractAddress: ContractAddress.Type; | ||
contractName: ContractName.Type; | ||
entrypointName: EntrypointName.Type; | ||
nonce: bigint | number; | ||
expiryTimeSignature: string; | ||
}; | ||
|
||
async function parseMessage(message: SignMessageObject) { | ||
return stringify( | ||
deserializeTypeValue(Buffer.from(message.data, 'hex'), Buffer.from(message.schema, 'base64')), | ||
undefined, | ||
2 | ||
); | ||
} | ||
|
||
function serializeMessage(payloadMessage: SignMessageObject, cis3ContractDetails: Cis3ContractDetailsObject) { | ||
const { contractAddress, entrypointName, nonce, expiryTimeSignature } = cis3ContractDetails; | ||
const message = { | ||
contract_address: { | ||
index: Number(contractAddress.index), | ||
subindex: Number(contractAddress.subindex), | ||
}, | ||
nonce: Number(nonce), | ||
timestamp: expiryTimeSignature, | ||
entry_point: EntrypointName.toString(entrypointName), | ||
payload: Array.from(Buffer.from(payloadMessage.data, 'hex')), | ||
}; | ||
|
||
return serializeTypeValue(message, Buffer.from(SERIALIZATION_HELPER_SCHEMA, 'base64')); | ||
} | ||
|
||
function MessageDetailsDisplay({ | ||
payloadMessage, | ||
cis3ContractDetails, | ||
}: { | ||
payloadMessage: SignMessageObject; | ||
cis3ContractDetails: Cis3ContractDetailsObject; | ||
}) { | ||
const { t } = useTranslation('signCIS3Message'); | ||
const { contractAddress, contractName, entrypointName, nonce, expiryTimeSignature } = cis3ContractDetails; | ||
const [parsedMessage, setParsedMessage] = useState<string>(''); | ||
const expiry = new Date(expiryTimeSignature).toString(); | ||
|
||
useEffect(() => { | ||
parseMessage(payloadMessage) | ||
.then((m) => setParsedMessage(m)) | ||
.catch(() => setParsedMessage(t('unableToDeserialize'))); | ||
}, []); | ||
|
||
return ( | ||
<div className="m-10 sign-cis3-message__details"> | ||
<h5>{t('contractIndex')}:</h5> | ||
<div> | ||
{contractAddress.index.toString()} ({contractAddress.subindex.toString()}) | ||
</div> | ||
<h5>{t('receiveName')}:</h5> | ||
<div> | ||
{contractName.value.toString()}.{entrypointName.value.toString()} | ||
</div> | ||
<h5>{t('nonce')}:</h5> | ||
<div>{nonce.toString()}</div> | ||
<h5>{t('expiry')}:</h5> | ||
<div>{expiry}</div> | ||
<h5>{t('parameter')}:</h5> | ||
<TextArea | ||
readOnly | ||
className={clsx('m-b-10 w-full flex-child-fill sign-cis3-message__details-text-area')} | ||
value={parsedMessage} | ||
/> | ||
</div> | ||
); | ||
} | ||
|
||
export default function SignCIS3Message({ onSubmit, onReject }: Props) { | ||
const { state } = useLocation() as Location; | ||
const { t } = useTranslation('signCIS3Message'); | ||
const { withClose } = useContext(fullscreenPromptContext); | ||
const { accountAddress, url, message, cis3ContractDetails } = state.payload; | ||
const key = usePrivateKey(accountAddress); | ||
const addToast = useSetAtom(addToastAtom); | ||
const onClick = useCallback(async () => { | ||
if (!key) { | ||
throw new Error('Missing key for the chosen address'); | ||
} | ||
|
||
return signMessage( | ||
AccountAddress.fromBase58(accountAddress), | ||
serializeMessage(message, cis3ContractDetails).buffer, | ||
buildBasicAccountSigner(key) | ||
); | ||
}, [state.payload.message, state.payload.accountAddress, key]); | ||
|
||
return ( | ||
<ExternalRequestLayout className="p-10"> | ||
<ConnectedBox accountAddress={accountAddress} url={new URL(url).origin} /> | ||
<div className="h-full flex-column align-center"> | ||
<h3 className="m-t-0 text-center">{t('description', { dApp: displayUrl(url) })}</h3> | ||
<p className="m-t-0 text-center">{t('descriptionWithSchema', { dApp: displayUrl(url) })}</p> | ||
<MessageDetailsDisplay payloadMessage={message} cis3ContractDetails={cis3ContractDetails} /> | ||
<br /> | ||
<div className="flex p-b-10 p-t-10 m-t-auto"> | ||
<Button width="narrow" className="m-r-10" onClick={withClose(onReject)}> | ||
{t('reject')} | ||
</Button> | ||
<Button | ||
width="narrow" | ||
onClick={() => | ||
onClick() | ||
.then(withClose(onSubmit)) | ||
.catch((e) => addToast(e.message)) | ||
} | ||
> | ||
{t('sign')} | ||
</Button> | ||
</div> | ||
</div> | ||
</ExternalRequestLayout> | ||
); | ||
} |
17 changes: 17 additions & 0 deletions
17
packages/browser-wallet/src/popup/pages/SignCIS3Message/i18n/da.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import type en from './en'; | ||
|
||
const t: typeof en = { | ||
description: '{{ dApp }} anmoder om en signatur på følgende besked', | ||
descriptionWithSchema: | ||
'{{ dApp }} har sendt en rå besked og et schema til at oversætte den. Vi har oversat beskeden, men du burde kun underskrive hvis du stoler på {{ dApp }}', | ||
unableToDeserialize: 'Det var ikke muligt at oversætte beskeden', | ||
contractIndex: 'Kontrakt indeks (under indeks)', | ||
receiveName: 'Kontrakt og funktions navn', | ||
parameter: 'Parameter', | ||
nonce: 'Nonce', | ||
expiry: 'Udløber', | ||
sign: 'Signér', | ||
reject: 'Afvis', | ||
}; | ||
|
||
export default t; |
15 changes: 15 additions & 0 deletions
15
packages/browser-wallet/src/popup/pages/SignCIS3Message/i18n/en.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const t = { | ||
description: '{{ dApp }} requests a signature on a message', | ||
descriptionWithSchema: | ||
"{{ dApp }} has provided the raw message and a schema to render it. We've rendered the message but you should only sign it if you trust {{ dApp }}.", | ||
unableToDeserialize: 'Unable to render message', | ||
contractIndex: 'Contract index (subindex)', | ||
receiveName: 'Contract and function name', | ||
parameter: 'Parameter', | ||
nonce: 'Nonce', | ||
expiry: 'Expiry time', | ||
sign: 'Sign', | ||
reject: 'Reject', | ||
}; | ||
|
||
export default t; |
1 change: 1 addition & 0 deletions
1
packages/browser-wallet/src/popup/pages/SignCIS3Message/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './SignCIS3Message'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.