Skip to content

Commit a5cd6d5

Browse files
authored
Replace fromBytes in favor of fromEd25519SK in KeyPair class (#81)
* Replace fromBytes in favor of fromEd25519SK in KeyPair class
1 parent 1c457dd commit a5cd6d5

File tree

3 files changed

+16
-37
lines changed

3 files changed

+16
-37
lines changed

src/__test__/unit/KeyPair.spec.ts

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,41 @@ import * as base64 from 'base64-js';
33
import { KeyPair } from '../../internal/KeyPair';
44

55
describe('KeyPair tests', () => {
6-
it('should create private key from seed and back', async function () {
7-
// arrange
8-
const sk = 'z1x3cVXhk9nJKE1pZaX9KxccUBzxu3aGlaUjDdAB2oY=';
9-
10-
// act
11-
const keyPair = await KeyPair.fromEd25519SK(sk);
12-
const privateKey = keyPair.toEd25519PrivateKey();
13-
const sk2 = base64.fromByteArray(privateKey)
14-
15-
// assert
16-
expect(sk2).toBe(sk);
17-
});
18-
196
it('generate keypair from seed', async function () {
207
// arrange
218
const random = await KeyPair.randomEd25519();
229
const privateKey = random.toEd25519PrivateKey();
2310

2411
// act
25-
const keyPair = await KeyPair.fromBytes(privateKey);
12+
const keyPair = await KeyPair.fromEd25519SK(privateKey);
2613
const privateKey2 = keyPair.toEd25519PrivateKey();
2714

2815
// assert
2916
expect(privateKey).toStrictEqual(privateKey2);
3017
});
3118

32-
it('create keypair from ed25519 private key', async function() {
19+
it('create keypair from ed25519 private key', async function () {
3320
// arrange
34-
const rustSK = "jDaxLJzYtzgwTMrELJCAqavtmx85ktQNfB2rLcK7MhH";
21+
const rustSK = 'jDaxLJzYtzgwTMrELJCAqavtmx85ktQNfB2rLcK7MhH';
3522
const sk = bs58.decode(rustSK);
3623

3724
// act
38-
const keyPair = await KeyPair.fromBytes(sk);
25+
const keyPair = await KeyPair.fromEd25519SK(sk);
3926

4027
// assert
41-
const expectedPeerId = "12D3KooWH1W3VznVZ87JH4FwABK4mkntcspTVWJDta6c2xg9Pzbp";
28+
const expectedPeerId = '12D3KooWH1W3VznVZ87JH4FwABK4mkntcspTVWJDta6c2xg9Pzbp';
4229
expect(keyPair.Libp2pPeerId.toB58String()).toStrictEqual(expectedPeerId);
4330
});
4431

45-
it('create keypair from a seed phrase', async function() {
32+
it('create keypair from a seed phrase', async function () {
4633
// arrange
4734
const seedArray = new Uint8Array(32).fill(1);
48-
35+
4936
// act
50-
const keyPair = await KeyPair.fromBytes(seedArray);
37+
const keyPair = await KeyPair.fromEd25519SK(seedArray);
5138

5239
// assert
53-
const expectedPeerId = "12D3KooWK99VoVxNE7XzyBwXEzW7xhK7Gpv85r9F3V3fyKSUKPH5";
40+
const expectedPeerId = '12D3KooWK99VoVxNE7XzyBwXEzW7xhK7Gpv85r9F3V3fyKSUKPH5';
5441
expect(keyPair.Libp2pPeerId.toB58String()).toStrictEqual(expectedPeerId);
5542
});
5643
});

src/__test__/unit/RequestFlow.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import { KeyPair } from '../../internal/KeyPair';
22
import { RequestFlow } from '../../internal/RequestFlow';
3+
import * as base64 from 'base64-js';
34

45
describe('Request flow tests', () => {
56
it('particle initiation should work', async () => {
67
// arrange
78
jest.useFakeTimers();
89
const sk = 'z1x3cVXhk9nJKE1pZaX9KxccUBzxu3aGlaUjDdAB2oY=';
10+
const skBytes = base64.toByteArray(sk);
911
const mockDate = new Date(Date.UTC(2021, 2, 14)).valueOf();
1012
Date.now = jest.fn(() => mockDate);
1113

1214
const request = RequestFlow.createLocal('(null)', 10000);
13-
const peerId = await (await KeyPair.fromEd25519SK(sk)).Libp2pPeerId;
15+
const peerId = await (await KeyPair.fromEd25519SK(skBytes)).Libp2pPeerId;
1416

1517
// act
1618
await request.initState(peerId);

src/internal/KeyPair.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,16 @@ export class KeyPair {
2424
*/
2525
public Libp2pPeerId: PeerId;
2626

27-
constructor(libp2pPeerId: PeerId) {
28-
this.Libp2pPeerId = libp2pPeerId
27+
constructor(libp2pPeerId: PeerId) {
28+
this.Libp2pPeerId = libp2pPeerId;
2929
}
3030

3131
/**
32-
* Generates new KeyPair from base64 string containing the 32 byte Ed25519 private key
33-
* @returns - Promise with the created KeyPair
34-
*/
35-
static async fromEd25519SK(base64Key: string): Promise<KeyPair> {
36-
// deserialize private key from base64
37-
const key = base64.toByteArray(base64Key);
38-
return await KeyPair.fromBytes(key);
39-
}
40-
41-
/**
42-
* Generates new KeyPair from a 32 byte array
32+
* Generates new KeyPair from ed25519 private key represented as a 32 byte array
4333
* @param key - Any sequence of 32 bytes
4434
* @returns - Promise with the created KeyPair
4535
*/
46-
static async fromBytes(arr: Uint8Array): Promise<KeyPair> {
36+
static async fromEd25519SK(arr: Uint8Array): Promise<KeyPair> {
4737
// generateKeyPairFromSeed takes seed and copies it to private key as is
4838
const privateKey = await keys.generateKeyPairFromSeed('Ed25519', arr, 256);
4939
const lib2p2Pid = await PeerId.createFromPrivKey(privateKey.bytes);

0 commit comments

Comments
 (0)