Skip to content
This repository was archived by the owner on Jul 10, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
31 changes: 9 additions & 22 deletions src/__test__/unit/KeyPair.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,41 @@ import * as base64 from 'base64-js';
import { KeyPair } from '../../internal/KeyPair';

describe('KeyPair tests', () => {
it('should create private key from seed and back', async function () {
// arrange
const sk = 'z1x3cVXhk9nJKE1pZaX9KxccUBzxu3aGlaUjDdAB2oY=';

// act
const keyPair = await KeyPair.fromEd25519SK(sk);
const privateKey = keyPair.toEd25519PrivateKey();
const sk2 = base64.fromByteArray(privateKey)

// assert
expect(sk2).toBe(sk);
});

it('generate keypair from seed', async function () {
// arrange
const random = await KeyPair.randomEd25519();
const privateKey = random.toEd25519PrivateKey();

// act
const keyPair = await KeyPair.fromBytes(privateKey);
const keyPair = await KeyPair.fromEd25519SK(privateKey);
const privateKey2 = keyPair.toEd25519PrivateKey();

// assert
expect(privateKey).toStrictEqual(privateKey2);
});

it('create keypair from ed25519 private key', async function() {
it('create keypair from ed25519 private key', async function () {
// arrange
const rustSK = "jDaxLJzYtzgwTMrELJCAqavtmx85ktQNfB2rLcK7MhH";
const rustSK = 'jDaxLJzYtzgwTMrELJCAqavtmx85ktQNfB2rLcK7MhH';
const sk = bs58.decode(rustSK);

// act
const keyPair = await KeyPair.fromBytes(sk);
const keyPair = await KeyPair.fromEd25519SK(sk);

// assert
const expectedPeerId = "12D3KooWH1W3VznVZ87JH4FwABK4mkntcspTVWJDta6c2xg9Pzbp";
const expectedPeerId = '12D3KooWH1W3VznVZ87JH4FwABK4mkntcspTVWJDta6c2xg9Pzbp';
expect(keyPair.Libp2pPeerId.toB58String()).toStrictEqual(expectedPeerId);
});

it('create keypair from a seed phrase', async function() {
it('create keypair from a seed phrase', async function () {
// arrange
const seedArray = new Uint8Array(32).fill(1);

// act
const keyPair = await KeyPair.fromBytes(seedArray);
const keyPair = await KeyPair.fromEd25519SK(seedArray);

// assert
const expectedPeerId = "12D3KooWK99VoVxNE7XzyBwXEzW7xhK7Gpv85r9F3V3fyKSUKPH5";
const expectedPeerId = '12D3KooWK99VoVxNE7XzyBwXEzW7xhK7Gpv85r9F3V3fyKSUKPH5';
expect(keyPair.Libp2pPeerId.toB58String()).toStrictEqual(expectedPeerId);
});
});
18 changes: 4 additions & 14 deletions src/internal/KeyPair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,16 @@ export class KeyPair {
*/
public Libp2pPeerId: PeerId;

constructor(libp2pPeerId: PeerId) {
this.Libp2pPeerId = libp2pPeerId
constructor(libp2pPeerId: PeerId) {
this.Libp2pPeerId = libp2pPeerId;
}

/**
* Generates new KeyPair from base64 string containing the 32 byte Ed25519 private key
* @returns - Promise with the created KeyPair
*/
static async fromEd25519SK(base64Key: string): Promise<KeyPair> {
// deserialize private key from base64
const key = base64.toByteArray(base64Key);
return await KeyPair.fromBytes(key);
}

/**
* Generates new KeyPair from a 32 byte array
* Generates new KeyPair from ed25519 private key represented as a 32 byte array
* @param key - Any sequence of 32 bytes
* @returns - Promise with the created KeyPair
*/
static async fromBytes(arr: Uint8Array): Promise<KeyPair> {
static async fromEd25519SK(arr: Uint8Array): Promise<KeyPair> {
// generateKeyPairFromSeed takes seed and copies it to private key as is
const privateKey = await keys.generateKeyPairFromSeed('Ed25519', arr, 256);
const lib2p2Pid = await PeerId.createFromPrivKey(privateKey.bytes);
Expand Down