Skip to content

Commit 2a869ba

Browse files
authoredJul 17, 2023
envelope serialization funcs in bundle pkg (#617)
Signed-off-by: Brian DeHamer <bdehamer@github.com>
1 parent 335cb74 commit 2a869ba

File tree

5 files changed

+74
-11
lines changed

5 files changed

+74
-11
lines changed
 

‎.changeset/swift-socks-knock.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sigstore/bundle': minor
3+
---
4+
5+
export `envelopeToJSON`/`envelopeFromJSON` functions for serialization/deserialization of DSSE envelopes

‎packages/bundle/src/__tests__/index.test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ import {
4545
assertBundleV01,
4646
bundleFromJSON,
4747
bundleToJSON,
48+
envelopeFromJSON,
49+
envelopeToJSON,
4850
isBundleV01,
4951
isBundleWithCertificateChain,
5052
isBundleWithDsseEnvelope,
@@ -144,6 +146,8 @@ describe('public interface', () => {
144146
it('exports serialization functions', () => {
145147
expect(bundleFromJSON).toBeDefined();
146148
expect(bundleToJSON).toBeDefined();
149+
expect(envelopeFromJSON).toBeDefined();
150+
expect(envelopeToJSON).toBeDefined();
147151
});
148152

149153
it('exports constants', () => {

‎packages/bundle/src/__tests__/serialized.test.ts

+47-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ import {
2323
X509CertificateChain,
2424
hashAlgorithmToJSON,
2525
} from '@sigstore/protobuf-specs';
26-
import { bundleFromJSON, bundleToJSON } from '../serialized';
26+
import {
27+
bundleFromJSON,
28+
bundleToJSON,
29+
envelopeFromJSON,
30+
envelopeToJSON,
31+
} from '../serialized';
2732

2833
import type { Bundle } from '../bundle';
2934

@@ -361,3 +366,44 @@ describe('bundleFromJSON', () => {
361366
expect(deserializedBundle).toEqual(bundle);
362367
});
363368
});
369+
370+
describe('envelopeToJSON', () => {
371+
const dsseEnvelope: Envelope = {
372+
payload: Buffer.from('payload'),
373+
payloadType: 'application/vnd.in-toto+json',
374+
signatures: [
375+
{
376+
keyid: 'keyid',
377+
sig: Buffer.from('signature'),
378+
},
379+
],
380+
};
381+
382+
it('matches the serialized form of the Envelope', () => {
383+
const json = envelopeToJSON(dsseEnvelope);
384+
385+
expect(json).toBeTruthy();
386+
expect(json.payload).toEqual(dsseEnvelope.payload.toString('base64'));
387+
expect(json.payloadType).toEqual(dsseEnvelope.payloadType);
388+
expect(json.signatures).toHaveLength(dsseEnvelope.signatures.length);
389+
const signature = json.signatures[0];
390+
const expectedSignature = dsseEnvelope.signatures[0];
391+
expect(signature).toBeTruthy();
392+
expect(signature?.keyid).toEqual(expectedSignature.keyid);
393+
expect(signature?.sig).toEqual(expectedSignature.sig.toString('base64'));
394+
});
395+
});
396+
397+
describe('envelopeFromJSON', () => {
398+
const envelope = {
399+
payload: Buffer.from('ABC'),
400+
payloadType: 'application/json',
401+
signatures: [{ sig: Buffer.from('BAR'), keyid: '' }],
402+
};
403+
404+
it('matches the deserialized form of the Envelope', () => {
405+
const json = envelopeToJSON(envelope);
406+
const deserializedEnvelope = envelopeFromJSON(json);
407+
expect(deserializedEnvelope).toEqual(envelope);
408+
});
409+
});

‎packages/bundle/src/index.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ export {
2222
isBundleWithPublicKey,
2323
} from './bundle';
2424
export { ValidationError } from './error';
25-
export { bundleFromJSON, bundleToJSON } from './serialized';
25+
export {
26+
bundleFromJSON,
27+
bundleToJSON,
28+
envelopeFromJSON,
29+
envelopeToJSON,
30+
} from './serialized';
2631
export {
2732
assertBundle,
2833
assertBundleLatest,

‎packages/bundle/src/serialized.ts

+12-9
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,30 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
16-
import { Bundle as ProtoBundle } from '@sigstore/protobuf-specs';
16+
import { Envelope, Bundle as ProtoBundle } from '@sigstore/protobuf-specs';
1717
import { assertBundle } from './validate';
1818

1919
import type { Bundle } from './bundle';
2020
import type { OneOf } from './utility';
2121

22-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
23-
export const bundleFromJSON = (obj: any): Bundle => {
22+
export const bundleFromJSON = (obj: unknown): Bundle => {
2423
const bundle = ProtoBundle.fromJSON(obj);
2524
assertBundle(bundle);
2625
return bundle;
2726
};
2827

29-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
3028
export const bundleToJSON = (bundle: Bundle): SerializedBundle => {
3129
return ProtoBundle.toJSON(bundle) as SerializedBundle;
3230
};
3331

32+
export const envelopeFromJSON = (obj: unknown): Envelope => {
33+
return Envelope.fromJSON(obj);
34+
};
35+
36+
export const envelopeToJSON = (envelope: Envelope): SerializedEnvelope => {
37+
return Envelope.toJSON(envelope) as SerializedEnvelope;
38+
};
39+
3440
type SerializedTLogEntry = {
3541
logIndex: string;
3642
logId: {
@@ -76,7 +82,7 @@ type SerializedMessageSignature = {
7682
};
7783

7884
// Serialized form of the dsseEnvelope option in the Sigstore Bundle
79-
type SerializedDSSEEnvelope = {
85+
export type SerializedEnvelope = {
8086
payload: string;
8187
payloadType: string;
8288
signatures: {
@@ -85,9 +91,6 @@ type SerializedDSSEEnvelope = {
8591
}[];
8692
};
8793

88-
// Serialized form of the DSSE Envelope
89-
export type { SerializedDSSEEnvelope as SerializedEnvelope };
90-
9194
// Serialized form of the Sigstore Bundle union type with all possible options
9295
// represented
9396
export type SerializedBundle = {
@@ -103,6 +106,6 @@ export type SerializedBundle = {
103106
timestampVerificationData: SerializedTimestampVerificationData | undefined;
104107
};
105108
} & OneOf<{
106-
dsseEnvelope: SerializedDSSEEnvelope;
109+
dsseEnvelope: SerializedEnvelope;
107110
messageSignature: SerializedMessageSignature;
108111
}>;

0 commit comments

Comments
 (0)
Please sign in to comment.