Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SyncManager to Web5 Managed Agent #204

Merged
merged 9 commits into from
Sep 1, 2023
191 changes: 117 additions & 74 deletions package-lock.json

Large diffs are not rendered by default.

116 changes: 115 additions & 1 deletion packages/agent/src/dwn-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,4 +532,118 @@ export class DwnManager {

return jws;
}
}


























frankhinek marked this conversation as resolved.
Show resolved Hide resolved
/**
* ADDED TO GET SYNC WORKING
* - createMessage()
* - processMessage()
* - writePrunedRecord()
*/

public async createMessage(options: {
author: string,
messageOptions: unknown,
messageType: string
}): Promise<EventsGet | MessagesGet | RecordsRead | RecordsQuery | RecordsWrite | RecordsDelete | ProtocolsQuery | ProtocolsConfigure> {
const { author, messageOptions, messageType } = options;

const signingKeyId = await this.getAuthorSigningKeyId({ did: author });

// ! TODO: Remove this once DWN SDK supports external signers.
const dwnSignatureInput = this.getTempSignatureInput({ signingKeyId });

// TODO: Figure out how to narrow this type.
const messageCreateInput = {
...<any>messageOptions,
authorizationSignatureInput: dwnSignatureInput
};

const messageCreator = dwnMessageCreators[messageType];

// ! TODO: START Remove this monkey patch (MP) as soon as the DWN SDK supports external signers.
// MP Step 1: Store the original methods.
const originalCreateAuthorization = RecordsWrite.createAuthorization;
const originalSignAsAuthorization = Message.signAsAuthorization;
// MP Step 2: Replace the methods.
RecordsWrite.createAuthorization = (
recordId: string,
contextId: string | undefined,
descriptorCid: string,
attestation: GeneralJws | undefined,
encryption: EncryptionProperty | undefined,
) => this.createAuthorization(recordId, contextId, descriptorCid, attestation, encryption, signingKeyId);
Message.signAsAuthorization = (
descriptor: GenericMessage['descriptor'],
signatureInput: SignatureInput,
permissionsGrantId?: string,
) => this.signAsAuthorization(descriptor, signingKeyId, permissionsGrantId);
// MP Step 3: Call the method that required monkey patching.
const dwnMessage = await messageCreator.create(messageCreateInput as any);
// MP Step 4: Restore the original methods.
RecordsWrite.createAuthorization = originalCreateAuthorization;
Message.signAsAuthorization = originalSignAsAuthorization;
// ! TODO: END Remove this monkey patch (MP) as soon as the DWN SDK supports external signers.

return dwnMessage;
}

/**
* Writes a pruned initial `RecordsWrite` to a DWN without needing to supply associated data.
* Note: This method should ONLY be used by a {@link SyncManager} implementation.
*
* @param options.targetDid - DID of the DWN tenant to write the pruned RecordsWrite to.
* @returns DWN reply containing the status of processing request.
*/
public async writePrunedRecord(options: {
targetDid: string,
message: RecordsWriteMessage
}): Promise<GenericMessageReply> {
const { targetDid, message } = options;

return await this._dwn.synchronizePrunedInitialRecordsWrite(targetDid, message);
}

public async processMessage(options: {
targetDid: string,
message: GenericMessage,
dataStream?: Readable
}): Promise<UnionMessageReply> {
const { dataStream, message, targetDid } = options;

return await this._dwn.processMessage(targetDid, message, dataStream);
}
}

type GenericMessageReply = {
status: Status;
};

type Status = {
code: number
detail: string
};
1 change: 1 addition & 0 deletions packages/agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export * from './rpc-client.js';
export * from './store-managed-did.js';
export * from './store-managed-key.js';
export * from './store-managed-identity.js';
export * from './sync-manager.js';
export * from './utils.js';

export * from './test-managed-agent.js';
Loading