Skip to content

Commit

Permalink
cleaning up lookup, changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
krpeacock committed Sep 28, 2023
1 parent ca980c0 commit ca8a33f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
8 changes: 7 additions & 1 deletion docs/generated/changelog.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ <h1>Agent-JS Changelog</h1>

<section>
<h2>Version x.x.x</h2>
<ul></ul>
<ul>
<li>
feat!: adds certificate logic to decode subnet and node key paths from the hashtree.
Changes the interface for `lookup_path` to allow returning a HashTree, but also constrains
`lookup` response to an ArrayBuffer using a new `lookupResultToBuffer` export
</li>
</ul>
<h2>Version 0.19.3</h2>
<ul>
<li>fix: Principal JSON is compatible with @dfinity/utils jsonReviver helper</li>
Expand Down
4 changes: 2 additions & 2 deletions packages/agent/src/certificate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ export class Certificate {
this.cert = cbor.decode(new Uint8Array(certificate));
}

public lookup(path: Array<ArrayBuffer | string>): ArrayBuffer | HashTree | undefined {
return lookup_path(path, this.cert.tree);
public lookup(path: Array<ArrayBuffer | string>): ArrayBuffer | undefined {
return lookupResultToBuffer(lookup_path(path, this.cert.tree));
}

public lookup_label(label: ArrayBuffer): ArrayBuffer | HashTree | undefined {
Expand Down
14 changes: 5 additions & 9 deletions packages/agent/src/polling/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Principal } from '@dfinity/principal';
import { Agent, RequestStatusResponseStatus } from '../agent';
import { Certificate, CreateCertificateOptions, lookupResultToBuffer } from '../certificate';
import { Certificate, CreateCertificateOptions } from '../certificate';
import { RequestId } from '../request_id';
import { toHex } from '../utils/buffer';

Expand Down Expand Up @@ -41,7 +41,7 @@ export async function pollForResponse(
canisterId: canisterId,
blsVerify,
});
const maybeBuf = lookupResultToBuffer(cert.lookup([...path, new TextEncoder().encode('status')]));
const maybeBuf = cert.lookup([...path, new TextEncoder().encode('status')]);
let status;
if (typeof maybeBuf === 'undefined') {
// Missing requestId means we need to wait
Expand All @@ -52,7 +52,7 @@ export async function pollForResponse(

switch (status) {
case RequestStatusResponseStatus.Replied: {
return lookupResultToBuffer(cert.lookup([...path, 'reply']))!;
return cert.lookup([...path, 'reply'])!;
}

case RequestStatusResponseStatus.Received:
Expand All @@ -63,12 +63,8 @@ export async function pollForResponse(
return pollForResponse(agent, canisterId, requestId, strategy, currentRequest);

case RequestStatusResponseStatus.Rejected: {
const rejectCode = new Uint8Array(
lookupResultToBuffer(cert.lookup([...path, 'reject_code']))!,
)[0];
const rejectMessage = new TextDecoder().decode(
lookupResultToBuffer(cert.lookup([...path, 'reject_message']))!,
);
const rejectCode = new Uint8Array(cert.lookup([...path, 'reject_code'])!)[0];
const rejectMessage = new TextDecoder().decode(cert.lookup([...path, 'reject_message'])!);
throw new Error(
`Call was rejected:\n` +
` Request ID: ${toHex(requestId)}\n` +
Expand Down
3 changes: 2 additions & 1 deletion packages/assets/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
getDefaultAgent,
HashTree,
lookup_path,
lookupResultToBuffer,
reconstruct,
uint8ToBuf,
} from '@dfinity/agent';
Expand Down Expand Up @@ -563,7 +564,7 @@ class Asset {
}

// Lookup hash of asset in tree
const treeSha = lookup_path(['http_assets', this._key], hashTree);
const treeSha = lookupResultToBuffer(lookup_path(['http_assets', this._key], hashTree));

return !!treeSha && !!this.sha256 && compare(this.sha256.buffer, treeSha) === 0;
}
Expand Down

0 comments on commit ca8a33f

Please sign in to comment.