diff --git a/README.md b/README.md index 056609b0f..77775298d 100644 --- a/README.md +++ b/README.md @@ -45,14 +45,18 @@ const webConnector = new WalletConnect( */ const session = await webConnector.initSession() -const { uri } = session; // Display QR code with URI string +if (session.new) { + const { uri } = session; // Display QR code with URI string +} else { + const { accounts } = session // Get wallet accounts +} /** - * Listen to session status + * Listen to session status (for new sessions) */ -webConnector.listenSessionStatus((err, result) => { - console.log(result) -}) +const sessionStatus = await webConnector.listenSessionStatus() + +const accounts = result.data // Get wallet accounts /** * Draft transaction @@ -67,9 +71,11 @@ const transactionId = await webConnector.createTransaction(tx) /** * Listen to transaction status */ -webConnector.listenTransactionStatus(transactionId, (err, result) => { - console.log(result) -}) +const transactionStatus = await webConnector.listenTransactionStatus(transactionId) + +if (transactionStatus.success) { + const { txHash } = transactionStatus // Get transaction hash +} ``` ### For Wallets (React-Native SDK) @@ -100,24 +106,15 @@ rn-nodeify --install "crypto" --hack ```js import RNWalletConnect from 'rn-walletconnect-wallet' - -/** - * Scan QR code URI to init WalletConnect - */ -onQRCodeScan(string => { - // save qrcode string -}) - - /** - * Create WalletConnector + * Create WalletConnector (using the URI from scanning the QR code) */ -const walletConnector = new RNWalletConnect(string) +const walletConnector = new RNWalletConnect(uri) /** * Send session data */ -walletConnector.sendSessionStatus({ +await walletConnector.sendSessionStatus({ fcmToken: '12354...3adc', pushEndpoint: 'https://push.walletconnect.org/notification/new', data: { @@ -141,7 +138,7 @@ FCM.on(FCMEvent.Notification, event => { /** * Send transaction status */ -walletConnector.sendTransactionStatus({ +await walletConnector.sendTransactionStatus(transactionId, { success: true, txHash: '0xabcd...873' }) diff --git a/packages/walletconnect-core/package-lock.json b/packages/walletconnect-core/package-lock.json index b45c80d36..f23b92112 100644 --- a/packages/walletconnect-core/package-lock.json +++ b/packages/walletconnect-core/package-lock.json @@ -1,6 +1,6 @@ { "name": "js-walletconnect-core", - "version": "0.6.0", + "version": "0.6.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/packages/walletconnect-react-native/README.md b/packages/walletconnect-react-native/README.md index de33d4e6c..9ee25ec0c 100644 --- a/packages/walletconnect-react-native/README.md +++ b/packages/walletconnect-react-native/README.md @@ -28,24 +28,15 @@ rn-nodeify --install "crypto" --hack ```js import RNWalletConnect from 'rn-walletconnect-wallet' - -/** - * Scan QR code URI to init WalletConnect - */ -onQRCodeScan(string => { - // save qrcode string -}) - - /** - * Create WalletConnector + * Create WalletConnector (using the URI from scanning the QR code) */ -const walletConnector = new RNWalletConnect(string) +const walletConnector = new RNWalletConnect(uri) /** * Send session data */ -walletConnector.sendSessionStatus({ +await walletConnector.sendSessionStatus({ fcmToken: '12354...3adc', pushEndpoint: 'https://push.walletconnect.org/notification/new', data: { @@ -69,7 +60,7 @@ FCM.on(FCMEvent.Notification, event => { /** * Send transaction status */ -walletConnector.sendTransactionStatus({ +await walletConnector.sendTransactionStatus(transactionId, { success: true, txHash: '0xabcd...873' }) diff --git a/packages/walletconnect-react-native/package-lock.json b/packages/walletconnect-react-native/package-lock.json index 11a121d91..0ee9e49c6 100644 --- a/packages/walletconnect-react-native/package-lock.json +++ b/packages/walletconnect-react-native/package-lock.json @@ -1,6 +1,6 @@ { "name": "rn-walletconnect-wallet", - "version": "0.6.0", + "version": "0.6.1", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -2921,9 +2921,9 @@ "dev": true }, "js-walletconnect-core": { - "version": "0.6.0", - "resolved": "https://registry.npmjs.org/js-walletconnect-core/-/js-walletconnect-core-0.6.0.tgz", - "integrity": "sha512-6skQ3sjIukVOwzuGHLRSxNwa8XB/r1O+4frH3f20ASVsH8p9hTC+F31P/bmUGYYdeq39W1/cKErS6xjUrh01lg==", + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/js-walletconnect-core/-/js-walletconnect-core-0.6.1.tgz", + "integrity": "sha512-gRvRQ1Cl9uHUyimwUYTTsaQqmJgpt3T6eJ6X0S9O3iboKgRTtvF/BQjhoqaqWURxJaXd8u1mrikyKEyZ/dFGhA==", "requires": { "ajv": "6.5.3", "safe-buffer": "5.1.2", diff --git a/packages/walletconnect/README.md b/packages/walletconnect/README.md index 1423bbdce..9328d9d62 100644 --- a/packages/walletconnect/README.md +++ b/packages/walletconnect/README.md @@ -34,14 +34,18 @@ const webConnector = new WalletConnect( */ const session = await webConnector.initSession() -const { uri } = session; // Display QR code with uri string +if (session.new) { + const { uri } = session; // Display QR code with URI string +} else { + const { accounts } = session // Get wallet accounts +} /** - * Listen to session status + * Listen to session status (for new sessions) */ -webConnector.listenSessionStatus((err, result) => { - console.log(result) -}) +const sessionStatus = await webConnector.listenSessionStatus() + +const accounts = result.data // Get wallet accounts /** * Draft transaction @@ -56,7 +60,12 @@ const transactionId = await webConnector.createTransaction(tx) /** * Listen to transaction status */ -webConnector.listenTransactionStatus(transactionId, (err, result) => { - console.log(result) -}) + /** + * Listen to transaction status + */ + const transactionStatus = await webConnector.listenTransactionStatus(transactionId) + + if (transactionStatus.success) { + const { txHash } = transactionStatus // Get transaction hash + } ``` diff --git a/packages/walletconnect/package-lock.json b/packages/walletconnect/package-lock.json index 6bf6ef630..5a6117004 100644 --- a/packages/walletconnect/package-lock.json +++ b/packages/walletconnect/package-lock.json @@ -1,6 +1,6 @@ { "name": "walletconnect", - "version": "0.6.0", + "version": "0.6.1", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -2921,9 +2921,9 @@ "dev": true }, "js-walletconnect-core": { - "version": "0.6.0", - "resolved": "https://registry.npmjs.org/js-walletconnect-core/-/js-walletconnect-core-0.6.0.tgz", - "integrity": "sha512-6skQ3sjIukVOwzuGHLRSxNwa8XB/r1O+4frH3f20ASVsH8p9hTC+F31P/bmUGYYdeq39W1/cKErS6xjUrh01lg==", + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/js-walletconnect-core/-/js-walletconnect-core-0.6.1.tgz", + "integrity": "sha512-gRvRQ1Cl9uHUyimwUYTTsaQqmJgpt3T6eJ6X0S9O3iboKgRTtvF/BQjhoqaqWURxJaXd8u1mrikyKEyZ/dFGhA==", "requires": { "ajv": "6.5.3", "safe-buffer": "5.1.2", diff --git a/packages/walletconnect/src/index.js b/packages/walletconnect/src/index.js index 67a2a269b..5aa2d0d42 100644 --- a/packages/walletconnect/src/index.js +++ b/packages/walletconnect/src/index.js @@ -23,7 +23,9 @@ export default class WalletConnect extends Connector { Object.keys(savedSessions).forEach(sessionId => { const session = savedSessions[sessionId] const now = Date.now() - return session.expires > now + if (session.expires > now) { + openSessions.push(session) + } }) liveSessions = await Promise.all( openSessions.map(async session => { @@ -44,6 +46,10 @@ export default class WalletConnect extends Connector { liveSessions = liveSessions.filter(session => !!session) } + let session = { + new: false + } + let currentSession = liveSessions && liveSessions.length ? liveSessions[0] : null @@ -53,21 +59,23 @@ export default class WalletConnect extends Connector { this.sharedKey = currentSession.sharedKey this.dappName = currentSession.dappName this.expires = currentSession.expires + session.accounts = currentSession.accounts } else { - currentSession = this.createSession() + currentSession = await this.createSession() + session.new = true + session.uri = currentSession.uri + + // save currentSession on localStorage + this.saveLocalSession(currentSession) } - return currentSession + + return session } // // Create session // async createSession() { - if (this.sessionId) { - throw new Error('session already created') - } - - // create shared key if (!this.sharedKey) { this.sharedKey = await generateKey() } @@ -147,33 +155,42 @@ export default class WalletConnect extends Connector { // // Listen for session status // - listenSessionStatus(cb, pollInterval = 1000, timeout = 60000) { - return new Listener(this, { - fn: () => { - return this.getSessionStatus() - }, - cb, - pollInterval, - timeout + listenSessionStatus(pollInterval = 1000, timeout = 60000) { + return new Promise((resolve, reject) => { + new Listener(this, { + fn: () => { + return this.getSessionStatus() + }, + cb: (err, result) => { + if (err) { + reject(err) + } + resolve(result) + }, + pollInterval, + timeout + }) }) } // // Listen for session status // - listenTransactionStatus( - transactionId, - cb, - pollInterval = 1000, - timeout = 60000 - ) { - return new Listener(this, { - fn: () => { - return this.getTransactionStatus(transactionId) - }, - cb, - pollInterval, - timeout + listenTransactionStatus(transactionId, pollInterval = 1000, timeout = 60000) { + return new Promise((resolve, reject) => { + new Listener(this, { + fn: () => { + return this.getTransactionStatus(transactionId) + }, + cb: (err, result) => { + if (err) { + reject(err) + } + resolve(result) + }, + pollInterval, + timeout + }) }) }