fluence-js/src/particle.ts

150 lines
3.9 KiB
TypeScript
Raw Normal View History

2020-09-28 17:01:49 +03:00
/*
* 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 { v4 as uuidv4 } from 'uuid';
2020-12-24 19:36:38 +03:00
import {fromByteArray, toByteArray} from 'base64-js';
2020-12-23 17:24:22 +03:00
import PeerId from 'peer-id';
import { encode } from 'bs58';
import { addData } from './dataStorage';
2020-09-28 17:01:49 +03:00
const DEFAULT_TTL = 7000;
export interface Particle {
2020-12-23 17:24:22 +03:00
id: string;
init_peer_id: string;
timestamp: number;
ttl: number;
script: string;
2020-09-28 17:01:49 +03:00
// sign upper fields
2020-12-23 17:24:22 +03:00
signature: string;
2020-12-24 19:11:10 +03:00
data: Uint8Array;
}
/**
* Represents particle action to send to a node
*/
interface ParticleAction {
action: 'Particle'
id: string;
init_peer_id: string;
timestamp: number;
ttl: number;
script: string;
signature: number[];
data: string;
2020-09-28 17:01:49 +03:00
}
2020-11-04 00:03:22 +03:00
function wrapScript(selfPeerId: string, script: string, fields: string[]): string {
fields.forEach((v) => {
2020-12-23 17:24:22 +03:00
script = `
2020-11-04 00:03:22 +03:00
(seq
2020-12-04 17:08:35 +03:00
(call %init_peer_id% ("" "load") ["${v}"] ${v})
2020-11-04 00:03:22 +03:00
${script}
)
2020-12-23 17:24:22 +03:00
`;
2020-11-04 00:03:22 +03:00
});
return script;
}
export async function build(peerId: PeerId, script: string, data: Map<string, any>, ttl?: number): Promise<Particle> {
2020-09-28 17:01:49 +03:00
let id = genUUID();
2020-12-23 17:24:22 +03:00
let currentTime = new Date().getTime();
2020-09-28 17:01:49 +03:00
2020-10-12 14:07:28 +03:00
if (ttl === undefined) {
2020-12-23 17:24:22 +03:00
ttl = DEFAULT_TTL;
2020-10-05 17:17:04 +03:00
}
2020-09-28 17:01:49 +03:00
2020-11-04 00:03:22 +03:00
addData(id, data, ttl);
2020-12-23 17:24:22 +03:00
script = wrapScript(peerId.toB58String(), script, Array.from(data.keys()));
2020-11-04 00:03:22 +03:00
2020-09-28 17:01:49 +03:00
let particle: Particle = {
id: id,
init_peer_id: peerId.toB58String(),
timestamp: currentTime,
ttl: ttl,
script: script,
2020-12-23 17:24:22 +03:00
signature: '',
2020-12-24 19:11:10 +03:00
data: Buffer.from([]),
2020-12-23 17:24:22 +03:00
};
2020-09-28 17:01:49 +03:00
particle.signature = await signParticle(peerId, particle);
return particle;
}
/**
2020-12-24 19:11:10 +03:00
* Creates an action to send to a node.
2020-09-28 17:01:49 +03:00
*/
2020-12-24 19:11:10 +03:00
export function toAction(particle: Particle): ParticleAction {
return {
action: "Particle",
id: particle.id,
init_peer_id: particle.init_peer_id,
timestamp: particle.timestamp,
ttl: particle.ttl,
script: particle.script,
// TODO: copy signature from a particle after signatures will be implemented on nodes
signature: [],
data: fromByteArray(particle.data)
};
2020-09-28 17:01:49 +03:00
}
export function parseParticle(str: string): Particle {
let json = JSON.parse(str);
return {
id: json.id,
init_peer_id: json.init_peer_id,
timestamp: json.timestamp,
ttl: json.ttl,
script: json.script,
signature: json.signature,
2020-12-24 19:36:38 +03:00
data: toByteArray(json.data),
2020-12-23 17:24:22 +03:00
};
2020-09-28 17:01:49 +03:00
}
export function canonicalBytes(particle: Particle) {
let peerIdBuf = Buffer.from(particle.init_peer_id, 'utf8');
let idBuf = Buffer.from(particle.id, 'utf8');
let tsArr = new ArrayBuffer(8);
new DataView(tsArr).setBigUint64(0, BigInt(particle.timestamp));
let tsBuf = Buffer.from(tsArr);
let ttlArr = new ArrayBuffer(4);
new DataView(ttlArr).setUint32(0, particle.ttl);
let ttlBuf = Buffer.from(ttlArr);
let scriptBuf = Buffer.from(particle.script, 'utf8');
return Buffer.concat([peerIdBuf, idBuf, tsBuf, ttlBuf, scriptBuf]);
}
/**
* Sign a particle with a private key from peerId.
*/
2020-12-23 17:24:22 +03:00
export async function signParticle(peerId: PeerId, particle: Particle): Promise<string> {
2020-09-28 17:01:49 +03:00
let bufToSign = canonicalBytes(particle);
2020-12-23 17:24:22 +03:00
let signature = await peerId.privKey.sign(bufToSign);
return encode(signature);
2020-09-28 17:01:49 +03:00
}
export function genUUID() {
return uuidv4();
}