switch proto definitions into json definitions

This commit is contained in:
Marin Petrunić 2020-02-06 09:51:39 +01:00
parent 2f6cc39888
commit b5100f3e47
No known key found for this signature in database
GPG Key ID: 834D07135E110DA5
4 changed files with 36 additions and 14 deletions

View File

@ -1,9 +0,0 @@
syntax = "proto3";
package pb;
message NoiseHandshakePayload {
bytes libp2p_key = 1;
bytes noise_static_key_signature = 2;
bytes libp2p_data = 3;
bytes libp2p_data_signature = 4;
}

24
src/proto/payload.json Normal file
View File

@ -0,0 +1,24 @@
{
"nested": {
"NoiseHandshakePayload": {
"fields": {
"libp2pKey": {
"type": "bytes",
"id": 1
},
"noiseStaticKeySignature": {
"type": "bytes",
"id": 2
},
"libp2pData": {
"type": "bytes",
"id": 3
},
"libp2pDataSignature": {
"type": "bytes",
"id": 4
}
}
}
}
}

View File

@ -3,14 +3,14 @@ import protobuf from "protobufjs";
import { Buffer } from "buffer"; import { Buffer } from "buffer";
import PeerId from "peer-id"; import PeerId from "peer-id";
import * as crypto from 'libp2p-crypto'; import * as crypto from 'libp2p-crypto';
import { KeyPair } from "./@types/libp2p"; import { KeyPair } from "./@types/libp2p";
import {bytes, bytes32} from "./@types/basic"; import {bytes, bytes32} from "./@types/basic";
import {Hkdf} from "./@types/handshake"; import {Hkdf} from "./@types/handshake";
import payloadProto from "./proto/payload.json";
export async function loadPayloadProto () { export async function loadPayloadProto () {
const payloadProtoBuf = await protobuf.load("protos/payload.proto"); const payloadProtoBuf = await protobuf.Root.fromJSON(payloadProto);
return payloadProtoBuf.lookupType("pb.NoiseHandshakePayload"); return payloadProtoBuf.lookupType("NoiseHandshakePayload");
} }
export function generateKeypair(): KeyPair { export function generateKeypair(): KeyPair {
@ -92,9 +92,15 @@ export async function verifySignedPayload(noiseStaticKey: bytes, plaintext: byte
let receivedPayload; let receivedPayload;
try { try {
const NoiseHandshakePayload = await loadPayloadProto(); const NoiseHandshakePayload = await loadPayloadProto();
receivedPayload = NoiseHandshakePayload.toObject(NoiseHandshakePayload.decode(plaintext)); receivedPayload = NoiseHandshakePayload.toObject(
NoiseHandshakePayload.decode(plaintext)
);
//temporary fix until protobufsjs conversion options starts working
//by default it ends up as Uint8Array
receivedPayload.libp2pKey = Buffer.from(receivedPayload.libp2pKey);
receivedPayload.noiseStaticKeySignature = Buffer.from(receivedPayload.noiseStaticKeySignature);
} catch (e) { } catch (e) {
throw new Error("Failed to decode received payload."); throw new Error("Failed to decode received payload. Reason: " + e.message);
} }
if (!(await isValidPeerId(peerId, receivedPayload.libp2pKey)) ) { if (!(await isValidPeerId(peerId, receivedPayload.libp2pKey)) ) {

View File

@ -3,6 +3,7 @@
"target": "es6", "target": "es6",
"module": "commonjs", "module": "commonjs",
"strict": true, "strict": true,
"resolveJsonModule": true,
"esModuleInterop": true, "esModuleInterop": true,
"noImplicitAny": false, "noImplicitAny": false,
"typeRoots": [ "typeRoots": [