Replace fromBytes in favor of fromEd25519SK in KeyPair class (#81)

* Replace fromBytes in favor of fromEd25519SK in KeyPair class
This commit is contained in:
Pavel 2021-09-24 17:19:28 +03:00 committed by GitHub
parent 1c457dd2cf
commit a5cd6d5a49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 37 deletions

View File

@ -3,26 +3,13 @@ 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
@ -31,14 +18,14 @@ describe('KeyPair tests', () => {
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);
});
@ -47,10 +34,10 @@ describe('KeyPair tests', () => {
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);
});
});

View File

@ -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);

View File

@ -25,25 +25,15 @@ export class KeyPair {
public Libp2pPeerId: PeerId;
constructor(libp2pPeerId: PeerId) {
this.Libp2pPeerId = libp2pPeerId
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);