Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
key.js91 linesDownload Raw Back to dist
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3    return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.Key = void 0;
7const util_1 = __importDefault(require("util"));
8const error_1 = require("./error");
9const utils_1 = require("./utils");
10const key_1 = require("./utils/key");
11// A container class representing the public portion of a Key.
12class Key {
13    keyID;
14    keyType;
15    scheme;
16    keyVal;
17    unrecognizedFields;
18    constructor(options) {
19        const { keyID, keyType, scheme, keyVal, unrecognizedFields } = options;
20        this.keyID = keyID;
21        this.keyType = keyType;
22        this.scheme = scheme;
23        this.keyVal = keyVal;
24        this.unrecognizedFields = unrecognizedFields || {};
25    }
26    // Verifies the that the metadata.signatures contains a signature made with
27    // this key and is correctly signed.
28    verifySignature(metadata) {
29        const signature = metadata.signatures[this.keyID];
30        if (!signature)
31            throw new error_1.UnsignedMetadataError('no signature for key found in metadata');
32        if (!this.keyVal.public)
33            throw new error_1.UnsignedMetadataError('no public key found');
34        const publicKey = (0, key_1.getPublicKey)({
35            keyType: this.keyType,
36            scheme: this.scheme,
37            keyVal: this.keyVal.public,
38        });
39        const signedData = metadata.signed.toJSON();
40        try {
41            if (!utils_1.crypto.verifySignature(signedData, publicKey, signature.sig)) {
42                throw new error_1.UnsignedMetadataError(`failed to verify ${this.keyID} signature`);
43            }
44        }
45        catch (error) {
46            if (error instanceof error_1.UnsignedMetadataError) {
47                throw error;
48            }
49            throw new error_1.UnsignedMetadataError(`failed to verify ${this.keyID} signature`);
50        }
51    }
52    equals(other) {
53        if (!(other instanceof Key)) {
54            return false;
55        }
56        return (this.keyID === other.keyID &&
57            this.keyType === other.keyType &&
58            this.scheme === other.scheme &&
59            util_1.default.isDeepStrictEqual(this.keyVal, other.keyVal) &&
60            util_1.default.isDeepStrictEqual(this.unrecognizedFields, other.unrecognizedFields));
61    }
62    toJSON() {
63        return {
64            keytype: this.keyType,
65            scheme: this.scheme,
66            keyval: this.keyVal,
67            ...this.unrecognizedFields,
68        };
69    }
70    static fromJSON(keyID, data) {
71        const { keytype, scheme, keyval, ...rest } = data;
72        if (typeof keytype !== 'string') {
73            throw new TypeError('keytype must be a string');
74        }
75        if (typeof scheme !== 'string') {
76            throw new TypeError('scheme must be a string');
77        }
78        if (!utils_1.guard.isStringRecord(keyval)) {
79            throw new TypeError('keyval must be a string record');
80        }
81        return new Key({
82            keyID,
83            keyType: keytype,
84            scheme,
85            keyVal: keyval,
86            unrecognizedFields: rest,
87        });
88    }
89}
90exports.Key = Key;
91 
codekingpro/portable-devtools · Team Ai