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

Add integration with WalletConnect #98

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
3902738
Add connection to Simple Signer Demo
Genebson Feb 12, 2022
0f8222d
Merge branch 'main' of https://github.com/fsodano/simple-stellar-sign…
Genebson Feb 12, 2022
c6d2d6a
Install wallets dependencies
Genebson Feb 13, 2022
4480e42
Add wallets logos
Genebson Feb 13, 2022
be50f0e
Add test
Genebson Feb 13, 2022
9b6ecb8
Fix test
Genebson Feb 13, 2022
4f3b074
Add global window
Genebson Feb 13, 2022
e851335
Add clear storage
Genebson Feb 13, 2022
1b67eb0
Add new store
Genebson Feb 13, 2022
f04717c
Add connect methods
Genebson Feb 13, 2022
3a9f448
Change file folder
Genebson Feb 13, 2022
ad97919
Add xBull sign feature
Genebson Feb 13, 2022
dba7394
New connect UI
Genebson Feb 13, 2022
f3bb033
Add albedo in the browser from UNPKG
Ja-boop Feb 16, 2022
8874e33
Refactor messageHandler
Ja-boop Feb 16, 2022
726e58f
Add wallets interfaces also add to global window
Ja-boop Feb 16, 2022
ee21160
Delete clear
Ja-boop Feb 16, 2022
69203fa
Refactor components and wallets classes
Ja-boop Feb 16, 2022
7a8ffec
Delete sign helper, and refactor test
Ja-boop Feb 16, 2022
7f2058e
Add clear storage, delete return
Ja-boop Feb 16, 2022
dfa6619
Add option to jest config
Ja-boop Feb 16, 2022
b6e845d
Improve message handler
Ja-boop Feb 16, 2022
d249787
Refactor cypress test
Ja-boop Feb 16, 2022
6fc4f6b
Merge branch 'main' into connect-wallets
Ja-boop Feb 16, 2022
7c1a201
Merge branch 'main' of https://github.com/fsodano/simple-stellar-sign…
Ja-boop Feb 21, 2022
72abd96
Merge branch 'main' of https://github.com/fsodano/simple-stellar-sign…
RivarolaNicolas Feb 22, 2022
c674473
Adds PoC of WalletConnect implementation
RivarolaNicolas Feb 27, 2022
c0a0d9e
Merge branch 'main' of https://github.com/fsodano/simple-stellar-sign…
RivarolaNicolas Mar 16, 2022
60f9687
Merge branch 'main' of https://github.com/fsodano/simple-stellar-sign…
RivarolaNicolas Mar 16, 2022
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
1 change: 1 addition & 0 deletions .env.dist
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
VITE_HORIZON_URL=https://horizon-testnet.stellar.org
VITE_TEST_PRIVATEKEY=PRIVATE_KEY
VITE_WALLET_CONNECT_PROJECT_ID=PROJECT_ID
3 changes: 2 additions & 1 deletion cypress/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
/// <reference types="cypress" />
// ***********************************************************
// This example plugins/index.js can be used to load plugins
Expand All @@ -15,7 +16,7 @@
/**
* @type {Cypress.PluginConfig}
*/
// eslint-disable-next-line no-unused-vars

module.exports = (on, config) => {
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
Expand Down
9,875 changes: 4,305 additions & 5,570 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@
},
"dependencies": {
"@stellar/freighter-api": "^1.1.2",
"@walletconnect/client": "^2.0.0-beta.21",
"@walletconnect/qrcode-modal": "^2.0.0-alpha.5",
"stellar-sdk": "^10.0.0"
}
}
1 change: 1 addition & 0 deletions src/assets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export { default as albedo } from './logo-wallets/albedo.svg';
export { default as xBull } from './logo-wallets/xbull.svg';
export { default as privateKey } from './logo-wallets/private-key.svg';
export { default as freighter } from './logo-wallets/freighter.svg';
export { default as walletConnect } from './logo-wallets/wallet-connect.svg';
export { default as rabet } from './logo-wallets/rabet.svg';
18 changes: 18 additions & 0 deletions src/assets/logo-wallets/wallet-connect.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/helpers/interfaces/ISimpleSignerEvent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default interface ISimpleSignerEvent {
type: string;
message: any;
message: unknown;
}
52 changes: 41 additions & 11 deletions src/routes/connect/Connect.svelte
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
<script lang="ts">
import Albedo from './ui/wallets/Albedo';
import Freighter from './ui/wallets/Freighter';
// import Freighter from './ui/wallets/Freighter';
import XBull from './ui/wallets/XBull';
import PrivateKey from './ui/wallets/PrivateKey';
import Rabet from './ui/wallets/Rabet';
import { inputValue, isPrivateKeyVisible, isWalletHidden } from './connectStore';
import { albedo, xBull, freighter, privateKey, rabet } from '../../assets/index';
// import WalletConnect from './ui/wallets/WalletConnect';

// const PUBNET = 'stellar:pubnet';
// const STELLAR_METHODS = {
// SIGN: 'stellar_signXDR',
// };

async function connectWithAlbedo() {
const albedo = new Albedo();
return albedo.logIn(await albedo.getPublicKey());
}

async function connectWithFreighter() {
const freighter = new Freighter();
return freighter.logIn(await freighter.getPublicKey());
}
// async function connectWithFreighter() {
// const freighter = new Freighter();
// return freighter.logIn(await freighter.getPublicKey());
// }

async function connectWithXBull() {
const xbull = new XBull();
Expand All @@ -30,6 +36,13 @@
async function connectWithSecretKey(privateKey: string): Promise<void> {
return new PrivateKey().logIn(privateKey);
}

// async function connectWithWalletConnect() {
// const walletConnect = new WalletConnect();
// const client = await walletConnect.initClient();
// const pairing = await walletConnect.createPairing(client);
// const session = await walletConnect.createSession(client);
// }
</script>

<div class="simple-signer-container">
Expand Down Expand Up @@ -63,7 +76,7 @@
</a>
</div>
<div class="simple-signer freighter-container">
<a href="{'#'}" class="connect-freighter" on:click="{() => connectWithFreighter()}">
<a href="{'#'}" class="connect-freighter">
<img
class="simple-signer freighter-logo"
src="{freighter}"
Expand Down Expand Up @@ -92,6 +105,18 @@
<p class="simple-signer wallet-private-key-title">Private Key</p>
</a>
</div>
<!-- <div class="simple-signer wallet-connect-container">
<a href="{'#'}" class="connect-wallet-connect" on:click="{() => connectWithWalletConnect()}">
<img
class="simple-signer wallet-connect-logo"
src="{walletConnect}"
alt="wallet connect logo"
width="45"
height="45"
/>
<p class="simple-signer wallet-connect-title">Wallet Connect</p>
</a>
</div> -->
</div>
{/if}
</div>
Expand All @@ -111,7 +136,8 @@
.rabet-logo,
.albedo-logo,
.xbull-logo,
.private-key-logo {
.private-key-logo
/* .wallet-connect-logo */ {
margin-top: 25px;
}
.freighter-logo {
Expand All @@ -120,7 +146,8 @@
.wallet-rabet-title,
.wallet-albedo-title,
.wallet-xbull-title,
.wallet-private-key-title {
.wallet-private-key-title
/* .wallet-connect-title { */ {
margin-top: 10px;
}

Expand All @@ -132,7 +159,8 @@
.wallet-albedo-title:hover,
.wallet-xbull-title:hover,
.wallet-private-key-title:hover,
.wallet-freighter-title:hover {
.wallet-freighter-title:hover
/* .wallet-connect-title:hover */ {
color: #000;
}

Expand All @@ -144,7 +172,8 @@
.albedo-container,
.freighter-container,
.xbull-container,
.private-key-container {
.private-key-container
/* .wallet-connect-container */ {
font-family: 'Montserrat', sans-serif;
font-size: 14px;
color: #bdbdbd;
Expand All @@ -158,7 +187,8 @@
.albedo-container,
.freighter-container,
.xbull-container,
.private-key-container {
.private-key-container
/* .wallet-connect-container */ {
margin-top: 15px;
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/routes/connect/ui/wallets/Wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Freighter from './Freighter';
import PrivateKey from './PrivateKey';
import Rabet from './Rabet';
import XBull from './XBull';
// import WalletConnect from './WalletConnect';

export default class WalletFactory {
create(name: string) {
Expand All @@ -25,6 +26,9 @@ export default class WalletFactory {
case PrivateKey.NAME:
wallet = new PrivateKey();
break;
// case WalletConnect.NAME:
// wallet = new WalletConnect();
// break;
default:
throw new InvalidWalletException();
}
Expand Down
146 changes: 146 additions & 0 deletions src/routes/connect/ui/wallets/WalletConnect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import WalletConnectClient from '@walletconnect/client';
import { CLIENT_EVENTS } from '@walletconnect/client';
import type { Client } from '@walletconnect/client/dist/cjs/client';
import QRCodeModal from '@walletconnect/qrcode-modal';
import type { PairingTypes, SessionTypes } from '@walletconnect/types';

// import { sendMessage } from '../../../../helpers/sendMessageHelpers';
import type IWalletConnect from './interfaces/IWalletConnect';

const PUBNET = 'stellar:pubnet';
const STELLAR_METHODS = {
SIGN: 'stellar_signXDR',
};

export default class WalletConnect implements IWalletConnect {
public static NAME = 'walletconnect';

async initClient() {
const client = await WalletConnectClient.init({
projectId: import.meta.env.VITE_WALLET_CONNECT_PROJECT_ID,
relayUrl: 'wss://relay.walletconnect.org',
metadata: {
name: 'Simple Signer Stellar #3',
description: 'Example Dapp',
url: 'https://localhost:3000/',
icons: ['https://walletconnect.com/walletconnect-logo.png'],
},
});
return client;
}

async createPairing(client: Client) {
client.on(CLIENT_EVENTS.pairing.proposal, async (proposal: PairingTypes.Proposal) => {
const { uri } = proposal.signal.params;
console.log('create pairing');
QRCodeModal.open(uri, async () => QRCodeModal.close());
console.log(proposal, 'proposal');
});
}

async createSession(client: Client) {
const session = await client.connect({
permissions: {
blockchain: {
chains: [PUBNET],
},
jsonrpc: {
methods: [STELLAR_METHODS.SIGN],
},
},
});
return session;
}
async deleteSession(session: SessionTypes.Settled, client: Client) {
client.disconnect({
topic: session.topic,
reason: {
code: 0,
message: 'Disconnecting WalletConnect from Simple Signer for additional security',
},
});
}
async sign(session: SessionTypes.RequestParams, client: Client) {
const result = await client.request({
topic: session.topic,
chainId: PUBNET,
request: {
method: 'stellar_signXDR',
params: {
xdr: 'AAAAAgAAAACs3d1Ffb9O6rqEUlrxKCPte210i4eyOUvlZh/M1QUZKAAAAGQCXVArAAAAAwAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAQAAAACs3d1Ffb9O6rqEUlrxKCPte210i4eyOUvlZh/M1QUZKAAAAAAAAAAAAJiWgAAAAAAAAAAA',
},
},
});
return result;
}
}
// const TESTNET = 'stellar:testnet';
// const PUBNET = 'stellar:pubnet';
// const STELLAR_METHODS = {
// SIGN: 'stellar_signXDR',
// };
// export let clientAndSessionArray: (WalletConnectClient | SessionTypes.Settled)[];

// export default async function connectWithWalletConnect() {
// const client = await WalletConnectClient.init({
// projectId: import.meta.env.VITE_WALLET_CONNECT_PROJECT_ID,
// relayUrl: 'wss://relay.walletconnect.org',
// metadata: {
// name: 'Simple Signer Stellar #3',
// description: 'Example Dapp',
// url: 'https://localhost:3000',
// icons: ['https://walletconnect.com/walletconnect-logo.png'],
// },
// });

// client.on(CLIENT_EVENTS.pairing.proposal, async (proposal: PairingTypes.Proposal) => {
// const { uri } = proposal.signal.params;
// QRCodeModal.open(uri, async () => QRCodeModal.close());
// });

// const session = await client.connect({
// permissions: {
// blockchain: {
// chains: [PUBNET],
// },
// jsonrpc: {
// methods: [STELLAR_METHODS.SIGN],
// },
// },
// });
// }

// export async function sign() {
// const client = await WalletConnectClient.init({
// projectId: import.meta.env.VITE_WALLET_CONNECT_PROJECT_ID,
// relayUrl: 'wss://relay.walletconnect.org',
// metadata: {
// name: 'Simple Signer Stellar',
// description: 'Use Simple Signer to easily support multiple wallets',
// url: 'https://localhost:3000',
// icons: ['https://walletconnect.com/walletconnect-logo.png'],
// },
// });

// let sessions = client.session.values;
// client.on(CLIENT_EVENTS.session.sync, () => {
// sessions = client.session.values;
// });

// const currentSession = sessions.map((session) => {
// console.log(session);
// });
// console.log(sessions);
// const result = await client.request({
// topic: currentSession!.topic,
// chainId: PUBNET,
// request: {
// method: 'stellar_signXDR',
// params: {
// xdr: 'AAAAAgAAAACs3d1Ffb9O6rqEUlrxKCPte210i4eyOUvlZh/M1QUZKAAAAGQCXVArAAAAAwAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAQAAAACs3d1Ffb9O6rqEUlrxKCPte210i4eyOUvlZh/M1QUZKAAAAAAAAAAAAJiWgAAAAAAAAAAA',
// },
// },
// });
// console.log(result);
// return result;
// }
15 changes: 15 additions & 0 deletions src/routes/connect/ui/wallets/interfaces/IWalletConnect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { Client } from '@walletconnect/client/dist/cjs/client';

import type { SessionTypes, ClientTypes } from '@walletconnect/types';

export default interface IWalletConnect {
initClient: () => Promise<Client>;

createPairing: (client: Client) => Promise<void>;

createSession: (client: Client) => Promise<SessionTypes.Settled>;

deleteSession: (session: SessionTypes.Settled, client: Client) => Promise<void>;

sign: (session: SessionTypes.RequestParams, client: Client, xdr: string) => Promise<ClientTypes.RespondParams>;
}
2 changes: 2 additions & 0 deletions src/vite-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

interface ImportMetaEnv {
readonly VITE_HORIZON_NETWORK_PASSPHRASE: string;
readonly VITE_HOST_SIMPLE_SIGNER_DEMO: string;
readonly VITE_WALLET_CONNECT_PROJECT_ID: string;
readonly VITE_STELLAR_NETWORK: string;
}

Expand Down