summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/tuf-js/dist/models/root.js
blob: 574ec1acdcc39d09f8f015e44beb5cf4287274a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Root = void 0;
const util_1 = __importDefault(require("util"));
const error_1 = require("../error");
const guard_1 = require("../utils/guard");
const types_1 = require("../utils/types");
const base_1 = require("./base");
const key_1 = require("./key");
const role_1 = require("./role");
/**
 * A container for the signed part of root metadata.
 *
 * The top-level role and metadata file signed by the root keys.
 * This role specifies trusted keys for all other top-level roles, which may further delegate trust.
 */
class Root extends base_1.Signed {
    constructor(options) {
        super(options);
        this.type = types_1.MetadataKind.Root;
        this.keys = options.keys || {};
        this.consistentSnapshot = options.consistentSnapshot ?? true;
        if (!options.roles) {
            this.roles = role_1.TOP_LEVEL_ROLE_NAMES.reduce((acc, role) => ({
                ...acc,
                [role]: new role_1.Role({ keyIDs: [], threshold: 1 }),
            }), {});
        }
        else {
            const roleNames = new Set(Object.keys(options.roles));
            if (!role_1.TOP_LEVEL_ROLE_NAMES.every((role) => roleNames.has(role))) {
                throw new error_1.ValueError('missing top-level role');
            }
            this.roles = options.roles;
        }
    }
    equals(other) {
        if (!(other instanceof Root)) {
            return false;
        }
        return (super.equals(other) &&
            this.consistentSnapshot === other.consistentSnapshot &&
            util_1.default.isDeepStrictEqual(this.keys, other.keys) &&
            util_1.default.isDeepStrictEqual(this.roles, other.roles));
    }
    toJSON() {
        return {
            spec_version: this.specVersion,
            version: this.version,
            expires: this.expires,
            keys: keysToJSON(this.keys),
            roles: rolesToJSON(this.roles),
            consistent_snapshot: this.consistentSnapshot,
            ...this.unrecognizedFields,
        };
    }
    static fromJSON(data) {
        const { unrecognizedFields, ...commonFields } = base_1.Signed.commonFieldsFromJSON(data);
        const { keys, roles, consistent_snapshot, ...rest } = unrecognizedFields;
        if (typeof consistent_snapshot !== 'boolean') {
            throw new TypeError('consistent_snapshot must be a boolean');
        }
        return new Root({
            ...commonFields,
            keys: keysFromJSON(keys),
            roles: rolesFromJSON(roles),
            consistentSnapshot: consistent_snapshot,
            unrecognizedFields: rest,
        });
    }
}
exports.Root = Root;
function keysToJSON(keys) {
    return Object.entries(keys).reduce((acc, [keyID, key]) => ({ ...acc, [keyID]: key.toJSON() }), {});
}
function rolesToJSON(roles) {
    return Object.entries(roles).reduce((acc, [roleName, role]) => ({ ...acc, [roleName]: role.toJSON() }), {});
}
function keysFromJSON(data) {
    let keys;
    if ((0, guard_1.isDefined)(data)) {
        if (!(0, guard_1.isObjectRecord)(data)) {
            throw new TypeError('keys must be an object');
        }
        keys = Object.entries(data).reduce((acc, [keyID, keyData]) => ({
            ...acc,
            [keyID]: key_1.Key.fromJSON(keyID, keyData),
        }), {});
    }
    return keys;
}
function rolesFromJSON(data) {
    let roles;
    if ((0, guard_1.isDefined)(data)) {
        if (!(0, guard_1.isObjectRecord)(data)) {
            throw new TypeError('roles must be an object');
        }
        roles = Object.entries(data).reduce((acc, [roleName, roleData]) => ({
            ...acc,
            [roleName]: role_1.Role.fromJSON(roleData),
        }), {});
    }
    return roles;
}