forked from dfinity/ic-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile-idl-js
executable file
·90 lines (71 loc) · 3 KB
/
compile-idl-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
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
#!/usr/bin/env bash
# Compiles candid .did files to .idl.js and .d.ts
set -euo pipefail
if [ "$(didc --version)" != "didc 0.3.7" ]; then
{
echo "didc version 0.3.7 is required. To install it on Mac:"
echo "curl -Lf https://github.com/dfinity/candid/releases/download/2024-02-27/didc-macos -o install_didc"
echo "install -m 755 install_didc /$HOME/.local/bin/didc"
} >&2
exit 1
fi
did_files_to_compile_for_pkg() {
local pkg="$1"
local path=packages/${pkg}/candid
find ${path} -type f -name '*.did' |
grep -vE ${path}/+payloads.did
}
did_files_to_compile() {
did_files_to_compile_for_pkg "nns"
did_files_to_compile_for_pkg "sns"
did_files_to_compile_for_pkg "cmc"
did_files_to_compile_for_pkg "ledger-icrc"
did_files_to_compile_for_pkg "ledger-icp"
did_files_to_compile_for_pkg "ckbtc"
did_files_to_compile_for_pkg "cketh"
did_files_to_compile_for_pkg "ic-management"
}
# Normal API access.
compile_did() {
local didfile="$1"
local jsFactoryFile="$(echo "$didfile" | sed 's/did$/idl.js/g')"
local tsFactoryFile="$(echo "$didfile" | sed 's/did$/idl.d.ts/g')"
local tsfile="$(echo "$didfile" | sed 's/did$/d.ts/g')"
{
echo "/* Do not edit. Compiled with ./scripts/compile-idl-js from ${didfile} */"
didc bind -t js "${didfile}"
} | sed -E "s/^export default/export const idlFactory =/g" >"${jsFactoryFile}"
generate_did_factory_ts
didc bind -t ts "${didfile}" >"${tsfile}"
}
# The certified API makes use of the fact that update calls are always signed, so it calls query calls as updates.
# Note: This is inefficient if query calls are already certified, which is often but not always the case.
# Note: It is not clear which API calls have certified responses or how to check that a query response has a certificate.
compile_certified_did() {
local didfile="$1"
local certified_didfile="$(mktemp)"
local jsFactoryFile="$(echo "$didfile" | sed 's/did$/certified.idl.js/g')"
local tsFactoryFile="$(echo "$didfile" | sed 's/did$/certified.idl.d.ts/g')"
QUERY_ARG='query;'
sed "s/$QUERY_ARG/;/g" "$didfile" >"$certified_didfile"
{
echo "/* Do not edit. Compiled with ./scripts/compile-idl-js from ${didfile} */"
didc bind -t js "${certified_didfile}"
} | sed "s/^export default/export const idlFactory =/g" >"${jsFactoryFile}"
generate_did_factory_ts
rm "$certified_didfile"
}
generate_did_factory_ts() {
echo "import type { IDL } from \"@dfinity/candid\";export const idlFactory: IDL.InterfaceFactory;" >"${tsFactoryFile}"
}
# First generate default certified versions of normal did files.
# Where there are custom certified did files the normal compilation will override the default.
did_files_to_compile | grep -v certified | while read line; do
compile_certified_did "$line"
done
did_files_to_compile | while read line; do
compile_did "$line"
done
# didc might generate definition file with invalid trailing comma which can be fixed by the code formatter
# in addition, types we create with generate_did_factory_ts are not formatted
npm run format