99 lines
4.1 KiB
JavaScript
99 lines
4.1 KiB
JavaScript
|
|
"use strict";
|
||
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
|
const schema_utils_1 = require("@trezor/schema-utils");
|
||
|
|
const utils_1 = require("@trezor/utils");
|
||
|
|
const verifyEntropy_1 = require("../api/firmware/verifyEntropy");
|
||
|
|
const constants_1 = require("../constants");
|
||
|
|
const AbstractMethod_1 = require("../core/AbstractMethod");
|
||
|
|
const prompts_1 = require("../device/prompts");
|
||
|
|
const events_1 = require("../events");
|
||
|
|
const paramsValidator_1 = require("./common/paramsValidator");
|
||
|
|
const pathUtils_1 = require("../utils/pathUtils");
|
||
|
|
class ResetDevice extends AbstractMethod_1.AbstractMethod {
|
||
|
|
init() {
|
||
|
|
this.allowDeviceMode = [events_1.UI.INITIALIZE, events_1.UI.SEEDLESS];
|
||
|
|
this.useDeviceState = false;
|
||
|
|
this.requiredPermissions = ['management'];
|
||
|
|
this.firmwareRange = (0, paramsValidator_1.getFirmwareRange)(this.name, null, this.firmwareRange);
|
||
|
|
const { payload } = this;
|
||
|
|
(0, schema_utils_1.Assert)(constants_1.PROTO.ResetDevice, payload);
|
||
|
|
this.params = {
|
||
|
|
strength: payload.strength || 256,
|
||
|
|
passphrase_protection: payload.passphrase_protection,
|
||
|
|
pin_protection: payload.pin_protection,
|
||
|
|
language: payload.language,
|
||
|
|
label: payload.label,
|
||
|
|
u2f_counter: payload.u2f_counter || Math.floor(Date.now() / 1000),
|
||
|
|
skip_backup: payload.skip_backup,
|
||
|
|
no_backup: payload.no_backup,
|
||
|
|
backup_type: payload.backup_type,
|
||
|
|
entropy_check: typeof payload.entropy_check === 'boolean' ? payload.entropy_check : true,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
get info() {
|
||
|
|
return 'Setup device';
|
||
|
|
}
|
||
|
|
get confirmation() {
|
||
|
|
return {
|
||
|
|
view: 'device-management',
|
||
|
|
label: 'Do you really you want to create a new wallet?',
|
||
|
|
};
|
||
|
|
}
|
||
|
|
async getEntropyData(type) {
|
||
|
|
const cmd = this.device.getCommands();
|
||
|
|
const entropy = (0, verifyEntropy_1.generateEntropy)(32).toString('hex');
|
||
|
|
const params = type === 'ResetDevice' ? this.params : {};
|
||
|
|
const entropyRequest = await cmd.typedCall(type, 'EntropyRequest', params);
|
||
|
|
await cmd.typedCall('EntropyAck', ['EntropyCheckReady', 'Success'], { entropy });
|
||
|
|
return {
|
||
|
|
...entropyRequest.message,
|
||
|
|
host_entropy: entropy,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
async entropyCheck(prevData) {
|
||
|
|
const cmd = this.device.getCommands();
|
||
|
|
const paths = ["m/84'/0'/0'", "m/44'/60'/0'"];
|
||
|
|
const xpubs = {};
|
||
|
|
for (let i = 0; i < paths.length; i++) {
|
||
|
|
const p = paths[i];
|
||
|
|
const pubKey = await cmd.getPublicKey({ address_n: (0, pathUtils_1.validatePath)(p) });
|
||
|
|
xpubs[p] = pubKey.xpub;
|
||
|
|
}
|
||
|
|
const currentData = await this.getEntropyData('EntropyCheckContinue');
|
||
|
|
const res = await (0, verifyEntropy_1.verifyEntropy)({
|
||
|
|
type: this.params.backup_type,
|
||
|
|
strength: this.params.strength,
|
||
|
|
commitment: prevData.entropy_commitment,
|
||
|
|
hostEntropy: prevData.host_entropy,
|
||
|
|
trezorEntropy: currentData.prev_entropy,
|
||
|
|
xpubs,
|
||
|
|
});
|
||
|
|
if (res.error) {
|
||
|
|
await (0, prompts_1.cancelPrompt)(this.device);
|
||
|
|
throw new Error(res.error);
|
||
|
|
}
|
||
|
|
return currentData;
|
||
|
|
}
|
||
|
|
async run() {
|
||
|
|
const cmd = this.device.getCommands();
|
||
|
|
if (this.params.entropy_check && this.device.unavailableCapabilities['entropyCheck']) {
|
||
|
|
this.params.entropy_check = false;
|
||
|
|
}
|
||
|
|
let entropyData = await this.getEntropyData('ResetDevice');
|
||
|
|
if (this.params.entropy_check) {
|
||
|
|
const tries = (0, utils_1.getRandomInt)(1, 5);
|
||
|
|
for (let i = 0; i < tries; i++) {
|
||
|
|
try {
|
||
|
|
entropyData = await this.entropyCheck(entropyData);
|
||
|
|
}
|
||
|
|
catch (error) {
|
||
|
|
throw constants_1.ERRORS.TypedError('Failure_EntropyCheck', error.message);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
await cmd.typedCall('EntropyCheckContinue', 'Success', { finish: true });
|
||
|
|
}
|
||
|
|
return { message: 'Success' };
|
||
|
|
}
|
||
|
|
}
|
||
|
|
exports.default = ResetDevice;
|
||
|
|
//# sourceMappingURL=resetDevice.js.map
|