diff --git a/src/__test__/unit/KeyPair.spec.ts b/src/__test__/unit/KeyPair.spec.ts index 76de8471..c4678ed3 100644 --- a/src/__test__/unit/KeyPair.spec.ts +++ b/src/__test__/unit/KeyPair.spec.ts @@ -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); }); }); diff --git a/src/__test__/unit/RequestFlow.spec.ts b/src/__test__/unit/RequestFlow.spec.ts index af96dae3..f0cd60f2 100644 --- a/src/__test__/unit/RequestFlow.spec.ts +++ b/src/__test__/unit/RequestFlow.spec.ts @@ -1,16 +1,18 @@ import { KeyPair } from '../../internal/KeyPair'; import { RequestFlow } from '../../internal/RequestFlow'; +import * as base64 from 'base64-js'; describe('Request flow tests', () => { it('particle initiation should work', async () => { // arrange jest.useFakeTimers(); const sk = 'z1x3cVXhk9nJKE1pZaX9KxccUBzxu3aGlaUjDdAB2oY='; + const skBytes = base64.toByteArray(sk); const mockDate = new Date(Date.UTC(2021, 2, 14)).valueOf(); Date.now = jest.fn(() => mockDate); const request = RequestFlow.createLocal('(null)', 10000); - const peerId = await (await KeyPair.fromEd25519SK(sk)).Libp2pPeerId; + const peerId = await (await KeyPair.fromEd25519SK(skBytes)).Libp2pPeerId; // act await request.initState(peerId); diff --git a/src/internal/KeyPair.ts b/src/internal/KeyPair.ts index 794a95e1..3a3e0d20 100644 --- a/src/internal/KeyPair.ts +++ b/src/internal/KeyPair.ts @@ -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 { - // 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 { + static async fromEd25519SK(arr: Uint8Array): Promise { // 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);