-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbytes.js
More file actions
68 lines (58 loc) · 1.8 KB
/
bytes.js
File metadata and controls
68 lines (58 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { builtins } from 'pythonia'
import { Atom } from '@urbit/nockjs'
// to run python functions (for byte handling):
async function pyExec (pythonCode, forceBigInt=false) {
let val
const returnToNode = (res) => val = forceBigInt ? BigInt(res) : res
await builtins.exec(pythonCode, { returnToNode })
return val
}
// str(js primative) => Atom
async function primitiveToAtom (s) {
return await pyExec(`returnToNode(str(int.from_bytes('''${s}'''.encode(), 'little')))`, true)
}
// Atom => str(js primative)
async function atomToPrimitive (atom) {
return await pyExec(`
def byte_len(i):
lyn = i.bit_length()
byt = lyn >> 3
return byt + 1 if lyn & 7 else byt
def intbytes(i):
return i.to_bytes(byte_len(i), 'little', signed=False)
returnToNode(str(intbytes(${atom.number}).decode()))
`)
}
// Buffer => Atom
async function newtDecode (buffer) {
const hexByteString = buffer.toString('hex')
return new Atom(await pyExec(`
byte_string = bytes.fromhex('''${hexByteString}''')
newt_decoded_byte_string = byte_string[5:]
raw_atom = int.from_bytes(newt_decoded_byte_string, 'little')
returnToNode(str(raw_atom))`, true))
}
// Atom => Buffer
async function newtEncode (jammedNoun) {
if ((typeof jammedNoun) != 'bigint') {
throw new Error(`${jammedNoun} is not of type BigInt`)
}
return Buffer.from(await pyExec(`
def byte_len(i: int):
lyn = i.bit_length()
byt = lyn >> 3
return byt + 1 if lyn & 7 else byt
def newt_encode(jammed_noun):
version = (0).to_bytes(1, 'little')
jammed_noun = jammed_noun.to_bytes(byte_len(jammed_noun), 'little', signed=False)
length = len(jammed_noun).to_bytes(4, 'little')
return version+length+jammed_noun
returnToNode(str(newt_encode(${jammedNoun}).hex()))
`), 'hex')
}
export {
primitiveToAtom,
atomToPrimitive,
newtDecode,
newtEncode,
}