Skip to content

Commit

Permalink
simplifying @dfinity/identity with agent exports
Browse files Browse the repository at this point in the history
  • Loading branch information
krpeacock committed Nov 2, 2023
1 parent 65204ef commit 5cf8707
Show file tree
Hide file tree
Showing 17 changed files with 44 additions and 391 deletions.
2 changes: 1 addition & 1 deletion packages/agent/src/actor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ describe('makeActor', () => {
try {
await actor.greet('test');
} catch (error) {
expect(error.message).toBe(
expect((error as Error).message).toBe(
"This identity has expired due this application's security policy. Please refresh your authentication.",
);
}
Expand Down
12 changes: 6 additions & 6 deletions packages/agent/src/agent/http/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -733,8 +733,8 @@ describe('default host', () => {
it('should use the existing host if the agent is used on a known hostname', () => {
const knownHosts = ['ic0.app', 'icp0.io', '127.0.0.1', '127.0.0.1'];
for (const host of knownHosts) {
delete window.location;
window.location = {
delete (window as any).location;
(window as any).location = {
hostname: host,
protocol: 'https:',
} as any;
Expand All @@ -745,8 +745,8 @@ describe('default host', () => {
it('should correctly handle subdomains on known hosts', () => {
const knownHosts = ['ic0.app', 'icp0.io', '127.0.0.1', '127.0.0.1'];
for (const host of knownHosts) {
delete window.location;
window.location = {
delete (window as any).location;
(window as any).location = {
host: `foo.${host}`,
hostname: `rrkah-fqaaa-aaaaa-aaaaq-cai.${host}`,
protocol: 'https:',
Expand All @@ -758,9 +758,9 @@ describe('default host', () => {
it('should handle port numbers for 127.0.0.1', () => {
const knownHosts = ['127.0.0.1', '127.0.0.1'];
for (const host of knownHosts) {
delete window.location;
delete (window as any).location;
// hostname is different from host when port is specified
window.location = {
(window as any).location = {
host: `${host}:4943`,
hostname: `${host}`,
protocol: 'http:',
Expand Down
2 changes: 1 addition & 1 deletion packages/agent/src/agent/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { AgentHTTPResponseError } from './errors';
import { request } from '../../canisterStatus';
import { CertificateVerificationError, SubnetStatus } from '../../certificate';
import { ed25519 } from '@noble/curves/ed25519';
import { Ed25519PublicKey } from '../../publicKey';
import { Ed25519PublicKey } from '../../public_key';

export * from './transforms';
export { Nonce, makeNonce } from './types';
Expand Down
4 changes: 2 additions & 2 deletions packages/agent/src/certificate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ test('delegation works for canisters within the subnet range', async () => {
const rangeStart = Principal.fromHex('00000000002000000101');
const rangeInterior = Principal.fromHex('000000000020000C0101');
const rangeEnd = Principal.fromHex('00000000002FFFFF0101');
async function verifies(canisterId) {
async function verifies(canisterId: Principal) {
jest.setSystemTime(new Date(Date.parse('2022-02-23T07:38:00.652Z')));
await expect(
Cert.Certificate.create({
Expand All @@ -197,7 +197,7 @@ test('delegation check fails for canisters outside of the subnet range', async (
// 0x00000000002FFFFF0101
const beforeRange = Principal.fromHex('00000000000000020101');
const afterRange = Principal.fromHex('00000000003000020101');
async function certificateFails(canisterId) {
async function certificateFails(canisterId: Principal) {
await expect(
Cert.Certificate.create({
certificate: fromHex(SAMPLE_CERT),
Expand Down
3 changes: 2 additions & 1 deletion packages/agent/src/der.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { bufEquals, encodeLenBytes, encodeLen, decodeLenBytes, decodeLen } from './der';
import { encodeLenBytes, encodeLen, decodeLenBytes, decodeLen } from './der';
import { bufEquals } from './utils/buffer';

describe('bufEquals tests', () => {
test('equal buffers', () => {
Expand Down
10 changes: 1 addition & 9 deletions packages/agent/src/der.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
export const bufEquals = (b1: ArrayBuffer, b2: ArrayBuffer): boolean => {
if (b1.byteLength !== b2.byteLength) return false;
const u1 = new Uint8Array(b1);
const u2 = new Uint8Array(b2);
for (let i = 0; i < u1.length; i++) {
if (u1[i] !== u2[i]) return false;
}
return true;
};
import { bufEquals } from './utils/buffer';

export const encodeLenBytes = (len: number): number => {
if (len <= 0x7f) {
Expand Down
6 changes: 4 additions & 2 deletions packages/agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import { ActorSubclass } from './actor';

export * from './actor';
export * from './agent';
export * from './auth';
export * from './certificate';
export * from './agent/http/transforms';
export * from './agent/http/types';
export * from './auth';
export * from './canisters/asset';
export * from './certificate';
export * from './der';
export * from './fetch_candid';
export * from './public_key';
export * from './request_id';
export * from './utils/bls';
export * from './utils/buffer';
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion packages/agent/src/utils/random.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ beforeEach(() => {
(global as any).crypto = undefined;
});

function isInteger(num) {
function isInteger(num: number) {
if (typeof num !== 'number') return false;
if (isNaN(num)) return false;
if (num % 1 !== 0) {
Expand Down
15 changes: 0 additions & 15 deletions packages/identity/src/buffer.ts

This file was deleted.

13 changes: 7 additions & 6 deletions packages/identity/src/identity/delegation.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import {
DerEncodedPublicKey,
fromHex,
HttpAgentRequest,
PublicKey,
requestIdOf,
Signature,
SignIdentity,
toHex,
} from '@dfinity/agent';
import { Principal } from '@dfinity/principal';
import * as cbor from 'simple-cbor';
import { fromHexString, toHexString } from '../buffer';

const domainSeparator = new TextEncoder().encode('\x1Aic-request-auth-delegation');
const requestDomainSeparator = new TextEncoder().encode('\x0Aic-request');
Expand All @@ -18,7 +19,7 @@ function _parseBlob(value: unknown): ArrayBuffer {
throw new Error('Invalid public key.');
}

return fromHexString(value);
return fromHex(value);
}

/**
Expand Down Expand Up @@ -51,7 +52,7 @@ export class Delegation {
// with an OID). After de-hex, if it's not obvious what it is, it's an ArrayBuffer.
return {
expiration: this.expiration.toString(16),
pubkey: toHexString(this.pubkey),
pubkey: toHex(this.pubkey),
...(this.targets && { targets: this.targets.map(p => p.toHex()) }),
};
}
Expand Down Expand Up @@ -247,15 +248,15 @@ export class DelegationChain {
return {
delegation: {
expiration: delegation.expiration.toString(16),
pubkey: toHexString(delegation.pubkey),
pubkey: toHex(delegation.pubkey),
...(targets && {
targets: targets.map(t => t.toHex()),
}),
},
signature: toHexString(signature),
signature: toHex(signature),
};
}),
publicKey: toHexString(this.publicKey),
publicKey: toHex(this.publicKey),
};
}
}
Expand Down
163 changes: 0 additions & 163 deletions packages/identity/src/identity/der.test.ts

This file was deleted.

Loading

0 comments on commit 5cf8707

Please sign in to comment.