mirror of
https://github.com/fluencelabs/fluence-js.git
synced 2025-06-18 02:21:21 +00:00
Xor (#24)
* Wrapping scripts with xor * Add function subscribeForErrors to handle errors
This commit is contained in:
@ -19,6 +19,7 @@ import { fromByteArray, toByteArray } from 'base64-js';
|
||||
import PeerId from 'peer-id';
|
||||
import { encode } from 'bs58';
|
||||
import { injectDataIntoParticle } from './ParticleProcessor';
|
||||
import { PeerIdB58 } from './commonTypes';
|
||||
|
||||
const DEFAULT_TTL = 7000;
|
||||
|
||||
@ -91,8 +92,28 @@ function wrapWithVariableInjectionScript(script: string, fields: string[]): stri
|
||||
return script;
|
||||
}
|
||||
|
||||
function wrapWithXor(script: string): string {
|
||||
return `
|
||||
(xor
|
||||
${script}
|
||||
(seq
|
||||
(call __magic_relay ("op" "identity") [])
|
||||
(call %init_peer_id% ("__magic" "handle_xor") [%last_error%])
|
||||
)
|
||||
)`;
|
||||
}
|
||||
|
||||
function wrapWithXorLocal(script: string): string {
|
||||
return `
|
||||
(xor
|
||||
${script}
|
||||
(call %init_peer_id% ("__magic" "handle_xor") [%last_error%])
|
||||
)`;
|
||||
}
|
||||
|
||||
export async function build(
|
||||
peerId: PeerId,
|
||||
relay: PeerIdB58 | undefined,
|
||||
script: string,
|
||||
data?: Map<string, any>,
|
||||
ttl?: number,
|
||||
@ -109,8 +130,16 @@ export async function build(
|
||||
ttl = DEFAULT_TTL;
|
||||
}
|
||||
|
||||
if (relay) {
|
||||
data.set('__magic_relay', relay);
|
||||
}
|
||||
injectDataIntoParticle(id, data, ttl);
|
||||
script = wrapWithVariableInjectionScript(script, Array.from(data.keys()));
|
||||
if (relay) {
|
||||
script = wrapWithXor(script);
|
||||
} else {
|
||||
script = wrapWithXorLocal(script);
|
||||
}
|
||||
|
||||
let particle: ParticleDto = {
|
||||
id: id,
|
||||
|
Reference in New Issue
Block a user