Do not use DataView (#28)

This commit is contained in:
folex 2021-03-03 17:27:00 +03:00 committed by GitHub
parent c65c0afbee
commit 052a8a7f24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 5 additions and 32 deletions

View File

@ -22,9 +22,9 @@ jobs:
- uses: actions/cache@v2
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ matrix.node-version }}-${{ hashFiles('**/package-lock.json') }}
key: ${{ runner.os }}-v1-node-${{ matrix.node-version }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-${{ matrix.node-version }}
${{ runner.os }}-v1-node-${{ matrix.node-version }}
- name: Setup Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1

View File

@ -1,4 +1,5 @@
import { createLocalClient } from '../connection';
import {subscribeForErrors} from "../../api";
describe('== AIR suite', () => {
it('check init_peer_id', async function () {
@ -59,7 +60,7 @@ describe('== AIR suite', () => {
it('call script without ttl', async function () {
// arrange
const client = await createLocalClient();
const script = `(call %init_peer_id% ("" "") [""])`;
const script = `(call %init_peer_id% ("op" "identity") [""])`;
// act
const promise = client.sendScript(script, undefined, 1);

View File

@ -147,12 +147,11 @@ export async function build(
timestamp: currentTime,
ttl: ttl,
script: script,
// TODO: sign particle
signature: '',
data: Buffer.from([]),
};
particle.signature = await signParticle(peerId, particle);
return particle;
}
@ -187,33 +186,6 @@ export function parseParticle(str: string): ParticleDto {
};
}
export function canonicalBytes(particle: ParticleDto) {
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.
*/
export async function signParticle(peerId: PeerId, particle: ParticleDto): Promise<string> {
let bufToSign = canonicalBytes(particle);
let signature = await peerId.privKey.sign(bufToSign);
return encode(signature);
}
export function genUUID() {
return uuidv4();
}