Skip to content

Commit

Permalink
chore: resolves a bug caused by checking instanceof the Principal cla…
Browse files Browse the repository at this point in the history
…ss (#502)

SDK-148
  • Loading branch information
krpeacock authored Sep 29, 2021
1 parent 96c5fee commit b06605b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
32 changes: 31 additions & 1 deletion packages/agent/src/request_id.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// tslint:disable-next-line: max-line-length
// https://github.com/dfinity-lab/dfinity/blob/5fef1450c9ab16ccf18381379149e504b11c8218/docs/spec/public/index.adoc#request-ids
import { Principal } from '@dfinity/principal';
import { hash, requestIdOf } from './request_id';
import { hash, hashValue, requestIdOf } from './request_id';
import { fromHex, toHex } from './utils/buffer';
import borc from 'borc';

const testHashOfBlob = async (input: ArrayBuffer, expected: string) => {
const hashed = await hash(input);
Expand Down Expand Up @@ -112,3 +113,32 @@ test.skip('requestIdOf for sender_delegation signature', async () => {
const delegation3ActualHashBytes = await requestIdOf(delegation3);
expect(toHex(delegation3ActualHashBytes)).toEqual(toHex(delegation1ActualHashBytes));
});

describe('hashValue', () => {
it('should hash a string', () => {
const value = hashValue('test');
expect(value instanceof ArrayBuffer).toBe(true);
});
it('should hash a borc tagged value', () => {
const tagged = hashValue(new borc.Tagged(42, 'hello'));
expect(tagged instanceof ArrayBuffer).toBe(true);
});
it('should hash a number', () => {
const value = hashValue(7);
expect(value instanceof ArrayBuffer).toBe(true);
});
it('should hash an array', () => {
const value = hashValue([7]);
expect(value instanceof ArrayBuffer).toBe(true);
});
it('should hash a bigint', () => {
const value = hashValue(BigInt(7));
expect(value instanceof ArrayBuffer).toBe(true);
});
it('should throw otherwise', () => {
const shouldThrow = () => {
hashValue({ foo: 'bar' });
};
expect(shouldThrow).toThrowError('Attempt to hash');
});
});
11 changes: 8 additions & 3 deletions packages/agent/src/request_id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ interface ToHashable {
toHash(): unknown;
}

function hashValue(value: unknown): ArrayBuffer {
/**
*
* @param value unknown value
* @returns ArrayBuffer
*/
export function hashValue(value: unknown): ArrayBuffer {
if (value instanceof borc.Tagged) {
return hashValue(value.value);
} else if (typeof value === 'string') {
Expand All @@ -30,8 +35,8 @@ function hashValue(value: unknown): ArrayBuffer {
} else if (Array.isArray(value)) {
const vals = value.map(hashValue);
return hash(concat(...vals));
} else if (value instanceof Principal) {
return hash(value.toUint8Array());
} else if (value && typeof value === 'object' && (value as any)._isPrincipal) {
return hash((value as Principal).toUint8Array());
} else if (
typeof value === 'object' &&
value !== null &&
Expand Down

0 comments on commit b06605b

Please sign in to comment.