Skip to content

Commit

Permalink
fix: ed25519KeyIdentity generates unique identities when no seed is p…
Browse files Browse the repository at this point in the history
…rovided (#851)
  • Loading branch information
krpeacock authored Feb 20, 2024
1 parent 166df8d commit f72cf94
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 29 deletions.
1 change: 1 addition & 0 deletions docs/generated/changelog.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ <h1>Agent-JS Changelog</h1>
<section>
<h2>Version x.x.x</h2>
<ul>
<li>fix: ed25519KeyIdentity was not generating unique identities when no seed was provided. This issue was introduced in v0.20.0-beta.0. If your code was affected please upgrade to >=1.0.1</li>
<li>chore: export `AuthClientStorage` to aid with custom implementations</li>
</ul>
<h2>Version 1.0.0</h2>
Expand Down
56 changes: 28 additions & 28 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions packages/identity/src/identity/ed25519.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,20 @@ describe('Ed25519KeyIdentity tests', () => {

expect(isValid).toBe(true);
});

it('generates random private keys', () => {
const key1 = Ed25519KeyIdentity.generate();
const key2 = Ed25519KeyIdentity.generate();
expect(key1.toJSON().toString()).not.toEqual(key2.toJSON().toString());
});

it('should warn if the key is an Uint8Array consisting of all zeroes', () => {
const consoleSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});

const baseKey = new Uint8Array(new Array(32).fill(0));
Ed25519KeyIdentity.generate(baseKey);
expect(consoleSpy).toHaveBeenCalledWith("Seed is all zeros. This is not a secure seed. Please provide a seed with sufficient entropy if this is a production environment.");
});
});

test('from JSON', async () => {
Expand Down
16 changes: 15 additions & 1 deletion packages/identity/src/identity/ed25519.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { bufEquals } from '@dfinity/agent';
import {
DerEncodedPublicKey,
KeyPair,
Expand Down Expand Up @@ -105,12 +106,25 @@ export class Ed25519PublicKey implements PublicKey {
}
}

/**
* Ed25519KeyIdentity is an implementation of SignIdentity that uses Ed25519 keys. This class is used to sign and verify messages for an agent.
*/
export class Ed25519KeyIdentity extends SignIdentity {
public static generate(seed = new Uint8Array(32)): Ed25519KeyIdentity {
/**
* Generate a new Ed25519KeyIdentity.
* @param seed a 32-byte seed for the private key. If not provided, a random seed will be generated.
* @returns Ed25519KeyIdentity
*/
public static generate(seed?: Uint8Array): Ed25519KeyIdentity {

if (seed && seed.length !== 32) {
throw new Error('Ed25519 Seed needs to be 32 bytes long.');
}
if (!seed) seed = ed25519.utils.randomPrivateKey();
// Check if the seed is all zeros
if(bufEquals(seed, new Uint8Array(new Array(32).fill(0)))) {
console.warn('Seed is all zeros. This is not a secure seed. Please provide a seed with sufficient entropy if this is a production environment.');
}
const sk = new Uint8Array(32);
for (let i = 0; i < 32; i++) sk[i] = new Uint8Array(seed)[i];

Expand Down

0 comments on commit f72cf94

Please sign in to comment.