Skip to content

Commit

Permalink
Merge pull request #18 from WalletConnect/v0.6.2
Browse files Browse the repository at this point in the history
v0.6.2
  • Loading branch information
pedrouid authored Sep 5, 2018
2 parents a4ea454 + 570b551 commit 0ec9f1c
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 80 deletions.
39 changes: 18 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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: {
Expand All @@ -141,7 +138,7 @@ FCM.on(FCMEvent.Notification, event => {
/**
* Send transaction status
*/
walletConnector.sendTransactionStatus({
await walletConnector.sendTransactionStatus(transactionId, {
success: true,
txHash: '0xabcd...873'
})
Expand Down
2 changes: 1 addition & 1 deletion packages/walletconnect-core/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 4 additions & 13 deletions packages/walletconnect-react-native/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -69,7 +60,7 @@ FCM.on(FCMEvent.Notification, event => {
/**
* Send transaction status
*/
walletConnector.sendTransactionStatus({
await walletConnector.sendTransactionStatus(transactionId, {
success: true,
txHash: '0xabcd...873'
})
Expand Down
8 changes: 4 additions & 4 deletions packages/walletconnect-react-native/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 17 additions & 8 deletions packages/walletconnect/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
```
8 changes: 4 additions & 4 deletions packages/walletconnect/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 46 additions & 29 deletions packages/walletconnect/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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

Expand All @@ -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()
}
Expand Down Expand Up @@ -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
})
})
}

Expand Down

0 comments on commit 0ec9f1c

Please sign in to comment.