-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathborsh.js
46 lines (39 loc) · 1.15 KB
/
borsh.js
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
const borsh = require("borsh");
class Assignable {
constructor(properties) {
Object.keys(properties).map((key) => {
this[key] = properties[key];
});
}
}
class StatusMessage extends Assignable {}
class Record extends Assignable {}
const schema = new Map([
[StatusMessage, { kind: "struct", fields: [["records", [Record]]] }],
[
Record,
{
kind: "struct",
fields: [
["k", "string"],
["v", "string"],
],
},
],
]);
const stateKey = "U1RBVEU=";
console.log(Buffer.from(stateKey, "base64"));
console.log(Buffer.from(stateKey, "base64").toString());
const stateValue =
"AgAAAA8AAABhbGljZS50ZXN0Lm5lYXIFAAAAaGVsbG8NAAAAYm9iLnRlc3QubmVhcgUAAAB3b3JsZA==";
const stateValueBuffer = Buffer.from(stateValue, "base64");
let statusMessage = borsh.deserialize(schema, StatusMessage, stateValueBuffer);
console.log(statusMessage);
console.log(
Buffer.from(borsh.serialize(schema, statusMessage)).toString("base64")
);
statusMessage.records.push(new Record({ k: "alice.near", v: "hello world" }));
console.log(statusMessage);
console.log(
Buffer.from(borsh.serialize(schema, statusMessage)).toString("base64")
);