Privkey conversion (#896)

This commit is contained in:
Dima 2020-05-27 15:04:13 +03:00 committed by GitHub
parent 4d306dab63
commit a28cce0b16
4 changed files with 46 additions and 3 deletions

View File

@ -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",

34
src/misc.ts Normal file
View File

@ -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<PeerId> {
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)
}

View File

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

View File

@ -41,7 +41,6 @@ export async function certificateFromString(str: string): Promise<Certificate> {
// 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) {