Skip to content

Commit

Permalink
feat(oob): support fetching shortened invitation urls (#840)
Browse files Browse the repository at this point in the history
Signed-off-by: KolbyRKunz <[email protected]>
  • Loading branch information
KolbyRKunz authored Jul 14, 2022
1 parent a604ec4 commit 60ee0e5
Show file tree
Hide file tree
Showing 7 changed files with 438 additions and 234 deletions.
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"web-did-resolver": "^2.0.8"
},
"devDependencies": {
"node-fetch": "^2.0",
"@types/bn.js": "^5.1.0",
"@types/events": "^3.0.0",
"@types/luxon": "^1.27.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,22 +121,18 @@ export class ConnectionInvitationMessage extends AgentMessage {
*
* @param invitationUrl invitation url containing c_i or d_m parameter
*
* @throws Error when url can not be decoded to JSON, or decoded message is not a valid `ConnectionInvitationMessage`
* @throws Error when the url does not contain c_i or d_m as parameter
* @throws Error when the url can not be decoded to JSON, or decoded message is not a valid 'ConnectionInvitationMessage'
*/
public static fromUrl(invitationUrl: string) {
const parsedUrl = parseUrl(invitationUrl).query
const encodedInvitation = parsedUrl['c_i'] ?? parsedUrl['d_m']

if (typeof encodedInvitation === 'string') {
const invitationJson = JsonEncoder.fromBase64(encodedInvitation)
const invitation = JsonTransformer.fromJSON(invitationJson, ConnectionInvitationMessage)

return invitation
} else {
throw new AriesFrameworkError(
'InvitationUrl is invalid. It needs to contain one, and only one, of the following parameters; `c_i` or `d_m`'
)
throw new AriesFrameworkError('InvitationUrl is invalid. Needs to be encoded with either c_i, d_m, or oob')
}
}
}
Expand Down
18 changes: 15 additions & 3 deletions packages/core/src/modules/oob/OutOfBandModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { injectable, module } from '../../plugins'
import { DidCommMessageRepository, DidCommMessageRole } from '../../storage'
import { JsonEncoder, JsonTransformer } from '../../utils'
import { parseMessageType, supportsIncomingMessageType } from '../../utils/messageType'
import { parseInvitationUrl } from '../../utils/parseInvitation'
import { parseInvitationUrl, parseInvitationShortUrl } from '../../utils/parseInvitation'
import { DidKey } from '../dids'
import { didKeyToVerkey } from '../dids/helpers'
import { outOfBandServiceToNumAlgo2Did } from '../dids/methods/peer/peerDidNumAlgo2'
Expand Down Expand Up @@ -271,8 +271,8 @@ export class OutOfBandModule {
* @param config configuration of how out-of-band invitation should be processed
* @returns out-of-band record and connection record if one has been created
*/
public receiveInvitationFromUrl(invitationUrl: string, config: ReceiveOutOfBandInvitationConfig = {}) {
const message = this.parseInvitation(invitationUrl)
public async receiveInvitationFromUrl(invitationUrl: string, config: ReceiveOutOfBandInvitationConfig = {}) {
const message = await this.parseInvitationShortUrl(invitationUrl)
return this.receiveInvitation(message, config)
}

Expand All @@ -287,6 +287,18 @@ export class OutOfBandModule {
return parseInvitationUrl(invitationUrl)
}

/**
* Parses URL containing encoded invitation and returns invitation message. Compatible with
* parsing shortened URLs
*
* @param invitationUrl URL containing encoded invitation
*
* @returns OutOfBandInvitation
*/
public async parseInvitationShortUrl(invitation: string): Promise<OutOfBandInvitation> {
return await parseInvitationShortUrl(invitation, this.agentConfig.agentDependencies)
}

/**
* Creates inbound out-of-band record and assigns out-of-band invitation message to it if the
* message is valid. It automatically passes out-of-band invitation for further processing to
Expand Down
Loading

0 comments on commit 60ee0e5

Please sign in to comment.