I’m using this logic to generate identity in NODE env:
import { Secp256k1KeyIdentity } from '@dfinity/identity';
import hdkey from 'hdkey';
import bip39 from 'bip39';
// Completely insecure seed phrase. Do not use for any purpose other than testing.
const seed = 'test test test test test test test test test test test test';
export const identityFromSeed = async (phrase) => {
const seed = await bip39.mnemonicToSeed(phrase);
const root = hdkey.fromMasterSeed(seed);
const addrnode = root.derive("m/44'/223'/0'/0/0");
return Secp256k1KeyIdentity.fromSecretKey(addrnode.privateKey);
};
export const identity = identityFromSeed(seed);
and then use it to authenticate my requests to the canisters:
const agent = new HttpAgent({ host: 'http://0.0.0.0:4943', fetch, identity });
How do I do the same thing in browser env? I’d like to use the same generated identity while creating AuthClient
to authenticate as the same user.
const client = await AuthClient.create({identity});
Both bip39
& hdkey
work fine in node env, but not in a browser? Is there a way to generate an identity which can be used in node and browser?
1 Like
In 0.14.1, you can use Secp256k1KeyIdentity.fromSeed
and the logic is bundled in. In 0.15.0, Secp256k1KeyIdentity
will be moving out of Identity into its own package, @dfinity/identity-secp256k1
, because of the size of these dependencies
4 Likes
@kpeacock I updated the code to generate a random identity to:
import { Secp256k1KeyIdentity } from '@dfinity/identity-secp256k1';
// Completely insecure seed phrase. Do not use for any purpose other than testing.
const seed = 'test test test test test test test test test test test test';
export const identityFromSeed = async (phrase) => {
return Secp256k1KeyIdentity.fromSeedPhrase(phrase);
};
export const identity = identityFromSeed(seed);
And then using the exported identity like so:
const agent = new HttpAgent({ host: 'http://0.0.0.0:4943', fetch, identity });
results in this error:
Error: input is invalid type
❯ Sha256.update ../../node_modules/js-sha256/src/sha256.js:177:19
❯ Secp256k1KeyIdentity.sign ../../node_modules/@dfinity/identity-secp256k1/src/secp256k1.ts:204:10
❯ Secp256k1KeyIdentity.transformRequest ../../node_modules/@dfinity/agent/src/auth.ts:94:32
❯ HttpAgent.call ../../node_modules/@dfinity/agent/src/agent/http/index.ts:331:4
❯ caller ../../node_modules/@dfinity/agent/src/actor.ts:376:26
Edit: I’m using "@dfinity/identity-secp256k1": "0.15.1",
I’m assuming you also updated import { Secp256k1KeyIdentity } from '@dfinity/identity';
to @dfinity/identity-secp256k1
.
Have you also updated @dfinity/agent
to 0.15.1
?
Yes, I updated the code in my previous reply
@dfinity/agent
is 0.15.1
.
@dfinity/identity-secp256k1
is 0.15.1
Basically everything is up to date.
And I’m creating this agent in vitest
if that matters.
Should I create a new issue for this on the github repo?