mirror of
https://github.com/fluencelabs/fluence-js.git
synced 2025-06-12 23:51:21 +00:00
Do not use DataView (#28)
This commit is contained in:
@ -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();
|
||||
}
|
||||
|
Reference in New Issue
Block a user