Skip to content

Commit

Permalink
Minor renamings
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeuz committed Jun 10, 2024
1 parent a9b6800 commit ad896e1
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -884,10 +884,11 @@ class Device extends DeviceBase {
}

/**
* @typedef {Object} UnprotectDeviceResult
* @typedef {Object} SetProtectedStateResult
* @property {Boolean} protected If `true`, device protection is enabled.
* @property {Buffer} [deviceNonce] Device nonce.
* @property {Buffer} [deviceSignatute] Device signature.
* @property {Buffer} [devicePublicKeyFingerprint] Fingerprint of the device public key.
*/

/**
Expand All @@ -897,28 +898,29 @@ class Device extends DeviceBase {
* @param {String} param.action `prepare`, `confirm` or `reset`.
* @param {Buffer} [param.serverNonce] Server nonce. Mandatory if `action` is `prepare`.
* @param {Buffer} [param.serverSignature] Server signature. Mandatory if `action` is `confirm`.
* @returns {Promise<UnprotectDeviceResult>}
* @param {Buffer} [param.serverPublicKeyFingerprint] Fingerprint of the server public key. Mandatory if `action` is `confirm`.
* @returns {Promise<SetProtectedStateResult>}
*/
async unprotectDevice({ action, serverNonce, serverSignature }) {
async setProtectedState({ action, serverNonce, serverSignature, serverPublicKeyFingerprint }) {
let req;
switch (action) {
case 'prepare': {
if (!Buffer.isBuffer(serverNonce)) {
throw new Error('Invalid arguments');
}
req = {
action: proto.SetProtectedStateRequest.Action.DISABLE_REQUEST,
serverNonce
action: proto.SetProtectedStateRequest.Action.PREPARE,
prepare: { serverNonce }
};
break;
}
case 'confirm': {
if (!Buffer.isBuffer(serverSignature)) {
if (!Buffer.isBuffer(serverSignature) || !Buffer.isBuffer(serverPublicKeyFingerprint)) {
throw new Error('Invalid arguments');
}
req = {
action: proto.SetProtectedStateRequest.Action.DISABLE_CONFIRM,
serverSignature
action: proto.SetProtectedStateRequest.Action.CONFIRM,
confirm: { serverSignature, serverPublicKeyFingerprint }
};
break;
}
Expand All @@ -934,12 +936,13 @@ class Device extends DeviceBase {
}
const rep = await this.sendProtobufRequest('SetProtectedStateRequest', req);
if (action === 'prepare') {
if (!rep.clientNonce || !rep.clientSignature) {
if (!rep.prepare) {
return { protected: false };
}
return {
deviceNonce: rep.clientNonce,
deviceSignature: rep.clientSignature,
deviceNonce: rep.prepare.clientNonce,
deviceSignature: rep.prepare.clientSignature,
devicePublicKeyFingerprint: rep.prepare.devicePublicKeyFingerprint,
protected: true
};
}
Expand Down

0 comments on commit ad896e1

Please sign in to comment.