diff --git a/apps/demo/src/app.ts b/apps/demo/src/app.ts index 2e765dd9..4bad3996 100644 --- a/apps/demo/src/app.ts +++ b/apps/demo/src/app.ts @@ -1,6 +1,65 @@ import { Application } from '@nativescript/core'; +import { Client, eth_personalSign, eth_signTypedData, init, PrivateKey } from '@nstudio/nativescript-walletconnect'; +init(); // import { Inquiry } from '@nstudio/nativescript-persona'; // Inquiry.init(); +declare const com, io; +Client.initialize('a296125a15cc255e4aa63fffdc421458', null, { + description: 'WalletConnect Developer App', + url: 'https://walletconnect.org', + icons: ['https://walletconnect.org/walletconnect-logo.png'], + name: 'WalletConnect', +}); + +console.dir((io as any).nstudio.plugins.walletconnect.NSCWalletConnectV2.Numeric); + +const key = new PrivateKey(); + + +const data = ['0x4d7920656d61696c206973206a6f686e40646f652e636f6d202d2031363734353533313833303131', '0x8e210C23Fc543B754F7eA84EC10e1aDA0Efe944b']; +console.log(eth_personalSign(key, data)); + + +const typedData = { + types: { + EIP712Domain: [ + { name: "name", type: "string" }, + { name: "version", type: "string" }, + { name: "chainId", type: "uint256" }, + { name: "verifyingContract", type: "address" }, + ], + Person: [ + { name: "name", type: "string" }, + { name: "account", type: "address" }, + ], + Mail: [ + { name: "from", type: "Person" }, + { name: "to", type: "Person" }, + { name: "contents", type: "string" }, + ], + }, + primaryType: "Mail", + domain: { + name: "Example Dapp", + version: "1.0", + chainId: 1, + verifyingContract: "0x0000000000000000000000000000000000000000", + }, + message: { + from: { + name: "Alice", + account: "0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + }, + to: { + name: "Bob", + account: "0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + }, + contents: "Hey, Bob!", + }, + }; + + console.log(eth_signTypedData(key, typedData)); + Application.run({ moduleName: 'app-root' }); diff --git a/apps/demo/src/plugin-demos/nativescript-walletconnect.ts b/apps/demo/src/plugin-demos/nativescript-walletconnect.ts index 9e6ea0c5..9d9f57d3 100644 --- a/apps/demo/src/plugin-demos/nativescript-walletconnect.ts +++ b/apps/demo/src/plugin-demos/nativescript-walletconnect.ts @@ -1,8 +1,10 @@ import { Observable, EventData, Page, Application, Dialogs } from '@nativescript/core'; import { DemoSharedNativescriptWalletconnect } from '@demo/shared'; -import { toHex, WalletConnect } from '@nstudio/nativescript-walletconnect'; +import { Client, Namespaces, PrivateKey, eth_signTypedData, eth_personalSign, eth_sendTransaction, eth_sign, eth_signTransaction } from '@nstudio/nativescript-walletconnect'; -import { PrivateKey, CoinType, CoinTypeInstance, Utils } from '@nstudio/nativescript-walletconnect/utils'; +//import {PrivateKey, CoinType, CoinTypeInstance, Utils} from '@nstudio/nativescript-walletconnect/utils'; + +declare const io; export function navigatingTo(args: EventData) { const page = args.object; @@ -11,18 +13,158 @@ export function navigatingTo(args: EventData) { export class DemoModel extends DemoSharedNativescriptWalletconnect { state: 'Disconnected'; - wallet: WalletConnect; + //wallet: Client; wcurl: string = ''; - accounts: string; - key: PrivateKey; - address: string; + accounts: string[]; + key = new PrivateKey('0xe56da0e170b5e09a8bb8f1b693392c7d56c3739a9c75740fbc558a2877868540'); constructor() { super(); - this.key = new PrivateKey('ba005cd605d8a02e3d5dfd04234cef3a3ee4f76bfbad2722d1fb5af8e12e6764'); - this.address = CoinType.getInstance(CoinTypeInstance.Ethereum).deriveAddress(this.key); + Client.instance.sign.on('session_settle', (args) => { + const event = args.event; + const namespaces = event.namespaces; + const firstNs = Object.keys(namespaces)[0]; + this.accounts = namespaces[firstNs].accounts; + console.log('session_settle', event); + }); + + Client.instance.sign.on('session_proposal', (args) => { + const meta = args.event.proposer; + Dialogs.confirm({ + title: `Connect to ${meta.name}`, + message: `${meta.description}\n${meta.url}`, + okButtonText: 'Approve', + cancelButtonText: 'Reject', + }).then((done) => { + if (done) { + const proposed = args.event.requiredNamespaces; + const ns: Namespaces = {}; + + Object.keys(proposed).forEach((key) => { + const value = proposed[key]; + const accounts = value.chains.map((id) => { + return `${id}:${this.key.address.hex(id.indexOf('eip55') > -1)}`; + }); + this.accounts = accounts; + ns[key] = { + accounts, + methods: value.methods, + chains: value.chains, + events: value.events, + extension: + value?.extensions?.map?.((item) => { + return { ...item, accounts }; + }) ?? undefined, + }; + }); + + Client.instance.sign.approve({ id: args.event.id, namespaces: ns }).catch((e) => { + console.log('session_proposal', 'approve', 'error', e); + }); + } else { + Client.instance.sign.reject({ id: args.event.id, reason: 'rejected' }).catch((e) => { + console.log('session_proposal', 'reject', 'error', e); + }); + } + }); + }); + + Client.instance.sign.on('session_event', (args) => { + console.log('session_event', args.event); + }); + + Client.instance.sign.on('session_ping', (args) => { + console.log('session_ping', args.event?.topic); + }); + + Client.instance.sign.on('session_request', (args) => { + const event = args.event; + const request = event.request; + const method = request.method; + const isTransaction = method === 'eth_signTransaction' || method === 'eth_sendTransaction'; + + console.log(request.params); + + const message = JSON.stringify(request?.params); + + Dialogs.confirm({ + title: method, + message, + okButtonText: isTransaction ? 'Approve' : 'Sign', + cancelButtonText: isTransaction ? 'Deny' : 'Cancel', + }).then((done) => { + if (done) { + let value; + + if (!this.accounts) { + const first = Client.instance.sign.getSessions()[0]; + const namespaces = first?.namespaces; + const firstNs = Object.keys(namespaces)[0]; + this.accounts = namespaces[firstNs].accounts; + } + + if (!isTransaction) { + switch (method) { + case 'eth_sign': + value = eth_sign(this.key, request.params); + break; + case 'personal_sign': + value = eth_personalSign(this.key, request.params); + break; + case 'eth_signTypedData': + value = eth_signTypedData(this.key, request.params); + break; + } + } else { + if (method === 'eth_signTransaction') { + value = eth_signTransaction(this.key, request.params); + console.log(value); + } else { + value = eth_sendTransaction(this.key, request.params); + } + } + console.log('aaa', value); + + console.log({ topic: event.topic, id: request.id, response: value }); + + Client.instance.sign.respond({ topic: event.topic, id: request.id, response: value }); + } else { + Client.instance.sign.respond({ + id: event.request.id, + topic: event.topic, + response: { + code: 1000, + message: 'User Rejected Session Request', + }, + }); + } + }); + }); + + Client.instance.sign.on('session_update', (args) => { + console.log(args.event.namespaces); + console.log('session_update', args.event); + }); + + Client.instance.sign.on('session_delete', (args) => { + console.log('session_delete', args.event); + }); + + // this.key = new PrivateKey('ba005cd605d8a02e3d5dfd04234cef3a3ee4f76bfbad2722d1fb5af8e12e6764'); + + // this.address = CoinType.getInstance(CoinTypeInstance.Ethereum).deriveAddress(this.key); + } + + pair() { + Client.instance.pair.pair({ uri: this.wcurl }); + + Client.instance.pair.getPairings().forEach((item) => { + Client.instance.pair.disconnect({ topic: item.topic }); + }); } + /* + resetConnection() { this.wallet = new WalletConnect({ uri: this.wcurl, @@ -179,4 +321,33 @@ export class DemoModel extends DemoSharedNativescriptWalletconnect { rejectSession() { this.wallet.rejectSession(); } + + */ } + +/* +[ + { + types: { + EIP712Domain: [ + { name: 'name', type: 'string' }, + { name: 'version', type: 'string' }, + { name: 'chainId', type: 'uint256' }, + { name: 'verifyingContract', type: 'address' }, + ], + Person: [ + { name: 'name', type: 'string' }, + { name: 'wallet', type: 'address' }, + ], + Mail: [ + { name: 'from', type: 'Person' }, + { name: 'to', type: 'Person' }, + { name: 'contents', type: 'string' }, + ], + }, + primaryType: 'Mail', + domain: { name: 'Ether Mail', version: '1', chainId: 1, verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC' }, + message: { from: { name: 'Cow', wallet: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826' }, to: { name: 'Bob', wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB' }, contents: 'Hello, Bob!' }, + }, +]; +*/ diff --git a/apps/demo/src/plugin-demos/nativescript-walletconnect.xml b/apps/demo/src/plugin-demos/nativescript-walletconnect.xml index 6fb298f4..f19438f4 100644 --- a/apps/demo/src/plugin-demos/nativescript-walletconnect.xml +++ b/apps/demo/src/plugin-demos/nativescript-walletconnect.xml @@ -9,7 +9,7 @@