generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* implement `MessagesGet`
- Loading branch information
Showing
16 changed files
with
705 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"$id": "https://identity.foundation/dwn/json-schemas/messages-get.json", | ||
"type": "object", | ||
"additionalProperties": false, | ||
"required": [ | ||
"authorization", | ||
"descriptor" | ||
], | ||
"properties": { | ||
"authorization": { | ||
"$ref": "https://identity.foundation/dwn/json-schemas/general-jws.json" | ||
}, | ||
"descriptor": { | ||
"type": "object", | ||
"additionalProperties": false, | ||
"required": [ | ||
"interface", | ||
"method" | ||
], | ||
"properties": { | ||
"interface": { | ||
"enum": [ | ||
"Messages" | ||
], | ||
"type": "string" | ||
}, | ||
"method": { | ||
"enum": [ | ||
"Get" | ||
], | ||
"type": "string" | ||
}, | ||
"messageCids": { | ||
"type": "array", | ||
"items": { | ||
"type": "string" | ||
}, | ||
"minItems": 1 | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import type { DataStore } from '../../../store/data-store.js'; | ||
import type { DidResolver } from '../../../did/did-resolver.js'; | ||
import type { MessageStore } from '../../../store/message-store.js'; | ||
import type { MethodHandler } from '../../types.js'; | ||
import type { MessagesGetMessage, MessagesGetReply, MessagesGetReplyEntry } from '../types.js'; | ||
|
||
import { DataStream } from '../../../utils/data-stream.js'; | ||
import { DwnConstant } from '../../../core/dwn-constant.js'; | ||
import { Encoder } from '../../../utils/encoder.js'; | ||
import { MessageReply } from '../../../core/message-reply.js'; | ||
import { MessagesGet } from '../messages/messages-get.js'; | ||
import { authenticate, authorize } from '../../../core/auth.js'; | ||
import { DwnInterfaceName, DwnMethodName, Message } from '../../../core/message.js'; | ||
|
||
type HandleArgs = { tenant: string, message: MessagesGetMessage }; | ||
|
||
export class MessagesGetHandler implements MethodHandler { | ||
constructor(private didResolver: DidResolver, private messageStore: MessageStore, private dataStore: DataStore) {} | ||
|
||
public async handle({ tenant, message }: HandleArgs): Promise<MessagesGetReply> { | ||
let messagesGet: MessagesGet; | ||
|
||
try { | ||
messagesGet = await MessagesGet.parse(message); | ||
} catch (e) { | ||
return MessageReply.fromError(e, 400); | ||
} | ||
|
||
try { | ||
await authenticate(message.authorization, this.didResolver); | ||
await authorize(tenant, messagesGet); | ||
} catch (e) { | ||
return MessageReply.fromError(e, 401); | ||
} | ||
|
||
const promises: Promise<MessagesGetReplyEntry>[] = []; | ||
const messageCids = new Set(message.descriptor.messageCids); | ||
|
||
for (const messageCid of messageCids) { | ||
const promise = this.messageStore.get(tenant, messageCid) | ||
.then(message => { | ||
return { messageCid, message }; | ||
}) | ||
.catch(_ => { | ||
return { messageCid, message: undefined, error: `Failed to get message ${messageCid}` }; | ||
}); | ||
|
||
promises.push(promise); | ||
} | ||
|
||
const messages = await Promise.all(promises); | ||
|
||
// for every message, include associated data as `encodedData` IF: | ||
// * its a RecordsWrite | ||
// * the data size is equal or smaller than the size threshold | ||
//! NOTE: this is somewhat duplicate code that also exists in `StorageController.query`. | ||
for (const entry of messages) { | ||
const { message } = entry; | ||
|
||
if (!message) { | ||
continue; | ||
} | ||
|
||
const { interface: messageInterface, method } = message.descriptor; | ||
if (messageInterface !== DwnInterfaceName.Records || method !== DwnMethodName.Write) { | ||
continue; | ||
} | ||
|
||
const dataCid = message.descriptor.dataCid; | ||
const dataSize = message.descriptor.dataSize; | ||
|
||
if (dataCid !== undefined && dataSize! <= DwnConstant.maxDataSizeAllowedToBeEncoded) { | ||
const messageCid = await Message.getCid(message); | ||
const result = await this.dataStore.get(tenant, messageCid, dataCid); | ||
|
||
if (result) { | ||
const dataBytes = await DataStream.toBytes(result.dataStream); | ||
entry.encodedData = Encoder.bytesToBase64Url(dataBytes); | ||
} | ||
} | ||
} | ||
|
||
return { | ||
status: { code: 200, detail: 'OK' }, | ||
messages | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import type { SignatureInput } from '../../../jose/jws/general/types.js'; | ||
import type { MessagesGetDescriptor, MessagesGetMessage } from '../types.js'; | ||
|
||
import { parseCid } from '../../../utils/cid.js'; | ||
import { validateAuthorizationIntegrity } from '../../../core/auth.js'; | ||
import { DwnInterfaceName, DwnMethodName, Message } from '../../../core/message.js'; | ||
|
||
export type MessagesGetOptions = { | ||
messageCids: string[]; | ||
authorizationSignatureInput: SignatureInput; | ||
}; | ||
|
||
export class MessagesGet extends Message<MessagesGetMessage> { | ||
public static async parse(message: MessagesGetMessage): Promise<MessagesGet> { | ||
Message.validateJsonSchema(message); | ||
this.validateMessageCids(message.descriptor.messageCids); | ||
|
||
await validateAuthorizationIntegrity(message); | ||
|
||
return new MessagesGet(message); | ||
} | ||
|
||
public static async create(options: MessagesGetOptions): Promise<MessagesGet> { | ||
const descriptor: MessagesGetDescriptor = { | ||
interface : DwnInterfaceName.Messages, | ||
method : DwnMethodName.Get, | ||
messageCids : options.messageCids | ||
}; | ||
|
||
const authorization = await Message.signAsAuthorization(descriptor, options.authorizationSignatureInput); | ||
const message = { descriptor, authorization }; | ||
|
||
Message.validateJsonSchema(message); | ||
MessagesGet.validateMessageCids(options.messageCids); | ||
|
||
return new MessagesGet(message); | ||
} | ||
|
||
/** | ||
* validates the provided cids | ||
* @param messageCids - the cids in question | ||
* @throws {Error} if an invalid cid is found. | ||
*/ | ||
private static validateMessageCids(messageCids: string[]): void { | ||
for (const cid of messageCids) { | ||
try { | ||
parseCid(cid); | ||
} catch (_) { | ||
throw new Error(`${cid} is not a valid CID`); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.