gridplus-sdk
Advanced tools
Comparing version 0.2.0-dev to 0.2.1-dev
{ | ||
"name": "gridplus-sdk", | ||
"version": "0.2.0-dev", | ||
"version": "0.2.1-dev", | ||
"description": "SDK to interact with GridPlus Lattice1 device", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -56,6 +56,15 @@ const superagent = require('superagent'); | ||
// Information about the current wallet. Should be null unless we know a wallet is present | ||
this.activeWallet = { | ||
uid: EMPTY_WALLET_UID, // 32 byte id | ||
name: null, // 20 char (max) string | ||
capabilities: null, // 4 byte flag | ||
this.activeWallets = { | ||
internal: { | ||
uid: EMPTY_WALLET_UID, // 32 byte id | ||
name: null, // 20 char (max) string | ||
capabilities: null, // 4 byte flag | ||
external: false, | ||
}, | ||
external: { | ||
uid: EMPTY_WALLET_UID, // 32 byte id | ||
name: null, // 20 char (max) string | ||
capabilities: null, // 4 byte flag | ||
external: true, | ||
} | ||
} | ||
@@ -128,3 +137,5 @@ } | ||
// WalletUID | ||
this.activeWallet.uid.copy(payload, off); off += 32; | ||
const wallet = this.getActiveWallet(); | ||
if (wallet === null) return cb('No active wallet.'); | ||
wallet.uid.copy(payload, off); off += 32; | ||
// Build the start path (5x u32 indices) | ||
@@ -220,3 +231,3 @@ for (let i = 0; i < startPath.length; i++) { | ||
if (err) { | ||
this._resetActiveWallet(); | ||
this._resetActiveWallets(); | ||
return cb('Error getting active wallet.'); | ||
@@ -320,3 +331,3 @@ } | ||
// If we caugh a `ErrWalletNotPresent` make sure we aren't caching an old ative walletUID | ||
if (parsed.responseCode === deviceResponses.ERR_WRONG_WALLET_UID) this._resetActiveWallet(); | ||
if (parsed.responseCode === deviceResponses.ERR_WRONG_WALLET_UID) this._resetActiveWallets(); | ||
// If there was an error in the response, return it | ||
@@ -429,13 +440,27 @@ if (parsed.err) return cb(parsed.err); | ||
const walletDescriptorLen = 71; | ||
for (let i = 1; i > -1; i--) { // loop through internal and external wallet data | ||
const off = 65 + i * walletDescriptorLen; // Skip 65byte pubkey prefix. WalletDescriptor contains 32byte id + 4byte flag + 35byte name | ||
const walletUID = res.slice(off, off+32); | ||
if (!walletUID.equals(EMPTY_WALLET_UID)) { | ||
this.activeWallet.uid = walletUID; | ||
this.activeWallet.capabilities = res.readUInt32BE(off+32); | ||
this.activeWallet.name = res.slice(off+36, off+walletDescriptorLen); | ||
return null; | ||
} | ||
// Skip 65byte pubkey prefix. WalletDescriptor contains 32byte id + 4byte flag + 35byte name | ||
let off = 65; | ||
// Internal first | ||
let hasActiveWallet = false; | ||
walletUID = res.slice(off, off+32); | ||
if (!walletUID.equals(EMPTY_WALLET_UID)) { | ||
this.activeWallets.internal.uid = walletUID; | ||
this.activeWallets.internal.capabilities = res.readUInt32BE(off+32); | ||
this.activeWallets.internal.name = res.slice(off+36, off+walletDescriptorLen); | ||
hasActiveWallet = true; | ||
} | ||
return 'No active wallet.'; | ||
// Offset the first item | ||
off += walletDescriptorLen; | ||
// External | ||
walletUID = res.slice(off, off+32); | ||
if (!walletUID.equals(EMPTY_WALLET_UID)) { | ||
this.activeWallets.external.uid = walletUID; | ||
this.activeWallets.external.capabilities = res.readUInt32BE(off+32); | ||
this.activeWallets.external.name = res.slice(off+36, off+walletDescriptorLen); | ||
hasActiveWallet = true; | ||
} | ||
if (hasActiveWallet === true) | ||
return null; | ||
else | ||
return 'No active wallet.'; | ||
} | ||
@@ -546,11 +571,24 @@ | ||
_resetActiveWallet() { | ||
this.activeWallet.uid = EMPTY_WALLET_UID; | ||
this.activeWallet.name = null; | ||
this.activeWallet.capabilities = null; | ||
_resetActiveWallets() { | ||
this.activeWallets.internal.uid = EMPTY_WALLET_UID; | ||
this.activeWallets.internal.name = null; | ||
this.activeWallets.internal.capabilities = null; | ||
this.activeWallets.external.uid = EMPTY_WALLET_UID; | ||
this.activeWallets.external.name = null; | ||
this.activeWallets.external.capabilities = null; | ||
return; | ||
} | ||
getActiveWallet() { | ||
if (!EMPTY_WALLET_UID.equals(this.activeWallets.external.uid)) { | ||
return this.activeWallets.external; | ||
} else if (!EMPTY_WALLET_UID.equals(this.activeWallets.internal.uid)) { | ||
return this.activeWallets.internal; | ||
} else { | ||
return null; | ||
} | ||
} | ||
hasActiveWallet() { | ||
return !EMPTY_WALLET_UID.equals(this.activeWallet.uid); | ||
return this.getActiveWallet() !== null; | ||
} | ||
@@ -557,0 +595,0 @@ |
51276
1198