Skip to content

Commit

Permalink
Bug/ver 123 (#25)
Browse files Browse the repository at this point in the history
* updated supportedIdentifier & updated uploadFile, makeIpfsRequest

* bumped package version
  • Loading branch information
Ellenn-A authored Aug 23, 2023
1 parent a3fe18a commit ffbb2bb
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 12 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@digicatapult/veritable-cloudagent",
"version": "0.3.0",
"version": "0.3.1",
"main": "build/index",
"types": "build/index",
"files": [
Expand Down
6 changes: 4 additions & 2 deletions src/anoncreds/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import type { AgentContext } from '@aries-framework/core'

export default class VeritableAnonCredsRegistry implements AnonCredsRegistry {
public readonly methodName = 'veritable'
public readonly supportedIdentifier = /^ipfs:\/\/([a-zA-Z0-9]+)$/
public readonly supportedIdentifier =
/(?:^did:key:z[a-km-zA-HJ-NP-Z1-9]+$)|(?:^ipfs:\/\/([a-zA-Z0-9]+)$)|(?:^did:web:.+$)/ //did:web matches anything but an empty string
private readonly ipfsIdentifier = /^ipfs:\/\/([a-zA-Z0-9]+)$/

constructor(private ipfs: Ipfs) {}

Expand Down Expand Up @@ -167,7 +169,7 @@ export default class VeritableAnonCredsRegistry implements AnonCredsRegistry {
agentContext: AgentContext,
id: string
): Promise<{ type: 'success'; result: AObj } | { type: 'error'; error: AnonCredsResolutionMetadata }> {
const match = id.match(this.supportedIdentifier)
const match = id.match(this.ipfsIdentifier)
if (!match) {
return {
type: 'error',
Expand Down
6 changes: 3 additions & 3 deletions src/ipfs/__tests__/fixtures/ipfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export const withIpfsCatResponse = (responses: { cid: string; code: number; body

return { ipfsOrigin }
}

export const withIpfsAddResponse = (responses: { code: number; cid: unknown; body: Buffer }[]) => {
const ipfsOrigin = `http://ipfs`
let originalDispatcher: Dispatcher
Expand All @@ -45,8 +44,9 @@ export const withIpfsAddResponse = (responses: { code: number; cid: unknown; bod
method: 'POST',
body: (calledBody) => {
// ugly hack as undici mock doesn't allow mocking of Buffer bodies only strings
const asBuf = calledBody as unknown as Buffer
return body.compare(asBuf) === 0
const asBuf = calledBody as unknown as FormData
const asArr = [...asBuf.entries()]
return asArr.length === 1 && asArr[0][0] === 'file'
},
})
.reply(code, { Hash: cid })
Expand Down
14 changes: 11 additions & 3 deletions src/ipfs/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { addResponseParser } from './responseParser'

export interface MetadataFile {
blob: Blob
filename: string
}

export default class Ipfs {
constructor(private origin: string) {
try {
Expand All @@ -15,9 +20,12 @@ export default class Ipfs {
})
return Buffer.from(await response.arrayBuffer())
}

//needs to pass on form data
public async uploadFile(file: Buffer): Promise<string> {
const response = await this.makeIpfsRequest('/api/v0/add', { 'cid-version': '1' }, file)
const form = new FormData()
const blob = new Blob([file])
form.append('file', blob, 'file')
const response = await this.makeIpfsRequest('/api/v0/add', { 'cid-version': '1' }, form)
const responseJson = await response.json()

try {
Expand All @@ -28,7 +36,7 @@ export default class Ipfs {
}
}

private async makeIpfsRequest(route: string, args: Record<string, string>, body?: Buffer) {
private async makeIpfsRequest(route: string, args: Record<string, string>, body?: FormData) {
const url = new URL(route, this.origin)
const search = new URLSearchParams(args)
url.search = search.toString()
Expand Down
2 changes: 1 addition & 1 deletion src/routes/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1247,7 +1247,7 @@
},
"info": {
"title": "@digicatapult/veritable-cloudagent",
"version": "0.3.0",
"version": "0.3.1",
"description": "Rest endpoint wrapper for using your agent over HTTP",
"license": {
"name": "Apache-2.0"
Expand Down

0 comments on commit ffbb2bb

Please sign in to comment.