Skip to content

Commit

Permalink
CU-86dt1fxtx - Implement Contextual Messages on WcSdk on both sides
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonardoDizConde committed May 15, 2024
1 parent 1d16f87 commit 5f5d4a0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 37 deletions.
5 changes: 0 additions & 5 deletions e2e/src/models/ExampleProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,4 @@ export default class ExampleProject {
await this.awaitTestId(testId)
await (await this.awaitAndGetTestId(testId)).click()
}

async awaitAndFillTestId(testId: string, fillText: string) {
await this.awaitTestId(testId)
await (await this.awaitAndGetTestId(testId)).fill(fillText)
}
}
3 changes: 1 addition & 2 deletions e2e/tests/DappMethods.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,7 @@ test('Test send a contextual with a Verify Success on dapp (React)', async ({ co
const dappPage = DAPP_REACT
const walletPage = WALLET_REACT
await connectReactDappToNewReactAccount(context, dappPage, walletPage)
await dappPage.awaitAndFillTestId('hello-world__contextual_message', DAPP_METHOD_CONTEXT_MESSAGE)
await dappPage.awaitAndClickTestId('hello-world__verify') // Click on verify success button
await dappPage.awaitAndClickTestId('hello-world__verify-with-context')
await acceptPendingRequestToReactWallet(walletPage, async (walletPageBeforeAcceptMethod) => {
const contextMessage = await walletPageBeforeAcceptMethod.awaitAndGetTestId('request-card__contextual_message')
expect(contextMessage).toBeDefined()
Expand Down
68 changes: 39 additions & 29 deletions examples/wc-dapp-react/src/components/HelloWorld.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ function HelloWorld() {
const [response, setResponse] = useState('')
const wcSdk = useWalletConnect()
const [networkType, setNetworkType] = React.useState<NetworkType>('neo3:testnet')
const [context, setContext] = React.useState('')

const connect = async (): Promise<void> => {
await wcSdk.connect(networkType, dappMethods)
Expand All @@ -33,7 +32,7 @@ function HelloWorld() {

const getMyBalance = async (): Promise<void> => {
try {
const resp = await wcSdk.withContext(context).testInvoke({
const resp = await wcSdk.testInvoke({
invocations: [
{
scriptHash: '0xd2a4cff31913016155e38e474a2c06d08be276cf',
Expand All @@ -58,7 +57,7 @@ function HelloWorld() {

const transferGas = async (): Promise<void> => {
try {
const resp = await wcSdk.withContext(context).invokeFunction({
const resp = await wcSdk.invokeFunction({
invocations: [
{
scriptHash: '0xd2a4cff31913016155e38e474a2c06d08be276cf',
Expand Down Expand Up @@ -132,7 +131,7 @@ function HelloWorld() {

const transferGasWithExtraFee = async (): Promise<void> => {
try {
const resp = await wcSdk.withContext(context).invokeFunction({
const resp = await wcSdk.invokeFunction({
invocations: [
{
scriptHash: '0xd2a4cff31913016155e38e474a2c06d08be276cf',
Expand Down Expand Up @@ -171,7 +170,7 @@ function HelloWorld() {

const multiInvokeFailing = async (): Promise<void> => {
try {
const resp = await wcSdk.withContext(context).invokeFunction({
const resp = await wcSdk.invokeFunction({
invocations: [
{
scriptHash: '0x010101c0775af568185025b0ce43cfaa9b990a2a',
Expand Down Expand Up @@ -216,15 +215,15 @@ function HelloWorld() {
const signAndVerify = async (): Promise<void> => {
if (!wcSdk) return
try {
const resp = await wcSdk.withContext(context).signMessage({
const resp = await wcSdk.signMessage({
message: 'Your sign message',
version: SignMessageVersion.DEFAULT,
})

console.log(resp)
setResponse(JSON.stringify(resp, null, 2))

const resp2 = await wcSdk.withContext(context).verifyMessage(resp)
const resp2 = await wcSdk.verifyMessage(resp)

console.log(resp2)
setResponse(JSON.stringify(resp2, null, 2))
Expand All @@ -236,15 +235,15 @@ function HelloWorld() {
const signWithoutSaltAndVerify = async (): Promise<void> => {
if (!wcSdk) return
try {
const resp = await wcSdk.withContext(context).signMessage({
const resp = await wcSdk.signMessage({
message: 'Your sign message',
version: SignMessageVersion.WITHOUT_SALT,
})

console.log(resp)
setResponse(JSON.stringify(resp, null, 2))

const resp2 = await wcSdk.withContext(context).verifyMessage(resp)
const resp2 = await wcSdk.verifyMessage(resp)

console.log(resp2)
setResponse(JSON.stringify(resp2, null, 2))
Expand All @@ -255,7 +254,7 @@ function HelloWorld() {

const verifyFailling = async (): Promise<void> => {
try {
const resp = await wcSdk.withContext(context).verifyMessage({
const resp = await wcSdk.verifyMessage({
data: '4fe1b478cf76564b2133bdff9ba97d8a360ce36d0511918931cda207c2ce589dfc07ec5d8b93ce7c3b70fc88b676cc9e08f9811bf0d5b5710a20f10c58191bfb',
messageHex:
'010001f05c3733336365623464346538666664633833656363366533356334343938393939436172616c686f2c206d756c65712c206f2062616775697520656820697373756d65726d6f2074616978206c696761646f206e61206d697373e36f3f0000',
Expand All @@ -272,7 +271,7 @@ function HelloWorld() {

const verify = async (): Promise<void> => {
try {
const resp = await wcSdk.withContext(context).verifyMessage({
const resp = await wcSdk.verifyMessage({
publicKey: '031757edb62014dea820a0b33a156f6a59fc12bd966202f0e49357c81f26f5de34',
data: 'aeb234ed1639e9fcc95a102633b1c70ca9f9b97e9592cc74bfc40cbc7fefdb19ae8c6b49ebd410dbcbeec6b5906e503d528e34cd5098cc7929dbcbbaf23c5d77',
salt: '052a55a8d56b73b342a8e41da3050b09',
Expand All @@ -289,7 +288,7 @@ function HelloWorld() {

const traverseIterator = async (): Promise<void> => {
try {
const resp = await wcSdk.withContext(context).testInvoke({
const resp = await wcSdk.testInvoke({
invocations: [
{
operation: 'getAllCandidates',
Expand All @@ -305,7 +304,7 @@ function HelloWorld() {
const sessionId = resp.session as string
const iteratorId = resp.stack[0].id as string

const resp2 = await wcSdk.withContext(context).traverseIterator(sessionId, iteratorId, 10)
const resp2 = await wcSdk.traverseIterator(sessionId, iteratorId, 10)

console.log(resp2)
setResponse(JSON.stringify(resp2, null, 2))
Expand All @@ -316,7 +315,7 @@ function HelloWorld() {

const getWalletInfo = async (): Promise<void> => {
try {
const resp = await wcSdk.withContext(context).getWalletInfo()
const resp = await wcSdk.getWalletInfo()

console.log(resp)
setResponse(JSON.stringify(resp, null, 2))
Expand All @@ -334,7 +333,7 @@ function HelloWorld() {
const message = 'Your sign message'
const publicKeys = ['02dd8169fb780a9cc01d785efc96888f99c39ab671c039acad8f1f646b9f944a0e']
try {
const resp = await wcSdk.withContext(context).encrypt(message, publicKeys)
const resp = await wcSdk.encrypt(message, publicKeys)
console.log(resp)
setResponse(JSON.stringify(resp, null, 2))
} catch (e) {
Expand All @@ -351,7 +350,7 @@ function HelloWorld() {
ephemPublicKey: '0278bcb970272910a0bbe69061a435586428e87363018bd0616b3ffebd66774ba9',
randomVector: '1bd2a7e1305a02e3d89dbd277d6272e5',
}
const resp2 = await wcSdk.withContext(context).withContext(context).decrypt(payload)
const resp2 = await wcSdk.decrypt(payload)
console.log({ resp2 })
window.alert(JSON.stringify(resp2, null, 2))
} catch (error) {
Expand All @@ -360,7 +359,7 @@ function HelloWorld() {
}

const signMessageEncryptAndDecrypt = async () => {
const signedMessage = await wcSdk.withContext(context).signMessage({
const signedMessage = await wcSdk.signMessage({
message: 'Message to Sign',
version: SignMessageVersion.DEFAULT,
})
Expand All @@ -370,7 +369,7 @@ function HelloWorld() {
const encrypted = await wcSdk.encrypt(message, publicKeys)

try {
const resp = await wcSdk.withContext(context).decrypt(encrypted[0])
const resp = await wcSdk.decrypt(encrypted[0])
console.log(resp)
window.alert(JSON.stringify(resp, null, 2))
} catch (e) {
Expand All @@ -387,7 +386,7 @@ function HelloWorld() {
ephemPublicKey: '0278bcb970272910a0bbe69061a435586428e87363018bd0616b3ffebd66774ba9',
randomVector: '1bd2a7e1305a02e3d89dbd277d6272e5',
}
const resp2 = await wcSdk.withContext(context).decryptFromArray([payload])
const resp2 = await wcSdk.decryptFromArray([payload])
console.log(resp2)
window.alert(JSON.stringify(resp2, null, 2))
} catch (error) {
Expand All @@ -398,7 +397,7 @@ function HelloWorld() {
const signTransaction = async () => {
// invoke this using the payer account (eg.: NbnjKGMBJzJ6j5PHeYhjJDaQ5Vy5UYu4Fv)
try {
const resp = await wcSdk.withContext(context).signTransaction({
const resp = await wcSdk.signTransaction({
invocations: [
{
scriptHash: '0xd2a4cff31913016155e38e474a2c06d08be276cf',
Expand Down Expand Up @@ -433,7 +432,23 @@ function HelloWorld() {

const wipeMethods = async () => {
try {
const resp = await wcSdk.withContext(context).wipeRequests()
const resp = await wcSdk.wipeRequests()
console.log(resp)
setResponse(JSON.stringify(resp, null, 2))
} catch (e) {
onError(e)
}
}

const verifySuccessWithContext = async () => {
try {
const resp = await wcSdk.withContext("Accept this and your balance goes 'to the moon'!").verifyMessage({
publicKey: '031757edb62014dea820a0b33a156f6a59fc12bd966202f0e49357c81f26f5de34',
data: 'aeb234ed1639e9fcc95a102633b1c70ca9f9b97e9592cc74bfc40cbc7fefdb19ae8c6b49ebd410dbcbeec6b5906e503d528e34cd5098cc7929dbcbbaf23c5d77',
salt: '052a55a8d56b73b342a8e41da3050b09',
messageHex:
'010001f0a0303532613535613864353662373362333432613865343164613330353062303965794a68624763694f694a49557a49314e694973496e523563434936496b705856434a392e65794a6c654841694f6a45324e444d304e7a63324e6a4d73496d6c68644349364d5459304d7a4d354d5449324d33302e7253315f73735230364c426778744831504862774c306d7a6557563950686d5448477a324849524f4a4f340000',
})
console.log(resp)
setResponse(JSON.stringify(resp, null, 2))
} catch (e) {
Expand Down Expand Up @@ -529,18 +544,13 @@ function HelloWorld() {
<button data-testid="hello-world__sign-transaction" onClick={signTransaction}>
Sign Transaction
</button>
<button data-testid="hello-world__verify-with-context" onClick={verifySuccessWithContext}>
Verify Success With Context
</button>
<button data-testid="hello-world__wipe-methods" onClick={wipeMethods}>
Wipe Methods
</button>
<br></br>
Dapp method invocation context (optional):
<br></br>
<textarea
data-testid="hello-world__contextual_message"
placeholder="Write a context message for method invocation"
onChange={(event) => setContext(event.target.value)}
/>
<br></br>
<span>Response:</span>
<br></br>
<span data-testid="hello-world__method-response">{response}</span>
Expand Down
1 change: 0 additions & 1 deletion packages/wallet-connect-sdk-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ export const DEFAULT_AUTO_ACCEPT_METHODS: Method[] = [
'getNetworkVersion',
'calculateFee',
'wipeRequests',
//'withContext'
]

export class WcSdkError extends Error {
Expand Down

0 comments on commit 5f5d4a0

Please sign in to comment.