Skip to content

Commit

Permalink
merge and change jwtheaderparams
Browse files Browse the repository at this point in the history
  • Loading branch information
nitro-neal committed Sep 6, 2023
2 parents 82558a7 + 150b419 commit 18782c7
Show file tree
Hide file tree
Showing 31 changed files with 1,615 additions and 81 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ const { web5, did: myDid } = await Web5.connect();

An object which may specify any of the following properties:

- **`agent`** - _`Web5Agent instance`_ _(optional)_: an instance of a `Web5Agent` implementation. Defaults to creating a local `Web5UserAgent` if not provided.

- **`appData`** - _`AppDataStore instance`_ _(optional)_: an instance of an `AppDataStore` implementation. Defaults to a LevelDB-backed store with an insecure, static unlock passphrase if not provided. To allow the end user to enter a secure passphrase of their choosing, provide an initialized `AppDataVault`.

- **`connectedDid`** - _`string`_ _(optional)_: an existing DID to connect to.

- **`sync`** - _`string`_ _(optional)_: enable or disable synchronization of DWN records between local and remote DWNs. Sync defaults to running every 2 minutes and can be set to any value accepted by [`ms`](https://www.npmjs.com/package/ms). To disable sync set to `'off'`.

- **`techPreview`** - _`object`_ _(optional)_: an object that specifies configuration parameters that are relevant during the Tech Preview period of Web5 JS and may be deprecated in the future with advance notice.

- **`dwnEndpoints`** - _`array`_ _(optional)_: a list of DWeb Node endpoints to define in the DID created and returned by `Web5.connect()`. If this property is omitted, during the Tech Preview two nodes will be included by default (e.g., `['https://dwn.tbddev.org/dwn0', 'https://dwn.tbddev.org/dwn3']`).
Expand Down
101 changes: 73 additions & 28 deletions package-lock.json

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

8 changes: 4 additions & 4 deletions packages/agent/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@web5/agent",
"version": "0.1.7",
"version": "0.2.0",
"type": "module",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
Expand Down Expand Up @@ -68,9 +68,9 @@
},
"dependencies": {
"@tbd54566975/dwn-sdk-js": "0.2.1",
"@web5/common": "0.1.1",
"@web5/crypto": "0.1.6",
"@web5/dids": "0.1.9",
"@web5/common": "0.2.0",
"@web5/crypto": "0.2.0",
"@web5/dids": "0.2.0",
"level": "8.0.0",
"readable-stream": "4.4.2",
"readable-web-to-node-stream": "3.0.2"
Expand Down
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;
}
}


























/**
* 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

0 comments on commit 18782c7

Please sign in to comment.