-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
109 lines (95 loc) · 3.87 KB
/
index.ts
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import "allocator/tlsf";
import { Globals } from "./types"
import {
u32_high_bits,
u32_low_bits,
serialize,
deserialize,
check_encoded_allocation,
errorCodeToString,
free,
} from './utils';
export {
u32_high_bits,
u32_low_bits,
serialize,
deserialize,
check_encoded_allocation,
errorCodeToString,
free
} from './utils';
import { stringify } from './json';
export { stringify } from './json';
declare namespace env {
function hc_debug(encoded_allocation_of_input: u32): u32;
function hc_call(encoded_allocation_of_input: u32): u32;
function hc_sign(encoded_allocation_of_input: u32): u32;
function hc_verify_signature(encoded_allocation_of_input: u32): u32;
function hc_commit_entry(encoded_allocation_of_input: u32): u32;
function hc_update_entry(encoded_allocation_of_input: u32): u32;
function hc_update_agent(encoded_allocation_of_input: u32): u32;
function hc_remove_entry(encoded_allocation_of_input: u32): u32;
function hc_get_entry(encoded_allocation_of_input: u32): u32;
function hc_link_entries(encoded_allocation_of_input: u32): u32;
function hc_query(encoded_allocation_of_input: u32): u32;
function hc_send(encoded_allocation_of_input: u32): u32;
function hc_start_bundle(encoded_allocation_of_input: u32): u32;
function hc_close_bundle(encoded_allocation_of_input: u32): u32;
function hc_init_globals(encoded_allocation_of_input: u32): u32;
}
export const enum ErrorCode {
Success = 0,
Failure = 1,
ArgumentDeserializationFailed = 2,
OutOfMemory = 3,
ReceivedWrongActionResult = 4,
CallbackFailed = 5,
RecursiveCallForbidden = 6,
ResponseSerializationFailed = 7,
PageOverflowError = 8, // returned by hdk if offset+size exceeds a page
}
// handleSerialization
// creates a consistent pattern we can use for serializing the input into memory,
// calling the core API function (api_call) which is passed in,
// and then returning an error or the deserialized result
// @param input: string, the string value that api_call will pull from memory and use as an input
// @param api_call: function, the core API call, which should be wrapped in a small inline function because assemblyscript
// @returns string, the successful result of the call, or an error string
function handleSerialization(input: string, api_call: (e: u32) => u32): string {
let encoded_allocation: u32 = serialize(input);
let result: u32 = api_call(encoded_allocation);
let resultCode = check_encoded_allocation(result);
if(resultCode == ErrorCode.Success) {
return deserialize(result)
} else {
return errorCodeToString(resultCode)
}
}
export function debug<T>(message: T): void {
if(isString<T>(message)) {
handleSerialization(message, (e: u32): u32 => env.hc_debug(e));
} else {
handleSerialization(stringify(message), (e: u32): u32 => env.hc_debug(e));
}
}
export function commit_entry<T>(entryType: string, entryContent: T): string {
let entryContentString: String;
if(isString<T>(entryContent)) {
entryContentString = entryContent;
} else {
entryContentString = stringify(entryContent);
}
let jsonEncodedParams = `{"entry_type_name":"`+entryType+`","entry_content":"`+entryContentString+`"}`;
return handleSerialization(jsonEncodedParams, (e: u32): u32 => env.hc_commit_entry(e));
}
export function get_entry(hash: string): string {
let jsonEncodedParams: string = `{"address":"`+hash+`"}`;
return handleSerialization(jsonEncodedParams, (e: u32): u32 => env.hc_get_entry(e));
}
export function call(zome_name: string, cap_name: string, fn_name: string, fn_args: string): string {
let jsonEncodedParams: string = `{"zome_name":"`+zome_name+`", "cap_name":"`+cap_name+`", "fn_name":"`+fn_name+`", "fn_args":"`+fn_args+`"}`;
return handleSerialization(jsonEncodedParams, (e: u32): u32 => env.hc_call(e));
}
export function init_globals(): string {
return handleSerialization("", (e: u32): u32 => env.hc_init_globals(e));
}