| 1 |
"use strict"; |
| 2 |
Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 |
exports.BaseBundleBuilder = void 0; |
| 4 |
// BaseBundleBuilder is a base class for BundleBuilder implementations. It |
| 5 |
// provides a the basic wokflow for signing and witnessing an artifact. |
| 6 |
// Subclasses must implement the `package` method to assemble a valid bundle |
| 7 |
// with the generated signature and verification material. |
| 8 |
class BaseBundleBuilder { |
| 9 |
signer; |
| 10 |
witnesses; |
| 11 |
constructor(options) { |
| 12 |
this.signer = options.signer; |
| 13 |
this.witnesses = options.witnesses; |
| 14 |
} |
| 15 |
// Executes the signing/witnessing process for the given artifact. |
| 16 |
async create(artifact) { |
| 17 |
const signature = await this.prepare(artifact).then((blob) => this.signer.sign(blob)); |
| 18 |
const bundle = await this.package(artifact, signature); |
| 19 |
// Invoke all of the witnesses in parallel |
| 20 |
const verificationMaterials = await Promise.all(this.witnesses.map((witness) => witness.testify(bundle.content, publicKey(signature.key)))); |
| 21 |
// Collect the verification material from all of the witnesses |
| 22 |
const tlogEntryList = []; |
| 23 |
const timestampList = []; |
| 24 |
verificationMaterials.forEach(({ tlogEntries, rfc3161Timestamps }) => { |
| 25 |
tlogEntryList.push(...(tlogEntries ?? [])); |
| 26 |
timestampList.push(...(rfc3161Timestamps ?? [])); |
| 27 |
}); |
| 28 |
// Merge the collected verification material into the bundle |
| 29 |
bundle.verificationMaterial.tlogEntries = tlogEntryList; |
| 30 |
bundle.verificationMaterial.timestampVerificationData = { |
| 31 |
rfc3161Timestamps: timestampList, |
| 32 |
}; |
| 33 |
return bundle; |
| 34 |
} |
| 35 |
// Override this function to apply any pre-signing transformations to the |
| 36 |
// artifact. The returned buffer will be signed by the signer. The default |
| 37 |
// implementation simply returns the artifact data. |
| 38 |
async prepare(artifact) { |
| 39 |
return artifact.data; |
| 40 |
} |
| 41 |
} |
| 42 |
exports.BaseBundleBuilder = BaseBundleBuilder; |
| 43 |
// Extracts the public key from a KeyMaterial. Returns either the public key |
| 44 |
// or the certificate, depending on the type of key material. |
| 45 |
function publicKey(key) { |
| 46 |
switch (key.$case) { |
| 47 |
case 'publicKey': |
| 48 |
return key.publicKey; |
| 49 |
case 'x509Certificate': |
| 50 |
return key.certificate; |
| 51 |
} |
| 52 |
} |