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

CU-86a0cm2j7 - Update WalletConnect to use new version of NeonInvoker… #95

Merged
merged 6 commits into from
Oct 17, 2023
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@cityofzion/wallet-connect-sdk-core",
"comment": "New method to allow signing a transaction multiple times by different wallets before invoking it",
"type": "minor"
}
],
"packageName": "@cityofzion/wallet-connect-sdk-core"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@cityofzion/wallet-connect-sdk-wallet-core",
"comment": "New method to allow signing a transaction multiple times by different wallets before invoking it",
"type": "minor"
}
],
"packageName": "@cityofzion/wallet-connect-sdk-wallet-core"
}
14 changes: 14 additions & 0 deletions e2e/tests/DappMethods.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,17 @@ 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 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
})
49 changes: 34 additions & 15 deletions examples/wc-dapp-react/src/components/HelloWorld.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,31 @@ const networks: Record<NetworkType, { name: string }> = {
},
}

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<NetworkType>('neo3:testnet')

const connect = async (): Promise<void> => {
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<void> => {
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<void> => {
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<void> => {
await wcSdk.disconnect()
Expand Down Expand Up @@ -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 <div>
{!wcSdk && <span>Loading...</span>}
{wcSdk && (<div>
Expand Down Expand Up @@ -316,6 +334,7 @@ function HelloWorld () {
<button data-testid="hello-world__sign-message-encrypt-and-decrypt" onClick={signMessageEncryptAndDecrypt}>signMessage, Encrypt And Decrypt</button>
<button data-testid="hello-world__decrypt-from-array" onClick={decryptFromArray}>decrypt from array</button>
<button data-testid="hello-world__calculate-fee" onClick={calculateFee}>Calculate Fee</button>
<button data-testid="hello-world__sign-transaction" onClick={signTransaction}>Sign Transaction</button>
<br></br>
<span>Response:</span>
<br></br>
Expand Down
1 change: 1 addition & 0 deletions examples/wc-wallet-react/src/constants/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const DEFAULT_METHODS: Method[] = [
'encrypt',
'decryptFromArray',
'calculateFee',
'signTransaction',
]

export const DEFAULT_LOGGER = "error";
Expand Down
30 changes: 27 additions & 3 deletions packages/wallet-connect-sdk-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -81,8 +81,32 @@ export default class WcSdk implements Neo3Invoker, Neo3Signer {
this.session = initSession
}
}
signTransaction(cim: ContractInvocationMulti | BuiltTransaction):Promise<BuiltTransaction> {
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<BuiltTransaction> {
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
}

/**
Expand Down
9 changes: 8 additions & 1 deletion packages/wallet-connect-sdk-wallet-core/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -127,6 +128,12 @@ export abstract class AbstractWalletConnectNeonAdapter {
return await invoker.calculateFee(params)
}

async signTransaction(args: TAdapterMethodParam): Promise<BuiltTransaction> {
const {invoker} = await this.getServices(args)
const params = this.convertParams(args)
return await invoker.signTransaction(params)
}

abstract getWalletInfo(args: TAdapterMethodParam): Promise<WalletInfo>

abstract getAccountString(args: TAdapterMethodParam): Promise<string>
Expand Down
Loading