Skip to content

Commit

Permalink
Responds to feedback from PR #413
Browse files Browse the repository at this point in the history
  • Loading branch information
douglance committed Jun 27, 2022
1 parent 3ba8c3b commit 10eb9e0
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 54 deletions.
1 change: 0 additions & 1 deletion src/__test__/unit/__snapshots__/validators.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ exports[`validators > fetchActiveWallet > should successfully validate 1`] = `

exports[`validators > getAddresses > should successfully validate 1`] = `
{
"flag": 1,
"fwVersion": {
"data": [
0,
Expand Down
27 changes: 13 additions & 14 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
sign
} from './functions/index';
import { retryWrapper } from './shared/functions';
import { validateEphemeralPub } from './shared/validators';
import { getP256KeyPair, getP256KeyPairFromPub, randomBytes } from './util';

/**
Expand All @@ -25,9 +26,9 @@ export class Client {
public isPaired: boolean;
/** The time to wait for a response before cancelling. */
public timeout: number;
/** The remote url to which the SDK sends requests. */
/** The base of the remote url to which the SDK sends requests. */
public baseUrl: string;
/** @internal */
/** @internal The `baseUrl` plus the `deviceId`. Set in {@link connect} when it completes successfully. */
public url?: string;
/** `name` is a human readable string associated with this app on the Lattice */
private name: string;
Expand Down Expand Up @@ -109,9 +110,8 @@ export class Client {

/** @internal */
public set ephemeralPub (ephemeralPub: KeyPair) {
if (ephemeralPub) {
this._ephemeralPub = ephemeralPub;
}
validateEphemeralPub(ephemeralPub)
this._ephemeralPub = ephemeralPub;
}

/**
Expand All @@ -126,7 +126,7 @@ export class Client {
id: deviceId,
client: this,
},
retries: 3,
retries: this.retryCount,
client: this
})
}
Expand All @@ -144,7 +144,7 @@ export class Client {
pairingSecret,
client: this,
},
retries: 3,
retries: this.retryCount,
client: this
})
}
Expand All @@ -166,7 +166,7 @@ export class Client {
flag,
client: this
},
retries: 3,
retries: this.retryCount,
client: this
})
}
Expand All @@ -180,7 +180,6 @@ export class Client {
currency,
cachedData,
nextCode,
retries = 3
}: SignRequestParams): Promise<SignData> {
return retryWrapper({
fn: sign,
Expand All @@ -191,7 +190,7 @@ export class Client {
nextCode,
client: this,
},
retries,
retries: this.retryCount,
client: this
})
}
Expand All @@ -205,7 +204,7 @@ export class Client {
params: {
client: this,
},
retries: 3,
retries: this.retryCount,
client: this
})
}
Expand All @@ -227,7 +226,7 @@ export class Client {
caseSensitive,
client: this,
},
retries: 3,
retries: this.retryCount,
client: this
})
}
Expand All @@ -249,7 +248,7 @@ export class Client {
start,
client: this,
},
retries: 3,
retries: this.retryCount,
client: this
})
}
Expand All @@ -269,7 +268,7 @@ export class Client {
ids,
client: this,
},
retries: 3,
retries: this.retryCount,
client: this
})
}
Expand Down
7 changes: 5 additions & 2 deletions src/functions/fetchActiveWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ import {
} from '../shared/validators';

/**
* Fetch the active wallet in the device.
* @returns callback with an error or null
* Fetch the active wallet in the device.
*
* The Lattice has two wallet interfaces: internal and external. If a SafeCard is inserted and
* unlocked, the external interface is considered "active" and this will return its {@link Wallet}
* data. Otherwise it will return the info for the internal Lattice wallet.
*/
export async function fetchActiveWallet ({
client,
Expand Down
18 changes: 8 additions & 10 deletions src/functions/getAddresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@ import {
import { isValidAssetPath } from '../util';

/**
* `getAddresses` takes a starting path and a number to get the addresses associated with the
* active wallet.
* `getAddresses` takes a starting path and a number to get the addresses or public keys associated
* with the active wallet.
* @category Lattice
* @returns An array of addresses.
* @returns An array of addresses or public keys.
*/
export async function getAddresses ({
startPath,
n,
flag: _flag,
flag,
client,
}: GetAddressesRequestFunctionParams): Promise<Buffer[]> {
const { url, fwVersion, wallet, sharedSecret, flag } =
const { url, fwVersion, wallet, sharedSecret } =
validateGetAddressesRequest({
startPath,
n,
flag: _flag,
flag,
url: client.url,
fwVersion: client.fwVersion,
wallet: client.getActiveWallet(),
Expand Down Expand Up @@ -79,7 +79,7 @@ export const validateGetAddressesRequest = ({
}: ValidateGetAddressesRequestParams) => {
validateStartPath(startPath);
validateNAddresses(n);
const validFlag = validateIsUInt4(flag);
validateIsUInt4(flag);
const validUrl = validateUrl(url);
const validFwVersion = validateFwVersion(fwVersion);
const validWallet = validateWallet(wallet);
Expand All @@ -90,7 +90,6 @@ export const validateGetAddressesRequest = ({
fwVersion: validFwVersion,
wallet: validWallet,
sharedSecret: validSharedSecret,
flag: validFlag,
};
};

Expand Down Expand Up @@ -175,9 +174,8 @@ export const requestGetAddresses = async (payload: Buffer, url: string) => {
};

/**
* @category Device Response
* @internal
* @return an array of address strings
* @return an array of address strings or pubkey buffers
*/
export const decodeGetAddresses = (data: any, flag: number): Buffer[] => {
let off = 65; // Skip 65 byte pubkey prefix
Expand Down
3 changes: 1 addition & 2 deletions src/shared/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { responseMsgs } from '../constants';
const buildLatticeResponseErrorMessage = ({ responseCode, errorMessage }) => {
const msg: string[] = [];
if (responseCode) {
msg.push(`Response Code: ${responseCode}`);
msg.push(`Message: ${responseMsgs[responseCode]}`);
msg.push(`${responseMsgs[responseCode]}`);
}
if (errorMessage) {
msg.push('Error Message: ');
Expand Down
48 changes: 25 additions & 23 deletions src/shared/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
ENC_MSG_LEN,
EXTERNAL,
REQUEST_TYPE_BYTE,
responseMsgs,
VERSION_BYTE,
} from '../constants';
import ethereum from '../ethereum';
Expand Down Expand Up @@ -225,33 +224,36 @@ export const retryWrapper = async ({
retries,
client,
}) => {
return fn({ ...params }).catch(async (err) => {
const errorMessage = err.errorMessage;
const responseCode = err.responseCode;
return fn({ ...params })
.catch(async (err) => {
/** `string` returned from the Lattice if there's an error */
const errorMessage = err.errorMessage;
/** `number` returned from the Lattice if there's an error */
const responseCode = err.responseCode;

if ((errorMessage || responseCode) && retries) {
if ((errorMessage || responseCode) && retries) {

if (isDeviceBusy(responseCode)) {
await sleep(3000);
}
if (isDeviceBusy(responseCode)) {
await sleep(3000);
}

if (isWrongWallet(responseCode) && !client.skipRetryOnWrongWallet) {
await client.fetchActiveWallet();
}
if (isWrongWallet(responseCode) && !client.skipRetryOnWrongWallet) {
await client.fetchActiveWallet();
}

if (isInvalidEphemeralId(responseCode)) {
await client.connect(client.deviceId);
}
if (isInvalidEphemeralId(responseCode)) {
await client.connect(client.deviceId);
}

return retryWrapper({
fn,
params,
retries: retries - 1,
client,
});
}
throw err;
});
return retryWrapper({
fn,
params,
retries: retries - 1,
client,
});
}
throw err;
});
};

/**
Expand Down
3 changes: 1 addition & 2 deletions src/shared/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export const validateIsUInt4 = (n?: number) => {
};

export const validateNAddresses = (n: number) => {
validateIsUInt4(n);
if (n > MAX_ADDR)
throw new Error(`You may only request ${MAX_ADDR} addresses at once.`);
};
Expand Down Expand Up @@ -84,7 +83,7 @@ export const validateFwConstants = (fwConstants?: FirmwareConstants) => {
return fwConstants;
};
export const validateFwVersion = (fwVersion?: Buffer) => {
if (!fwVersion) {
if (!fwVersion || fwVersion.byteLength > 4) {
throw new Error('Firmware version does not exist. Please reconnect.');
}
return fwVersion;
Expand Down

0 comments on commit 10eb9e0

Please sign in to comment.