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

feat: wallet-connect v2 #114

Draft
wants to merge 26 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
a0e7f1d
feat(walleteconnect): v2
triniwiz Jan 14, 2023
a84c8fb
feat(wallet-connect): android v2
triniwiz Jan 15, 2023
2157a38
chore(wallet-connect): ios v2 clean up
triniwiz Jan 15, 2023
db013cb
feat(wallet-connect): v2 impl
triniwiz Jan 15, 2023
21e5c31
feat(wallet-connect): client impl
triniwiz Jan 15, 2023
c65394f
feat(wallet-connect): expose enums & codable class values
triniwiz Jan 16, 2023
bd77f1e
feat(wallet-connect): deserialize & events
triniwiz Jan 16, 2023
5e051e2
Update nativescript-walletconnect.ts
triniwiz Jan 16, 2023
7c58c6a
feat: full API
triniwiz Jan 17, 2023
a8b51f6
chore(wallet-connect): update demo
triniwiz Jan 20, 2023
d7d7faf
feat(wallet-connect): init update
triniwiz Jan 20, 2023
2347450
feat(wallet-connect): ios
triniwiz Jan 20, 2023
a845a55
chroe: match major version with sdk major
NathanWalker Jan 20, 2023
f82ab09
fix: buildscript
triniwiz Jan 21, 2023
d7b2941
feat: android full api
triniwiz Jan 24, 2023
d821298
feat: @nstudio/nativescript-cardview (#116)
NathanWalker Jan 18, 2023
822f1df
chore: bump plugin-tools
NathanWalker Jan 19, 2023
16b8dbd
Merge remote-tracking branch 'origin/main' into feat/wallet-connect-v2
NathanWalker Jan 31, 2023
5a8cd53
chore: 2.0.0-alpha.1
NathanWalker Jan 31, 2023
ec9a00b
fix: cancellable
NathanWalker Feb 4, 2023
ede9fae
fix(walletconnect): resolve from callback
triniwiz Feb 17, 2023
d849629
chore: bump
triniwiz Feb 17, 2023
10376e7
fix(android): arg handling issue with signRespond
NathanWalker Apr 19, 2023
b795607
chore: cleanup ios promises
NathanWalker Apr 26, 2023
c255e50
fix(android): use reflection
NathanWalker Apr 26, 2023
f477278
chore(walletconnect): 2.0.0-alpha.4
NathanWalker Apr 26, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions apps/demo/src/app.ts
Original file line number Diff line number Diff line change
@@ -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' });
187 changes: 179 additions & 8 deletions apps/demo/src/plugin-demos/nativescript-walletconnect.ts
Original file line number Diff line number Diff line change
@@ -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 = <Page>args.object;
Expand All @@ -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,
Expand Down Expand Up @@ -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!' },
},
];
*/
2 changes: 1 addition & 1 deletion apps/demo/src/plugin-demos/nativescript-walletconnect.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<Label text="{{ state }}" />
<Label text="{{ accounts }}" />
<TextView text="{{ wcurl }}" hint="Enter WCURL"/>
<Button text="Create Session" tap="{{ createSession }}" class="btn btn-primary"/>
<Button text="Pair" tap="{{ pair }}" class="btn btn-primary"/>
<Button text="Approve Session" tap="{{ approveSession }}" class="btn btn-primary"/>
<Button text="Open WCUrl" tap="{{ openWCUrl }}" class="btn btn-primary"/>
<Button text="Reject Session" tap="{{ rejectSession }}" class="btn btn-primary"/>
Expand Down
Binary file not shown.
3 changes: 2 additions & 1 deletion packages/nativescript-walletconnect/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export function generateId() {
const datePart = new Date().getTime() * Math.pow(10, 3);
const extraPart = Math.floor(Math.random() * Math.pow(10, 3));
return datePart + extraPart;
}
}

Loading