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

fix: ed25519KeyIdentity generates unique identities when no seed is provided #851

Merged
merged 1 commit into from
Feb 20, 2024
Merged
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
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
Loading