* Wrapping scripts with xor

* Add function subscribeForErrors to handle errors
This commit is contained in:
Pavel
2021-02-25 18:36:10 +03:00
committed by GitHub
parent 619c8a6d6a
commit c65c0afbee
8 changed files with 122 additions and 24 deletions

View File

@ -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,