@ngraveio/ur-sync
Version:
Provides BC-UR types for syncing multiple coins and accounts from cold wallets to watch only wallets.
86 lines (84 loc) • 3.07 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PortfolioMetadata = void 0;
const bc_ur_1 = require("@ngraveio/bc-ur");
const LanguageCodes_1 = require("./LanguageCodes");
const buffer_1 = require("buffer/");
class PortfolioMetadata extends (0, bc_ur_1.registryItemFactory)({
tag: 41404,
URType: 'portfolio-metadata',
keyMap: {
syncId: 1,
language: 2,
firmwareVersion: 3,
device: 4,
},
allowKeysNotInMap: true,
CDDL: `
metadata = {
? sync_id: bytes .size 16 ; Generated by the hardware wallet to identify
the device
? language: language_code, ; Indicates the selected language
on the hardware wallet
? fw_version: string, ; Firmware version of the hardware wallet
? device: string ; Indicates the device name
}
sync_id = 1
language = 2
fw_version = 3
device = 4
language_code = string ; following [ISO 639-1] Code (e.g. "en" for English,
;"fr" for French, "nl" for Dutch and "es" for Spanish
`,
}) {
constructor(metadata = {}) {
super(metadata);
this.getSyncId = () => {
// should pad the left of the buffer with 0 if smaller than 16
if (this.data.syncId) {
return buffer_1.Buffer.concat([buffer_1.Buffer.alloc(16 - this.data.syncId.length), this.data.syncId]);
}
return undefined;
};
this.getlanguage = () => this.data.language;
this.getFirmwareVersion = () => this.data.firmwareVersion;
this.getDevice = () => this.data.device;
this.data = metadata;
if (metadata.syncId !== undefined) {
// Remove padding from sync id
let padRemovedSyncId = buffer_1.Buffer.from(metadata.syncId);
// Remove starting zeros from sync id buffer
while (padRemovedSyncId && padRemovedSyncId[0] === 0) {
padRemovedSyncId = padRemovedSyncId.slice(1);
}
this.data.syncId = padRemovedSyncId;
}
}
verifyInput(input) {
const errors = [];
if (input.language && !LanguageCodes_1.languages[input.language]) {
errors.push(new Error('Invalid language code'));
}
if (input.syncId && input.syncId.length > 16) {
errors.push(new Error('Sync id buffer size must be maximum 16'));
}
// TODO: make firmware version valid SemVer
return {
valid: errors.length === 0,
reasons: errors.length > 0 ? errors : undefined,
};
}
preCBOR() {
// Convert sync id buffer to unint8array
if (this.data.syncId !== undefined) {
const updatedData = {
...this.data,
syncId: new Uint8Array(this.data.syncId),
};
return super.preCBOR(updatedData);
}
return super.preCBOR();
}
}
exports.PortfolioMetadata = PortfolioMetadata;
//# sourceMappingURL=PortfolioMetadata.js.map