diff --git a/package.json b/package.json index 084aaa79..07c789d6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fluence", - "version": "0.5.0", + "version": "0.5.1", "description": "the browser js-libp2p client for the Fluence network", "main": "./dist/fluence.js", "typings": "./dist/fluence.d.ts", diff --git a/src/misc.ts b/src/misc.ts new file mode 100644 index 00000000..da884ffa --- /dev/null +++ b/src/misc.ts @@ -0,0 +1,34 @@ +/* + * Copyright 2020 Fluence Labs Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import * as PeerId from "peer-id"; +import {decode, encode} from "bs58" +import {keys} from "libp2p-crypto"; + +/** + * @param seed 32 bytes + */ +export async function seedToPeerId(seed: string): Promise { + let seedArr = decode(seed); + + let privateK = await keys.generateKeyPairFromSeed("Ed25519", Uint8Array.from(seedArr), 256); + return await PeerId.createFromPrivKey(privateK.bytes); +} + +export function peerIdToSeed(peerId: PeerId): string { + let seedBuf = peerId.privKey.marshal().subarray(0, 32) + return encode(seedBuf) +} diff --git a/src/test/address.spec.ts b/src/test/address.spec.ts index 98b7600c..8db02fd9 100644 --- a/src/test/address.spec.ts +++ b/src/test/address.spec.ts @@ -8,12 +8,14 @@ import { import {expect} from 'chai'; import 'mocha'; +import {decode, encode} from "bs58" import * as PeerId from "peer-id"; import {callToString, genUUID, makeFunctionCall, parseFunctionCall} from "../function_call"; import Fluence from "../fluence"; import {certificateFromString, certificateToString, issue} from "../trust/certificate"; import {TrustGraph} from "../trust/trust_graph"; import {nodeRootCert} from "../trust/misc"; +import {peerIdToSeed, seedToPeerId} from "../misc"; describe("Typescript usage suite", () => { @@ -92,6 +94,14 @@ describe("Typescript usage suite", () => { }); + it("should create private key from seed and back", async function () { + let seed = [46, 188, 245, 171, 145, 73, 40, 24, 52, 233, 215, 163, 54, 26, 31, 221, 159, 179, 126, 106, 27, 199, 189, 194, 80, 133, 235, 42, 42, 247, 80, 201]; + let seedStr = encode(seed) + console.log("SEED STR: " + seedStr) + let pid = await seedToPeerId(seedStr) + expect(peerIdToSeed(pid)).to.be.equal(seedStr) + }) + it("should serialize and deserialize certificate correctly", async function () { let cert = `11 1111 @@ -111,7 +121,7 @@ describe("Typescript usage suite", () => { }); // delete `.skip` and run `npm run test` to check service's and certificate's api with Fluence nodes - it("integration test", async function () { + it.skip("integration test", async function () { this.timeout(15000); await testCerts(); // await testCalculator(); diff --git a/src/trust/certificate.ts b/src/trust/certificate.ts index 1acf6083..9ca331ef 100644 --- a/src/trust/certificate.ts +++ b/src/trust/certificate.ts @@ -41,7 +41,6 @@ export async function certificateFromString(str: string): Promise { // TODO do match different formats and versions let _format = lines[0]; let _version = lines[1]; - console.log("LENGTH: " + lines.length) // every trust is 4 lines, certificate lines number without format and version should be divided by 4 if ((lines.length - 2) % 4 !== 0) {