| 20 | | { status: 'error'; error: string }; |
| 21 | |
| 22 | export class SignedWebBundle { |
| 23 | private constructor( |
| 24 | private integrityBlock: IntegrityBlock, |
| 25 | private pureWebBundle: Uint8Array |
| 26 | ) {} |
| 27 | |
| 28 | // Use with raw Singed Web Bundle data, for example read from file with fs.readFile('bundle.swbn') |
| 29 | static fromBytes(signedWebBundle: Uint8Array): SignedWebBundle { |
| 30 | if (!isSignedWebBundle(signedWebBundle)) { |
| 31 | throw new Error('Not a signed web bundle.'); |
| 32 | } |
| 33 | |
| 34 | const offset = this.obtainIntegrityOffset(signedWebBundle); |
| 35 | |
| 36 | const integrityBlockBytes = signedWebBundle.slice(0, offset); |
| 37 | const integrityBlock = IntegrityBlock.fromCbor(integrityBlockBytes); |
| 38 | |
| 39 | const bundle = signedWebBundle.slice(offset); |
| 40 | |
| 41 | return new SignedWebBundle(integrityBlock, bundle); |
| 42 | } |
| 43 | |
| 44 | // Web bundle ID is derived from the first key if not provided |
| 45 | static async fromWebBundle( |
| 46 | webBundle: Uint8Array, |
| 47 | signingStrategies: Array<ISigningStrategy>, |
| 48 | options?: { webBundleId?: string } |
| 49 | ): Promise<SignedWebBundle> { |
| 50 | assert(signingStrategies.length > 0, 'No signing strategies provided'); |
| 51 | |
| 52 | const publicKey = await signingStrategies[0].getPublicKey(); |
| 53 | const webBundleId = |
| 54 | options?.webBundleId ?? new WebBundleId(publicKey).serialize(); |
| 55 | |
| 56 | const { signedWebBundle } = await new IntegrityBlockSigner( |
| 57 | webBundle, |
| 58 | webBundleId, |
| 59 | signingStrategies |
| 60 | ).sign(); |
| 61 | |
| 62 | return SignedWebBundle.fromBytes(signedWebBundle); |
| 63 | } |
| 64 | |
| 65 | async addSignature(signingStrategy: ISigningStrategy): Promise<this> { |
| 66 | this.integrityBlock = await new IntegrityBlockSigner( |
| 67 | this.pureWebBundle, |
| 68 | this.integrityBlock, |
| 69 | [signingStrategy] |
| 70 | ).signAndGetIntegrityBlock(); |
| 71 | return this; |
| 72 | } |
| 73 | |
| 74 | removeSignature(publicKey: Uint8Array): this { |
| 75 | this.integrityBlock.removeIntegritySignature(publicKey); |
| 76 | return this; |
| 77 | } |
| 78 | |
| 79 | printInfo(): void { |
nothing calls this directly
no outgoing calls
no test coverage detected