mirror of
https://github.com/fluencelabs/fluence-js.git
synced 2025-03-15 07:20:49 +00:00
Replace fromBytes in favor of fromEd25519SK in KeyPair class (#81)
* Replace fromBytes in favor of fromEd25519SK in KeyPair class
This commit is contained in:
parent
1c457dd2cf
commit
a5cd6d5a49
@ -3,54 +3,41 @@ import * as base64 from 'base64-js';
|
|||||||
import { KeyPair } from '../../internal/KeyPair';
|
import { KeyPair } from '../../internal/KeyPair';
|
||||||
|
|
||||||
describe('KeyPair tests', () => {
|
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 () {
|
it('generate keypair from seed', async function () {
|
||||||
// arrange
|
// arrange
|
||||||
const random = await KeyPair.randomEd25519();
|
const random = await KeyPair.randomEd25519();
|
||||||
const privateKey = random.toEd25519PrivateKey();
|
const privateKey = random.toEd25519PrivateKey();
|
||||||
|
|
||||||
// act
|
// act
|
||||||
const keyPair = await KeyPair.fromBytes(privateKey);
|
const keyPair = await KeyPair.fromEd25519SK(privateKey);
|
||||||
const privateKey2 = keyPair.toEd25519PrivateKey();
|
const privateKey2 = keyPair.toEd25519PrivateKey();
|
||||||
|
|
||||||
// assert
|
// assert
|
||||||
expect(privateKey).toStrictEqual(privateKey2);
|
expect(privateKey).toStrictEqual(privateKey2);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('create keypair from ed25519 private key', async function() {
|
it('create keypair from ed25519 private key', async function () {
|
||||||
// arrange
|
// arrange
|
||||||
const rustSK = "jDaxLJzYtzgwTMrELJCAqavtmx85ktQNfB2rLcK7MhH";
|
const rustSK = 'jDaxLJzYtzgwTMrELJCAqavtmx85ktQNfB2rLcK7MhH';
|
||||||
const sk = bs58.decode(rustSK);
|
const sk = bs58.decode(rustSK);
|
||||||
|
|
||||||
// act
|
// act
|
||||||
const keyPair = await KeyPair.fromBytes(sk);
|
const keyPair = await KeyPair.fromEd25519SK(sk);
|
||||||
|
|
||||||
// assert
|
// assert
|
||||||
const expectedPeerId = "12D3KooWH1W3VznVZ87JH4FwABK4mkntcspTVWJDta6c2xg9Pzbp";
|
const expectedPeerId = '12D3KooWH1W3VznVZ87JH4FwABK4mkntcspTVWJDta6c2xg9Pzbp';
|
||||||
expect(keyPair.Libp2pPeerId.toB58String()).toStrictEqual(expectedPeerId);
|
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
|
// arrange
|
||||||
const seedArray = new Uint8Array(32).fill(1);
|
const seedArray = new Uint8Array(32).fill(1);
|
||||||
|
|
||||||
// act
|
// act
|
||||||
const keyPair = await KeyPair.fromBytes(seedArray);
|
const keyPair = await KeyPair.fromEd25519SK(seedArray);
|
||||||
|
|
||||||
// assert
|
// assert
|
||||||
const expectedPeerId = "12D3KooWK99VoVxNE7XzyBwXEzW7xhK7Gpv85r9F3V3fyKSUKPH5";
|
const expectedPeerId = '12D3KooWK99VoVxNE7XzyBwXEzW7xhK7Gpv85r9F3V3fyKSUKPH5';
|
||||||
expect(keyPair.Libp2pPeerId.toB58String()).toStrictEqual(expectedPeerId);
|
expect(keyPair.Libp2pPeerId.toB58String()).toStrictEqual(expectedPeerId);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -1,16 +1,18 @@
|
|||||||
import { KeyPair } from '../../internal/KeyPair';
|
import { KeyPair } from '../../internal/KeyPair';
|
||||||
import { RequestFlow } from '../../internal/RequestFlow';
|
import { RequestFlow } from '../../internal/RequestFlow';
|
||||||
|
import * as base64 from 'base64-js';
|
||||||
|
|
||||||
describe('Request flow tests', () => {
|
describe('Request flow tests', () => {
|
||||||
it('particle initiation should work', async () => {
|
it('particle initiation should work', async () => {
|
||||||
// arrange
|
// arrange
|
||||||
jest.useFakeTimers();
|
jest.useFakeTimers();
|
||||||
const sk = 'z1x3cVXhk9nJKE1pZaX9KxccUBzxu3aGlaUjDdAB2oY=';
|
const sk = 'z1x3cVXhk9nJKE1pZaX9KxccUBzxu3aGlaUjDdAB2oY=';
|
||||||
|
const skBytes = base64.toByteArray(sk);
|
||||||
const mockDate = new Date(Date.UTC(2021, 2, 14)).valueOf();
|
const mockDate = new Date(Date.UTC(2021, 2, 14)).valueOf();
|
||||||
Date.now = jest.fn(() => mockDate);
|
Date.now = jest.fn(() => mockDate);
|
||||||
|
|
||||||
const request = RequestFlow.createLocal('(null)', 10000);
|
const request = RequestFlow.createLocal('(null)', 10000);
|
||||||
const peerId = await (await KeyPair.fromEd25519SK(sk)).Libp2pPeerId;
|
const peerId = await (await KeyPair.fromEd25519SK(skBytes)).Libp2pPeerId;
|
||||||
|
|
||||||
// act
|
// act
|
||||||
await request.initState(peerId);
|
await request.initState(peerId);
|
||||||
|
@ -24,26 +24,16 @@ export class KeyPair {
|
|||||||
*/
|
*/
|
||||||
public Libp2pPeerId: PeerId;
|
public Libp2pPeerId: PeerId;
|
||||||
|
|
||||||
constructor(libp2pPeerId: PeerId) {
|
constructor(libp2pPeerId: PeerId) {
|
||||||
this.Libp2pPeerId = libp2pPeerId
|
this.Libp2pPeerId = libp2pPeerId;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates new KeyPair from base64 string containing the 32 byte Ed25519 private key
|
* Generates new KeyPair from ed25519 private key represented as a 32 byte array
|
||||||
* @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
|
|
||||||
* @param key - Any sequence of 32 bytes
|
* @param key - Any sequence of 32 bytes
|
||||||
* @returns - Promise with the created KeyPair
|
* @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
|
// generateKeyPairFromSeed takes seed and copies it to private key as is
|
||||||
const privateKey = await keys.generateKeyPairFromSeed('Ed25519', arr, 256);
|
const privateKey = await keys.generateKeyPairFromSeed('Ed25519', arr, 256);
|
||||||
const lib2p2Pid = await PeerId.createFromPrivKey(privateKey.bytes);
|
const lib2p2Pid = await PeerId.createFromPrivKey(privateKey.bytes);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user