Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 0 additions & 97 deletions packages/core/src/decorators/attachment/Attachment.test.ts

This file was deleted.

80 changes: 55 additions & 25 deletions packages/core/src/decorators/attachment/Attachment.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Wallet } from '../../wallet/Wallet'

import { Expose, Type } from 'class-transformer'
import {
IsBase64,
Expand All @@ -15,6 +17,8 @@ import { AriesFrameworkError } from '../../error'
import { JsonEncoder } from '../../utils/JsonEncoder'
import { uuid } from '../../utils/uuid'

import { AttachmentJws, getVerkeysForJws, signAttachmentJws, verifyAttachmentJws } from './AttachmentJwsUtil'

export interface AttachmentOptions {
id?: string
description?: string
Expand All @@ -29,37 +33,14 @@ export interface AttachmentDataOptions {
base64?: string
json?: Record<string, unknown>
links?: string[]
jws?: Record<string, unknown>
jws?: AttachmentJws
sha256?: string
}

/**
* A JSON object that gives access to the actual content of the attachment
*/
export class AttachmentData {
public constructor(options: AttachmentDataOptions) {
if (options) {
this.base64 = options.base64
this.json = options.json
this.links = options.links
this.jws = options.jws
this.sha256 = options.sha256
}
}

/*
* Helper function returning JSON representation of attachment data (if present). Tries to obtain the data from .base64 or .json, throws an error otherwise
*/
public getDataAsJson<T>(): T {
if (typeof this.base64 === 'string') {
return JsonEncoder.fromBase64(this.base64) as T
} else if (this.json) {
return this.json as T
} else {
throw new AriesFrameworkError('No attachment data found in `json` or `base64` data fields.')
}
}

/**
* Base64-encoded data, when representing arbitrary content inline instead of via links. Optional.
*/
Expand All @@ -84,14 +65,63 @@ export class AttachmentData {
* A JSON Web Signature over the content of the attachment. Optional.
*/
@IsOptional()
public jws?: Record<string, unknown>
@Type(() => AttachmentJws)
@ValidateNested()
public jws?: AttachmentJws

/**
* The hash of the content. Optional.
*/
@IsOptional()
@IsHash('sha256')
public sha256?: string

public constructor(options: AttachmentDataOptions) {
if (options) {
this.base64 = options.base64
this.json = options.json
this.links = options.links
this.jws = options.jws
this.sha256 = options.sha256
}
}

public get jwsVerkeys(): string[] {
if (!this.jws) return []

return getVerkeysForJws(this.jws)
}

/*
* Helper function returning JSON representation of attachment data (if present). Tries to obtain the data from .base64 or .json, throws an error otherwise
*/
public getDataAsJson<T>(): T {
if (typeof this.base64 === 'string') {
return JsonEncoder.fromBase64(this.base64) as T
} else if (this.json) {
return this.json as T
} else {
throw new AriesFrameworkError('No attachment data found in `json` or `base64` data fields.')
}
}

public async sign(wallet: Wallet, verkeys: string[]) {
if (!this.base64) {
throw new AriesFrameworkError('Missing base64 data on attachment')
}

this.jws = await signAttachmentJws(wallet, verkeys, this.base64)
}

public async verify(wallet: Wallet) {
if (!this.jws || !this.base64) {
throw new AriesFrameworkError('Missing JWS and/or base64 parameters')
}

const isValid = await verifyAttachmentJws(wallet, this.jws, this.base64)

return isValid
}
}

/**
Expand Down
Loading