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

build: bump agent-js v0.20.2 #4067

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6,911 changes: 659 additions & 6,252 deletions frontend/package-lock.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,19 @@
},
"type": "module",
"dependencies": {
"@dfinity/agent": "^0.19.2",
"@dfinity/auth-client": "^0.19.2",
"@dfinity/candid": "^0.19.2",
"@dfinity/agent": "^0.20.2",
"@dfinity/auth-client": "^0.20.2",
"@dfinity/candid": "^0.20.2",
"@dfinity/ckbtc": "next",
"@dfinity/cmc": "next",
"@dfinity/gix-components": "next",
"@dfinity/ic-management": "next",
"@dfinity/identity": "^0.19.2",
"@dfinity/identity": "^0.20.2",
"@dfinity/ledger-icp": "next",
"@dfinity/ledger-icrc": "next",
"@dfinity/nns": "next",
"@dfinity/nns-proto": "next",
"@dfinity/principal": "^0.19.2",
"@dfinity/principal": "^0.20.2",
"@dfinity/sns": "next",
"@dfinity/utils": "next",
"@ledgerhq/hw-transport-node-hid-noevents": "^6.27.8",
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/lib/api/dev.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const getTestAccountAgent = async (): Promise<Agent> => {
const agent: Agent = new HttpAgent({
host: HOST,
identity,
verifyQuerySignatures: false,
});
await agent.fetchRootKey();

Expand Down Expand Up @@ -359,6 +360,7 @@ export const receiveMockBtc = async ({
}) => {
const agent = new HttpAgent({
host: HOST,
verifyQuerySignatures: false,
});
await agent.fetchRootKey();
const actor = Actor.createActor(mockBitcoinIdlFactory, {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/lib/identities/ledger.identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class LedgerIdentity extends SignIdentity {
}
}

public override getPublicKey(): PublicKey {
public override getPublicKey(): Required<PublicKey> {
return this.publicKey;
}

Expand Down
61 changes: 35 additions & 26 deletions frontend/src/lib/keys/secp256k1.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import type { DerEncodedPublicKey, PublicKey } from "@dfinity/agent";
import {
bufEquals,
concat,
type DerEncodedPublicKey,
type PublicKey,
} from "@dfinity/agent";

// TODO(L2-433): should we use @dfinity/identity-ledgerhq the implementation is 100% similar

Expand All @@ -7,6 +12,15 @@ import type { DerEncodedPublicKey, PublicKey } from "@dfinity/agent";
// This implementation is adjusted from the Ed25519PublicKey.
// The RAW_KEY_LENGTH and DER_PREFIX are modified accordingly
export class Secp256k1PublicKey implements PublicKey {
/**
* Construct Secp256k1PublicKey from an existing PublicKey
* @param {PublicKey} key
* @returns {Secp256k1PublicKey} Instance of Secp256k1PublicKey
*/
public static from(key: PublicKey): Secp256k1PublicKey {
return this.fromDer(key.toDer());
}

public static fromRaw(rawKey: ArrayBuffer): Secp256k1PublicKey {
return new Secp256k1PublicKey(rawKey);
}
Expand Down Expand Up @@ -37,12 +51,13 @@ export class Secp256k1PublicKey implements PublicKey {
);
}

const derPublicKey = Uint8Array.from([
...Secp256k1PublicKey.DER_PREFIX,
...new Uint8Array(publicKey),
]);
const derPublicKey = concat(
Secp256k1PublicKey.DER_PREFIX.buffer,
publicKey
) as DerEncodedPublicKey;
derPublicKey.__derEncodedPublicKey__ = undefined;

return derPublicKey.buffer as DerEncodedPublicKey;
return derPublicKey;
}

private static derDecode(key: DerEncodedPublicKey): ArrayBuffer {
Expand All @@ -55,23 +70,8 @@ export class Secp256k1PublicKey implements PublicKey {
);
}

const equals = (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;
};

const rawKey = key.slice(0, Secp256k1PublicKey.DER_PREFIX.length);
if (!equals(this.derEncode(rawKey), key)) {
if (!bufEquals(this.derEncode(rawKey), key)) {
throw new TypeError(
"secp256k1 DER-encoded public key is invalid. A valid secp256k1 DER-encoded public key " +
`must have the following prefix: ${Secp256k1PublicKey.DER_PREFIX}`
Expand All @@ -81,13 +81,22 @@ export class Secp256k1PublicKey implements PublicKey {
return rawKey;
}

private readonly rawKey: ArrayBuffer;
private readonly derKey: DerEncodedPublicKey;
#rawKey: ArrayBuffer;

public get rawKey(): ArrayBuffer {
return this.#rawKey;
}

#derKey: DerEncodedPublicKey;

public get derKey(): DerEncodedPublicKey {
return this.#derKey;
}

// `fromRaw` and `fromDer` should be used for instantiation, not this constructor.
private constructor(key: ArrayBuffer) {
this.rawKey = key;
this.derKey = Secp256k1PublicKey.derEncode(key);
this.#rawKey = key;
this.#derKey = Secp256k1PublicKey.derEncode(key);
}

public toDer(): DerEncodedPublicKey {
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/lib/services/seed-neurons.services.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createAgent } from "$lib/api/agent.api";
import { HOST } from "$lib/constants/environment.constants";
import type { Secp256k1PublicKey } from "$lib/keys/secp256k1";
import { Secp256k1PublicKey } from "$lib/keys/secp256k1";
import { getLedgerIdentityProxy } from "$lib/proxy/icp-ledger.services.proxy";
import { startBusy, stopBusy } from "$lib/stores/busy.store";
import { icpAccountsStore } from "$lib/stores/icp-accounts.store";
Expand Down Expand Up @@ -40,7 +40,7 @@ export const claimSeedNeurons = async () => {
agent: await createAgent({ identity, host: HOST }),
});

const bufferKey = identity.getPublicKey() as Secp256k1PublicKey;
const bufferKey = Secp256k1PublicKey.from(identity.getPublicKey());
const hexPubKey = buf2hex(bufferKey.toRaw());
const isHex = hexPubKey.match("^[0-9a-fA-F]+$");
if (!isHex) {
Expand Down
1 change: 1 addition & 0 deletions frontend/src/lib/worker-api/canisters.worker-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const canisters = async ({
const agent = new HttpAgent({
identity,
host,
verifyQuerySignatures: false,
});

if (fetchRootKey) {
Expand Down
1 change: 1 addition & 0 deletions frontend/src/lib/worker-utils/canister.worker-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const createCanisterWorker = async <T>({
const agent = new HttpAgent({
identity,
host,
verifyQuerySignatures: false,
});

if (fetchRootKey) {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/tests/lib/utils/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe("utils", () => {

it("should render principal as hash", () => {
expect(stringifyJson({ principal: mockPrincipal })).toBe(
`{"principal":"${mockPrincipal.toString()}"}`
`{"principal":{"__principal__":"${mockPrincipal.toString()}"}}`
);
});

Expand Down
Loading