@ngraveio/ur-sign
Version:
Provides BC-UR types for signature request and response from cold wallets to hot wallets.
84 lines (82 loc) • 3.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SignResponse = void 0;
const bc_ur_1 = require("@ngraveio/bc-ur");
const ur_uuid_1 = require("@ngraveio/ur-uuid");
const buffer_1 = require("buffer/");
class SignResponse extends (0, bc_ur_1.registryItemFactory)({
tag: 41412,
URType: 'sign-response',
allowKeysNotInMap: true,
keyMap: {
requestId: 1,
signature: 2,
origin: 3,
},
CDDL: `
sign-response = {
?request-id: uuid, ; Identifier of the signing request
signature: bytes, ; Signature result
?origin: text, ; The device info providing this signature
}
; request-id must be present in case of response to a sign-request where
; the request-id is specified
request-id = 1
signature = 2
origin = 3
`,
}) {
constructor(data) {
super(data);
this.getRequestId = () => this.data.requestId;
this.getSignature = () => this.data.signature;
this.getOrigin = () => this.data.origin;
//@ts-ignore
this.data = data;
// Convert requestId to UUID if it is not already an instance of UUID
if (data.requestId !== undefined) {
if (typeof data.requestId === 'string' || data.requestId instanceof Uint8Array) {
this.data.requestId = new ur_uuid_1.UUID(data.requestId);
}
else if (!(data.requestId instanceof ur_uuid_1.UUID)) {
throw new Error('Invalid requestId. Expected a UUID, string, or Uint8Array.');
}
}
// Convert signature to Buffer
if (!(data.signature instanceof buffer_1.Buffer)) {
this.data.signature = buffer_1.Buffer.from(data.signature);
}
}
verifyInput(input) {
const reasons = [];
if (!input) {
reasons.push(new Error('Input is undefined'));
return { valid: false, reasons };
}
// If request id is present it must be a valid UUID
if (input.requestId !== undefined) {
if (!(input.requestId instanceof ur_uuid_1.UUID)) {
try {
new ur_uuid_1.UUID(input.requestId);
}
catch (error) {
reasons.push(new Error('Invalid requestId: ' + error.message));
}
}
}
// Signature must be present and must be a buffer
if (input.signature === undefined || !(input.signature instanceof Uint8Array)) {
reasons.push(new Error('Signature must be a buffer'));
}
// Origin must be a string if present
if (input.origin !== undefined && typeof input.origin !== 'string') {
reasons.push(new Error('Origin must be a string'));
}
return {
valid: reasons.length === 0,
reasons: reasons.length > 0 ? reasons : undefined,
};
}
}
exports.SignResponse = SignResponse;
//# sourceMappingURL=SignResponse.js.map