From 5390542c26910003fb5ac8591a07f70747dd3dc8 Mon Sep 17 00:00:00 2001 From: Kenedy Ribeiro Date: Mon, 16 Oct 2023 18:33:47 -0300 Subject: [PATCH 1/6] CU-86a0cm2j7 - Update WalletConnect to use new version of NeonInvoker to allow different wallets signing the same transaction --- .../src/components/HelloWorld.tsx | 49 +++++++++++++------ 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/examples/wc-dapp-react/src/components/HelloWorld.tsx b/examples/wc-dapp-react/src/components/HelloWorld.tsx index c533da6..6781f1c 100644 --- a/examples/wc-dapp-react/src/components/HelloWorld.tsx +++ b/examples/wc-dapp-react/src/components/HelloWorld.tsx @@ -14,31 +14,31 @@ const networks: Record = { }, } -function HelloWorld () { - const [dappUri, setDappUri] = useState(''); - const [response, setResponse] = useState(''); +function HelloWorld() { + const [dappUri, setDappUri] = useState(''); + const [response, setResponse] = useState(''); const wcSdk = useWalletConnect() const [networkType, setNetworkType] = React.useState('neo3:testnet') const connect = async (): Promise => { await wcSdk.connect(networkType, [ 'invokeFunction', 'testInvoke', 'signMessage', 'verifyMessage', 'traverseIterator', 'getWalletInfo', - 'getNetworkVersion', 'decrypt', 'encrypt', 'decryptFromArray', 'calculateFee' + 'getNetworkVersion', 'decrypt', 'encrypt', 'decryptFromArray', 'calculateFee', 'signTransaction' ]) } - const getUri = async (): Promise => { - const { uri, approval } = await wcSdk.createConnection('neo3:testnet', [ - 'invokeFunction', 'testInvoke', 'signMessage', 'verifyMessage', 'traverseIterator', 'getWalletInfo', - 'getNetworkVersion', 'decrypt', 'encrypt', 'decryptFromArray', 'calculateFee' - ]) - if(uri) { - setDappUri(uri); - await navigator.clipboard.writeText(uri) - const session = await approval() - wcSdk.setSession(session); + const getUri = async (): Promise => { + const { uri, approval } = await wcSdk.createConnection('neo3:testnet', [ + 'invokeFunction', 'testInvoke', 'signMessage', 'verifyMessage', 'traverseIterator', 'getWalletInfo', + 'getNetworkVersion', 'decrypt', 'encrypt', 'decryptFromArray', 'calculateFee', 'signTransaction' + ]) + if (uri) { + setDappUri(uri); + await navigator.clipboard.writeText(uri) + const session = await approval() + wcSdk.setSession(session); + } } - } const disconnect = async (): Promise => { await wcSdk.disconnect() @@ -284,6 +284,24 @@ function HelloWorld () { } } + const signTransaction = async () => { + const resp = await wcSdk.signTransaction({ + invocations: [{ + scriptHash: '0xd2a4cff31913016155e38e474a2c06d08be276cf', + operation: 'transfer', + args: [ + { type: 'Hash160', value: wcSdk.getAccountAddress() ?? '' }, + { type: 'Hash160', value: 'NbnjKGMBJzJ6j5PHeYhjJDaQ5Vy5UYu4Fv' }, + { type: 'Integer', value: '100000000' }, + { type: 'Array', value: [] } + ] + }], + signers: [{ scopes: 1 }] + }) + console.log(resp) + setResponse(JSON.stringify(resp, null, 2)) + } + return
{!wcSdk && Loading...} {wcSdk && (
@@ -316,6 +334,7 @@ function HelloWorld () { +

Response:

From 64f340a7b35bbd730c55e8655e90a86b806bad66 Mon Sep 17 00:00:00 2001 From: Kenedy Ribeiro Date: Mon, 16 Oct 2023 18:45:19 -0300 Subject: [PATCH 2/6] CU-86a0cm2j7 - Update WalletConnect to use new version of NeonInvoker to allow different wallets signing the same transaction --- .../CU-86a0cm2j7_2023-10-16-21-44.json | 10 +++++++ .../CU-86a0cm2j7_2023-10-16-21-44.json | 10 +++++++ e2e/tests/DappMethods.spec.ts | 13 ++++++++ packages/wallet-connect-sdk-core/src/index.ts | 30 +++++++++++++++++-- .../src/adapter.ts | 9 +++++- 5 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 common/changes/@cityofzion/wallet-connect-sdk-core/CU-86a0cm2j7_2023-10-16-21-44.json create mode 100644 common/changes/@cityofzion/wallet-connect-sdk-wallet-core/CU-86a0cm2j7_2023-10-16-21-44.json diff --git a/common/changes/@cityofzion/wallet-connect-sdk-core/CU-86a0cm2j7_2023-10-16-21-44.json b/common/changes/@cityofzion/wallet-connect-sdk-core/CU-86a0cm2j7_2023-10-16-21-44.json new file mode 100644 index 0000000..f17aa8b --- /dev/null +++ b/common/changes/@cityofzion/wallet-connect-sdk-core/CU-86a0cm2j7_2023-10-16-21-44.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@cityofzion/wallet-connect-sdk-core", + "comment": "Added a method for the wallet to sign a transaction", + "type": "minor" + } + ], + "packageName": "@cityofzion/wallet-connect-sdk-core" +} \ No newline at end of file diff --git a/common/changes/@cityofzion/wallet-connect-sdk-wallet-core/CU-86a0cm2j7_2023-10-16-21-44.json b/common/changes/@cityofzion/wallet-connect-sdk-wallet-core/CU-86a0cm2j7_2023-10-16-21-44.json new file mode 100644 index 0000000..af4de15 --- /dev/null +++ b/common/changes/@cityofzion/wallet-connect-sdk-wallet-core/CU-86a0cm2j7_2023-10-16-21-44.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@cityofzion/wallet-connect-sdk-wallet-core", + "comment": "Added a method for the wallet to sign a transaction", + "type": "minor" + } + ], + "packageName": "@cityofzion/wallet-connect-sdk-wallet-core" +} \ No newline at end of file diff --git a/e2e/tests/DappMethods.spec.ts b/e2e/tests/DappMethods.spec.ts index 9764b6b..243aa2c 100644 --- a/e2e/tests/DappMethods.spec.ts +++ b/e2e/tests/DappMethods.spec.ts @@ -147,3 +147,16 @@ test('Test Calculate Fee on dapp (React)', async ({ context }) => { expect(response.systemFee).toBeDefined() // Verify if the response returned systemFee expect(response.total).toBeDefined() // Verify if the response returned total }) + +test('Test Sign Transaction on dapp (React)', async ({ context }) => { + // Define the dapp and wallet pages + const dappPage = DAPP_REACT + const walletPage = WALLET_REACT + await connectReactDappToNewReactAccount(context, dappPage, walletPage) + await dappPage.page.waitForLoadState('networkidle') // Wait to load request + await dappPage.awaitSeconds(5) // Wait for 5 seconds + await dappPage.page.getByTestId('hello-world__sign-transaction').click() // Click on Sign Transaction button + await dappPage.awaitSeconds(2) // Wait for 2 seconds + const response = await getAnyFromInnerHTML(dappPage.page.getByTestId('hello-world__method-response')) + expect(response).toBeDefined() // Verify if the response had a return +}) \ No newline at end of file diff --git a/packages/wallet-connect-sdk-core/src/index.ts b/packages/wallet-connect-sdk-core/src/index.ts index aa35ccd..640786e 100644 --- a/packages/wallet-connect-sdk-core/src/index.ts +++ b/packages/wallet-connect-sdk-core/src/index.ts @@ -14,7 +14,7 @@ export type Chain = "private" | "testnet" | "mainnet" export type NetworkType = `${Blockchain}:${Chain}` -export type Method = 'invokeFunction' | 'testInvoke' | 'signMessage' | 'verifyMessage' | 'traverseIterator' | 'getWalletInfo' | "getNetworkVersion" | "encrypt" | "decrypt" | "decryptFromArray" | "calculateFee" +export type Method = 'invokeFunction' | 'testInvoke' | 'signMessage' | 'verifyMessage' | 'traverseIterator' | 'getWalletInfo' | "getNetworkVersion" | "encrypt" | "decrypt" | "decryptFromArray" | "calculateFee" | "signTransaction" /** * A number that will be compared by the wallet to check if it is compatible with the dApp @@ -81,8 +81,32 @@ export default class WcSdk implements Neo3Invoker, Neo3Signer { this.session = initSession } } - signTransaction(cim: ContractInvocationMulti | BuiltTransaction):Promise { - throw new Error("not implemented yet"); + + /** + * This method is used to sign a transaction. + * @param params the contract invocation options + * @return the call result promise + */ + async signTransaction(params: ContractInvocationMulti | BuiltTransaction): Promise { + this.validateContractInvocationMulti(params) + const request = { + id: 1, + jsonrpc: "2.0", + method: "signTransaction", + params + } + + const resp = await this.signClient.request({ + topic: this.session?.topic ?? '', + chainId: this.getChainId() ?? '', + request + }) + + if (!resp) { + throw new WcSdkError(resp); + } + + return resp as BuiltTransaction } /** diff --git a/packages/wallet-connect-sdk-wallet-core/src/adapter.ts b/packages/wallet-connect-sdk-wallet-core/src/adapter.ts index bb59908..7ce8cf3 100644 --- a/packages/wallet-connect-sdk-wallet-core/src/adapter.ts +++ b/packages/wallet-connect-sdk-wallet-core/src/adapter.ts @@ -11,7 +11,8 @@ import { ContractInvocationMulti, EncryptedPayload, DecryptFromArrayResult, - CalculateFee + CalculateFee, + BuiltTransaction } from '@cityofzion/wallet-connect-sdk-core' export abstract class AbstractWalletConnectNeonAdapter { protected async getServices(args: TAdapterMethodParam) { @@ -127,6 +128,12 @@ export abstract class AbstractWalletConnectNeonAdapter { return await invoker.calculateFee(params) } + async signTransaction(args: TAdapterMethodParam): Promise { + const {invoker} = await this.getServices(args) + const params = this.convertParams(args) + return await invoker.signTransaction(params) + } + abstract getWalletInfo(args: TAdapterMethodParam): Promise abstract getAccountString(args: TAdapterMethodParam): Promise From bfc1ed82bda30b063fb0e4363f6589f33397872f Mon Sep 17 00:00:00 2001 From: Kenedy Ribeiro Date: Tue, 17 Oct 2023 10:07:41 -0300 Subject: [PATCH 3/6] CU-86a0cm2j7 - Update WalletConnect to use new version of NeonInvoker to allow different wallets signing the same transaction --- examples/wc-wallet-react/src/constants/default.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/wc-wallet-react/src/constants/default.ts b/examples/wc-wallet-react/src/constants/default.ts index 9ffc983..63037e9 100644 --- a/examples/wc-wallet-react/src/constants/default.ts +++ b/examples/wc-wallet-react/src/constants/default.ts @@ -16,6 +16,7 @@ export const DEFAULT_METHODS: Method[] = [ 'encrypt', 'decryptFromArray', 'calculateFee', + 'signTransaction', ] export const DEFAULT_LOGGER = "error"; From b1b91ff8a677a9229040b5a72be8f4c6c6b7ade1 Mon Sep 17 00:00:00 2001 From: Kenedy Ribeiro Date: Tue, 17 Oct 2023 10:45:34 -0300 Subject: [PATCH 4/6] CU-86a0cm2j7 - Update WalletConnect to use new version of NeonInvoker to allow different wallets signing the same transaction --- e2e/tests/DappMethods.spec.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/e2e/tests/DappMethods.spec.ts b/e2e/tests/DappMethods.spec.ts index 243aa2c..bc3491d 100644 --- a/e2e/tests/DappMethods.spec.ts +++ b/e2e/tests/DappMethods.spec.ts @@ -156,6 +156,7 @@ test('Test Sign Transaction on dapp (React)', async ({ context }) => { await dappPage.page.waitForLoadState('networkidle') // Wait to load request await dappPage.awaitSeconds(5) // Wait for 5 seconds await dappPage.page.getByTestId('hello-world__sign-transaction').click() // Click on Sign Transaction button + await acceptPendingRequestToReactWallet(walletPage) await dappPage.awaitSeconds(2) // Wait for 2 seconds const response = await getAnyFromInnerHTML(dappPage.page.getByTestId('hello-world__method-response')) expect(response).toBeDefined() // Verify if the response had a return From b6cc4f7e82cee9b8cb6c55404316f1723eae62c8 Mon Sep 17 00:00:00 2001 From: Gil Lopes Bueno Date: Tue, 17 Oct 2023 15:42:24 -0300 Subject: [PATCH 5/6] Update common/changes/@cityofzion/wallet-connect-sdk-core/CU-86a0cm2j7_2023-10-16-21-44.json --- .../wallet-connect-sdk-core/CU-86a0cm2j7_2023-10-16-21-44.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/changes/@cityofzion/wallet-connect-sdk-core/CU-86a0cm2j7_2023-10-16-21-44.json b/common/changes/@cityofzion/wallet-connect-sdk-core/CU-86a0cm2j7_2023-10-16-21-44.json index f17aa8b..f014cee 100644 --- a/common/changes/@cityofzion/wallet-connect-sdk-core/CU-86a0cm2j7_2023-10-16-21-44.json +++ b/common/changes/@cityofzion/wallet-connect-sdk-core/CU-86a0cm2j7_2023-10-16-21-44.json @@ -2,7 +2,7 @@ "changes": [ { "packageName": "@cityofzion/wallet-connect-sdk-core", - "comment": "Added a method for the wallet to sign a transaction", + "comment": "New method to allow signing a transaction multiple times by different wallets before invoking it", "type": "minor" } ], From de59b12cb825e716f6fcccfc6e45be9797f63f17 Mon Sep 17 00:00:00 2001 From: Gil Lopes Bueno Date: Tue, 17 Oct 2023 15:42:30 -0300 Subject: [PATCH 6/6] Update common/changes/@cityofzion/wallet-connect-sdk-wallet-core/CU-86a0cm2j7_2023-10-16-21-44.json --- .../CU-86a0cm2j7_2023-10-16-21-44.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/changes/@cityofzion/wallet-connect-sdk-wallet-core/CU-86a0cm2j7_2023-10-16-21-44.json b/common/changes/@cityofzion/wallet-connect-sdk-wallet-core/CU-86a0cm2j7_2023-10-16-21-44.json index af4de15..ad578be 100644 --- a/common/changes/@cityofzion/wallet-connect-sdk-wallet-core/CU-86a0cm2j7_2023-10-16-21-44.json +++ b/common/changes/@cityofzion/wallet-connect-sdk-wallet-core/CU-86a0cm2j7_2023-10-16-21-44.json @@ -2,7 +2,7 @@ "changes": [ { "packageName": "@cityofzion/wallet-connect-sdk-wallet-core", - "comment": "Added a method for the wallet to sign a transaction", + "comment": "New method to allow signing a transaction multiple times by different wallets before invoking it", "type": "minor" } ],