@icp-sdk/core 5.4.0 → 6.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/agent/actor.d.ts +7 -3
- package/lib/cjs/agent/actor.d.ts.map +1 -1
- package/lib/cjs/agent/actor.js +18 -5
- package/lib/cjs/agent/actor.js.map +1 -1
- package/lib/cjs/agent/agent/api.d.ts +19 -6
- package/lib/cjs/agent/agent/api.d.ts.map +1 -1
- package/lib/cjs/agent/agent/api.js.map +1 -1
- package/lib/cjs/agent/agent/http/index.d.ts +8 -11
- package/lib/cjs/agent/agent/http/index.d.ts.map +1 -1
- package/lib/cjs/agent/agent/http/index.js +190 -166
- package/lib/cjs/agent/agent/http/index.js.map +1 -1
- package/lib/cjs/agent/auth.d.ts +6 -5
- package/lib/cjs/agent/auth.d.ts.map +1 -1
- package/lib/cjs/agent/auth.js +3 -3
- package/lib/cjs/agent/auth.js.map +1 -1
- package/lib/cjs/agent/canisterStatus/index.d.ts.map +1 -1
- package/lib/cjs/agent/canisterStatus/index.js +10 -10
- package/lib/cjs/agent/canisterStatus/index.js.map +1 -1
- package/lib/cjs/agent/cbor.d.ts +2 -1
- package/lib/cjs/agent/cbor.d.ts.map +1 -1
- package/lib/cjs/agent/cbor.js +4 -1
- package/lib/cjs/agent/cbor.js.map +1 -1
- package/lib/cjs/agent/certificate.d.ts +5 -4
- package/lib/cjs/agent/certificate.d.ts.map +1 -1
- package/lib/cjs/agent/certificate.js +14 -18
- package/lib/cjs/agent/certificate.js.map +1 -1
- package/lib/cjs/agent/der.d.ts +3 -2
- package/lib/cjs/agent/der.d.ts.map +1 -1
- package/lib/cjs/agent/der.js.map +1 -1
- package/lib/cjs/agent/errors.d.ts +7 -1
- package/lib/cjs/agent/errors.d.ts.map +1 -1
- package/lib/cjs/agent/errors.js +34 -16
- package/lib/cjs/agent/errors.js.map +1 -1
- package/lib/cjs/agent/polling/index.d.ts +3 -4
- package/lib/cjs/agent/polling/index.d.ts.map +1 -1
- package/lib/cjs/agent/polling/index.js +17 -10
- package/lib/cjs/agent/polling/index.js.map +1 -1
- package/lib/cjs/agent/polling/strategy.d.ts.map +1 -1
- package/lib/cjs/agent/polling/strategy.js +6 -6
- package/lib/cjs/agent/polling/strategy.js.map +1 -1
- package/lib/cjs/agent/polling/types.d.ts +3 -4
- package/lib/cjs/agent/polling/types.d.ts.map +1 -1
- package/lib/cjs/agent/public_key.d.ts +3 -2
- package/lib/cjs/agent/public_key.d.ts.map +1 -1
- package/lib/cjs/agent/public_key.js +2 -2
- package/lib/cjs/agent/public_key.js.map +1 -1
- package/lib/cjs/agent/request_id.js +10 -10
- package/lib/cjs/agent/request_id.js.map +1 -1
- package/lib/cjs/agent/subnetStatus/index.js +7 -7
- package/lib/cjs/agent/subnetStatus/index.js.map +1 -1
- package/lib/cjs/agent/utils/bls.d.ts.map +1 -1
- package/lib/cjs/agent/utils/bls.js +7 -6
- package/lib/cjs/agent/utils/bls.js.map +1 -1
- package/lib/cjs/agent/utils/readState.d.ts +2 -2
- package/lib/cjs/agent/utils/readState.d.ts.map +1 -1
- package/lib/cjs/agent/utils/readState.js +4 -4
- package/lib/cjs/agent/utils/readState.js.map +1 -1
- package/lib/cjs/agent/utils/target.d.ts +15 -0
- package/lib/cjs/agent/utils/target.d.ts.map +1 -0
- package/lib/cjs/agent/utils/target.js +27 -0
- package/lib/cjs/agent/utils/target.js.map +1 -0
- package/lib/cjs/candid/utils/buffer.d.ts +2 -1
- package/lib/cjs/candid/utils/buffer.d.ts.map +1 -1
- package/lib/cjs/candid/utils/buffer.js +5 -2
- package/lib/cjs/candid/utils/buffer.js.map +1 -1
- package/lib/cjs/identity/identity/delegation.d.ts +10 -3
- package/lib/cjs/identity/identity/delegation.d.ts.map +1 -1
- package/lib/cjs/identity/identity/delegation.js +25 -12
- package/lib/cjs/identity/identity/delegation.js.map +1 -1
- package/lib/cjs/identity/identity/ecdsa.d.ts +2 -2
- package/lib/cjs/identity/identity/ecdsa.d.ts.map +1 -1
- package/lib/cjs/identity/identity/ecdsa.js +3 -2
- package/lib/cjs/identity/identity/ecdsa.js.map +1 -1
- package/lib/cjs/identity/identity/ed25519.d.ts +3 -2
- package/lib/cjs/identity/identity/ed25519.d.ts.map +1 -1
- package/lib/cjs/identity/identity/ed25519.js +18 -15
- package/lib/cjs/identity/identity/ed25519.js.map +1 -1
- package/lib/cjs/identity/identity/webauthn.d.ts +4 -3
- package/lib/cjs/identity/identity/webauthn.d.ts.map +1 -1
- package/lib/cjs/identity/identity/webauthn.js +12 -11
- package/lib/cjs/identity/identity/webauthn.js.map +1 -1
- package/lib/cjs/identity/secp256k1/der.d.ts +2 -1
- package/lib/cjs/identity/secp256k1/der.d.ts.map +1 -1
- package/lib/cjs/identity/secp256k1/der.js.map +1 -1
- package/lib/cjs/identity/secp256k1/pem.d.ts +4 -3
- package/lib/cjs/identity/secp256k1/pem.d.ts.map +1 -1
- package/lib/cjs/identity/secp256k1/pem.js +2 -2
- package/lib/cjs/identity/secp256k1/pem.js.map +1 -1
- package/lib/cjs/identity/secp256k1/secp256k1.d.ts +5 -4
- package/lib/cjs/identity/secp256k1/secp256k1.d.ts.map +1 -1
- package/lib/cjs/identity/secp256k1/secp256k1.js +17 -20
- package/lib/cjs/identity/secp256k1/secp256k1.js.map +1 -1
- package/lib/cjs/principal/principal.js +5 -5
- package/lib/cjs/principal/principal.js.map +1 -1
- package/lib/esm/agent/actor.d.ts +7 -3
- package/lib/esm/agent/actor.d.ts.map +1 -1
- package/lib/esm/agent/actor.js +18 -5
- package/lib/esm/agent/actor.js.map +1 -1
- package/lib/esm/agent/agent/api.d.ts +19 -6
- package/lib/esm/agent/agent/api.d.ts.map +1 -1
- package/lib/esm/agent/agent/api.js.map +1 -1
- package/lib/esm/agent/agent/http/index.d.ts +8 -11
- package/lib/esm/agent/agent/http/index.d.ts.map +1 -1
- package/lib/esm/agent/agent/http/index.js +183 -159
- package/lib/esm/agent/agent/http/index.js.map +1 -1
- package/lib/esm/agent/auth.d.ts +6 -5
- package/lib/esm/agent/auth.d.ts.map +1 -1
- package/lib/esm/agent/auth.js +1 -1
- package/lib/esm/agent/auth.js.map +1 -1
- package/lib/esm/agent/canister-env/index.js +1 -1
- package/lib/esm/agent/canister-env/index.js.map +1 -1
- package/lib/esm/agent/canisterStatus/index.d.ts.map +1 -1
- package/lib/esm/agent/canisterStatus/index.js +2 -2
- package/lib/esm/agent/canisterStatus/index.js.map +1 -1
- package/lib/esm/agent/cbor.d.ts +2 -1
- package/lib/esm/agent/cbor.d.ts.map +1 -1
- package/lib/esm/agent/cbor.js +4 -1
- package/lib/esm/agent/cbor.js.map +1 -1
- package/lib/esm/agent/certificate.d.ts +5 -4
- package/lib/esm/agent/certificate.d.ts.map +1 -1
- package/lib/esm/agent/certificate.js +5 -9
- package/lib/esm/agent/certificate.js.map +1 -1
- package/lib/esm/agent/der.d.ts +3 -2
- package/lib/esm/agent/der.d.ts.map +1 -1
- package/lib/esm/agent/der.js.map +1 -1
- package/lib/esm/agent/errors.d.ts +7 -1
- package/lib/esm/agent/errors.d.ts.map +1 -1
- package/lib/esm/agent/errors.js +18 -1
- package/lib/esm/agent/errors.js.map +1 -1
- package/lib/esm/agent/polling/index.d.ts +3 -4
- package/lib/esm/agent/polling/index.d.ts.map +1 -1
- package/lib/esm/agent/polling/index.js +15 -8
- package/lib/esm/agent/polling/index.js.map +1 -1
- package/lib/esm/agent/polling/strategy.d.ts.map +1 -1
- package/lib/esm/agent/polling/strategy.js +6 -6
- package/lib/esm/agent/polling/strategy.js.map +1 -1
- package/lib/esm/agent/polling/types.d.ts +3 -4
- package/lib/esm/agent/polling/types.d.ts.map +1 -1
- package/lib/esm/agent/public_key.d.ts +3 -2
- package/lib/esm/agent/public_key.d.ts.map +1 -1
- package/lib/esm/agent/public_key.js +2 -2
- package/lib/esm/agent/public_key.js.map +1 -1
- package/lib/esm/agent/request_id.js +2 -2
- package/lib/esm/agent/request_id.js.map +1 -1
- package/lib/esm/agent/subnetStatus/index.js +1 -1
- package/lib/esm/agent/subnetStatus/index.js.map +1 -1
- package/lib/esm/agent/utils/bls.d.ts.map +1 -1
- package/lib/esm/agent/utils/bls.js +7 -6
- package/lib/esm/agent/utils/bls.js.map +1 -1
- package/lib/esm/agent/utils/readState.d.ts +2 -2
- package/lib/esm/agent/utils/readState.d.ts.map +1 -1
- package/lib/esm/agent/utils/readState.js +1 -1
- package/lib/esm/agent/utils/readState.js.map +1 -1
- package/lib/esm/agent/utils/target.d.ts +15 -0
- package/lib/esm/agent/utils/target.d.ts.map +1 -0
- package/lib/esm/agent/utils/target.js +23 -0
- package/lib/esm/agent/utils/target.js.map +1 -0
- package/lib/esm/candid/utils/buffer.d.ts +2 -1
- package/lib/esm/candid/utils/buffer.d.ts.map +1 -1
- package/lib/esm/candid/utils/buffer.js +5 -2
- package/lib/esm/candid/utils/buffer.js.map +1 -1
- package/lib/esm/identity/identity/delegation.d.ts +10 -3
- package/lib/esm/identity/identity/delegation.d.ts.map +1 -1
- package/lib/esm/identity/identity/delegation.js +22 -9
- package/lib/esm/identity/identity/delegation.js.map +1 -1
- package/lib/esm/identity/identity/ecdsa.d.ts +2 -2
- package/lib/esm/identity/identity/ecdsa.d.ts.map +1 -1
- package/lib/esm/identity/identity/ecdsa.js +3 -2
- package/lib/esm/identity/identity/ecdsa.js.map +1 -1
- package/lib/esm/identity/identity/ed25519.d.ts +3 -2
- package/lib/esm/identity/identity/ed25519.d.ts.map +1 -1
- package/lib/esm/identity/identity/ed25519.js +10 -7
- package/lib/esm/identity/identity/ed25519.js.map +1 -1
- package/lib/esm/identity/identity/webauthn.d.ts +4 -3
- package/lib/esm/identity/identity/webauthn.d.ts.map +1 -1
- package/lib/esm/identity/identity/webauthn.js +8 -7
- package/lib/esm/identity/identity/webauthn.js.map +1 -1
- package/lib/esm/identity/secp256k1/der.d.ts +2 -1
- package/lib/esm/identity/secp256k1/der.d.ts.map +1 -1
- package/lib/esm/identity/secp256k1/der.js.map +1 -1
- package/lib/esm/identity/secp256k1/pem.d.ts +4 -3
- package/lib/esm/identity/secp256k1/pem.d.ts.map +1 -1
- package/lib/esm/identity/secp256k1/pem.js +2 -2
- package/lib/esm/identity/secp256k1/pem.js.map +1 -1
- package/lib/esm/identity/secp256k1/secp256k1.d.ts +5 -4
- package/lib/esm/identity/secp256k1/secp256k1.d.ts.map +1 -1
- package/lib/esm/identity/secp256k1/secp256k1.js +10 -13
- package/lib/esm/identity/secp256k1/secp256k1.js.map +1 -1
- package/lib/esm/principal/principal.js +2 -2
- package/lib/esm/principal/principal.js.map +1 -1
- package/package.json +20 -10
- package/src/agent/actor.ts +0 -524
- package/src/agent/agent/api.ts +0 -310
- package/src/agent/agent/http/expiry.ts +0 -107
- package/src/agent/agent/http/index.ts +0 -1724
- package/src/agent/agent/http/transforms.ts +0 -54
- package/src/agent/agent/http/types.ts +0 -145
- package/src/agent/agent/index.ts +0 -4
- package/src/agent/auth.ts +0 -147
- package/src/agent/canister-env/index.ts +0 -192
- package/src/agent/canisterStatus/index.ts +0 -260
- package/src/agent/cbor.ts +0 -63
- package/src/agent/certificate.ts +0 -1058
- package/src/agent/constants.ts +0 -24
- package/src/agent/der.ts +0 -191
- package/src/agent/errors.ts +0 -962
- package/src/agent/fetch_candid.ts +0 -37
- package/src/agent/index.ts +0 -130
- package/src/agent/observable.ts +0 -49
- package/src/agent/polling/backoff.ts +0 -129
- package/src/agent/polling/index.ts +0 -238
- package/src/agent/polling/strategy.ts +0 -140
- package/src/agent/polling/types.ts +0 -28
- package/src/agent/public_key.ts +0 -65
- package/src/agent/request_id.ts +0 -99
- package/src/agent/subnetStatus/index.ts +0 -263
- package/src/agent/utils/bls.ts +0 -18
- package/src/agent/utils/buffer.ts +0 -64
- package/src/agent/utils/certificateReject.ts +0 -25
- package/src/agent/utils/createExpirableStore.ts +0 -31
- package/src/agent/utils/expirableStore.ts +0 -47
- package/src/agent/utils/inMemoryExpirableStore.ts +0 -49
- package/src/agent/utils/indexedDBExpirableStore.ts +0 -191
- package/src/agent/utils/leb.ts +0 -14
- package/src/agent/utils/random.ts +0 -30
- package/src/agent/utils/readState.ts +0 -180
- package/src/candid/candid-core.ts +0 -274
- package/src/candid/candid-ui.ts +0 -281
- package/src/candid/idl.ts +0 -2654
- package/src/candid/index.ts +0 -35
- package/src/candid/types.ts +0 -7
- package/src/candid/utils/bigint-math.ts +0 -26
- package/src/candid/utils/buffer.ts +0 -221
- package/src/candid/utils/hash.ts +0 -31
- package/src/candid/utils/leb128.ts +0 -233
- package/src/identity/identity/attributes.ts +0 -91
- package/src/identity/identity/delegation.ts +0 -411
- package/src/identity/identity/ecdsa.ts +0 -162
- package/src/identity/identity/ed25519.ts +0 -246
- package/src/identity/identity/partial.ts +0 -60
- package/src/identity/identity/webauthn.ts +0 -265
- package/src/identity/index.ts +0 -44
- package/src/identity/secp256k1/der.ts +0 -177
- package/src/identity/secp256k1/index.ts +0 -52
- package/src/identity/secp256k1/pem.ts +0 -39
- package/src/identity/secp256k1/secp256k1.ts +0 -276
- package/src/index.ts +0 -3
- package/src/principal/index.ts +0 -19
- package/src/principal/principal.ts +0 -181
- package/src/principal/utils/base32.ts +0 -101
- package/src/principal/utils/getCrc.ts +0 -52
|
@@ -1,274 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-this-alias */
|
|
2
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
3
|
-
import type * as IDL from './idl.ts';
|
|
4
|
-
|
|
5
|
-
export interface ParseConfig {
|
|
6
|
-
random?: boolean;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export interface UIConfig {
|
|
10
|
-
input?: HTMLElement;
|
|
11
|
-
form?: InputForm;
|
|
12
|
-
parse(t: IDL.Type, config: ParseConfig, v: string): any;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export interface FormConfig {
|
|
16
|
-
open?: HTMLElement;
|
|
17
|
-
event?: string;
|
|
18
|
-
labelMap?: Record<string, string>;
|
|
19
|
-
container?: HTMLElement;
|
|
20
|
-
render(t: IDL.Type): InputBox;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export class InputBox {
|
|
24
|
-
public status: HTMLElement;
|
|
25
|
-
public label: string | null = null;
|
|
26
|
-
public value: any = undefined;
|
|
27
|
-
|
|
28
|
-
constructor(
|
|
29
|
-
public idl: IDL.Type,
|
|
30
|
-
public ui: UIConfig,
|
|
31
|
-
) {
|
|
32
|
-
const status = document.createElement('span');
|
|
33
|
-
status.className = 'status';
|
|
34
|
-
this.status = status;
|
|
35
|
-
|
|
36
|
-
if (ui.input) {
|
|
37
|
-
ui.input.addEventListener('blur', () => {
|
|
38
|
-
if ((ui.input as HTMLInputElement).value === '') {
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
|
-
this.parse();
|
|
42
|
-
});
|
|
43
|
-
ui.input.addEventListener('input', () => {
|
|
44
|
-
status.style.display = 'none';
|
|
45
|
-
ui.input!.classList.remove('reject');
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
public isRejected(): boolean {
|
|
50
|
-
return this.value === undefined;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
public parse(config: ParseConfig = {}): any {
|
|
54
|
-
if (this.ui.form) {
|
|
55
|
-
const value = this.ui.form.parse(config);
|
|
56
|
-
this.value = value;
|
|
57
|
-
return value;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
if (this.ui.input) {
|
|
61
|
-
const input = this.ui.input as HTMLInputElement;
|
|
62
|
-
try {
|
|
63
|
-
const value = this.ui.parse(this.idl, config, input.value);
|
|
64
|
-
if (!this.idl.covariant(value)) {
|
|
65
|
-
throw new Error(`${input.value} is not of type ${this.idl.display()}`);
|
|
66
|
-
}
|
|
67
|
-
this.status.style.display = 'none';
|
|
68
|
-
this.value = value;
|
|
69
|
-
return value;
|
|
70
|
-
} catch (err) {
|
|
71
|
-
input.classList.add('reject');
|
|
72
|
-
this.status.style.display = 'block';
|
|
73
|
-
this.status.innerHTML = `InputError: ${(err as Error).message}`;
|
|
74
|
-
this.value = undefined;
|
|
75
|
-
return undefined;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
return null;
|
|
79
|
-
}
|
|
80
|
-
public render(dom: HTMLElement): void {
|
|
81
|
-
const container = document.createElement('span');
|
|
82
|
-
if (this.label) {
|
|
83
|
-
const label = document.createElement('label');
|
|
84
|
-
label.innerText = this.label;
|
|
85
|
-
container.appendChild(label);
|
|
86
|
-
}
|
|
87
|
-
if (this.ui.input) {
|
|
88
|
-
container.appendChild(this.ui.input);
|
|
89
|
-
container.appendChild(this.status);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
if (this.ui.form) {
|
|
93
|
-
this.ui.form.render(container);
|
|
94
|
-
}
|
|
95
|
-
dom.appendChild(container);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
export abstract class InputForm {
|
|
100
|
-
public form: InputBox[] = [];
|
|
101
|
-
constructor(public ui: FormConfig) {}
|
|
102
|
-
|
|
103
|
-
public abstract parse(config: ParseConfig): any;
|
|
104
|
-
public abstract generateForm(): any;
|
|
105
|
-
public renderForm(dom: HTMLElement): void {
|
|
106
|
-
if (this.ui.container) {
|
|
107
|
-
this.form.forEach(e => e.render(this.ui.container!));
|
|
108
|
-
dom.appendChild(this.ui.container);
|
|
109
|
-
} else {
|
|
110
|
-
this.form.forEach(e => e.render(dom));
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
public render(dom: HTMLElement): void {
|
|
114
|
-
if (this.ui.open && this.ui.event) {
|
|
115
|
-
dom.appendChild(this.ui.open);
|
|
116
|
-
const form = this;
|
|
117
|
-
|
|
118
|
-
form.ui.open!.addEventListener(form.ui.event!, () => {
|
|
119
|
-
// Remove old form
|
|
120
|
-
if (form.ui.container) {
|
|
121
|
-
form.ui.container.innerHTML = '';
|
|
122
|
-
} else {
|
|
123
|
-
const oldContainer = form.ui.open!.nextElementSibling;
|
|
124
|
-
if (oldContainer) {
|
|
125
|
-
oldContainer.parentNode!.removeChild(oldContainer);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
// Render form
|
|
129
|
-
form.generateForm();
|
|
130
|
-
form.renderForm(dom);
|
|
131
|
-
});
|
|
132
|
-
} else {
|
|
133
|
-
this.generateForm();
|
|
134
|
-
this.renderForm(dom);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
export class RecordForm extends InputForm {
|
|
140
|
-
constructor(
|
|
141
|
-
public fields: Array<[string, IDL.Type]>,
|
|
142
|
-
public ui: FormConfig,
|
|
143
|
-
) {
|
|
144
|
-
super(ui);
|
|
145
|
-
}
|
|
146
|
-
public generateForm(): void {
|
|
147
|
-
this.form = this.fields.map(([key, type]) => {
|
|
148
|
-
const input = this.ui.render(type);
|
|
149
|
-
// eslint-disable-next-line
|
|
150
|
-
if (this.ui.labelMap && this.ui.labelMap.hasOwnProperty(key)) {
|
|
151
|
-
input.label = `${this.ui.labelMap[key]} `;
|
|
152
|
-
} else {
|
|
153
|
-
input.label = `${key} `;
|
|
154
|
-
}
|
|
155
|
-
return input;
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
public parse(config: ParseConfig): Record<string, any> | undefined {
|
|
159
|
-
const v: Record<string, any> = {};
|
|
160
|
-
this.fields.forEach(([key, _], i) => {
|
|
161
|
-
const value = this.form[i].parse(config);
|
|
162
|
-
v[key] = value;
|
|
163
|
-
});
|
|
164
|
-
if (this.form.some(input => input.isRejected())) {
|
|
165
|
-
return undefined;
|
|
166
|
-
}
|
|
167
|
-
return v;
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
export class TupleForm extends InputForm {
|
|
172
|
-
constructor(
|
|
173
|
-
public components: IDL.Type[],
|
|
174
|
-
public ui: FormConfig,
|
|
175
|
-
) {
|
|
176
|
-
super(ui);
|
|
177
|
-
}
|
|
178
|
-
public generateForm(): void {
|
|
179
|
-
this.form = this.components.map(type => {
|
|
180
|
-
const input = this.ui.render(type);
|
|
181
|
-
return input;
|
|
182
|
-
});
|
|
183
|
-
}
|
|
184
|
-
public parse(config: ParseConfig): any[] | undefined {
|
|
185
|
-
const v: any[] = [];
|
|
186
|
-
this.components.forEach((_, i) => {
|
|
187
|
-
const value = this.form[i].parse(config);
|
|
188
|
-
v.push(value);
|
|
189
|
-
});
|
|
190
|
-
if (this.form.some(input => input.isRejected())) {
|
|
191
|
-
return undefined;
|
|
192
|
-
}
|
|
193
|
-
return v;
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
export class VariantForm extends InputForm {
|
|
198
|
-
constructor(
|
|
199
|
-
public fields: Array<[string, IDL.Type]>,
|
|
200
|
-
public ui: FormConfig,
|
|
201
|
-
) {
|
|
202
|
-
super(ui);
|
|
203
|
-
}
|
|
204
|
-
public generateForm(): void {
|
|
205
|
-
const index = (this.ui.open as HTMLSelectElement).selectedIndex;
|
|
206
|
-
const [_, type] = this.fields[index];
|
|
207
|
-
const variant = this.ui.render(type);
|
|
208
|
-
this.form = [variant];
|
|
209
|
-
}
|
|
210
|
-
public parse(config: ParseConfig): Record<string, any> | undefined {
|
|
211
|
-
const select = this.ui.open as HTMLSelectElement;
|
|
212
|
-
const selected = select.options[select.selectedIndex].value;
|
|
213
|
-
const value = this.form[0].parse(config);
|
|
214
|
-
if (value === undefined) {
|
|
215
|
-
return undefined;
|
|
216
|
-
}
|
|
217
|
-
const v: Record<string, any> = {};
|
|
218
|
-
v[selected] = value;
|
|
219
|
-
return v;
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
export class OptionForm extends InputForm {
|
|
224
|
-
constructor(
|
|
225
|
-
public ty: IDL.Type,
|
|
226
|
-
public ui: FormConfig,
|
|
227
|
-
) {
|
|
228
|
-
super(ui);
|
|
229
|
-
}
|
|
230
|
-
public generateForm(): void {
|
|
231
|
-
if ((this.ui.open as HTMLInputElement).checked) {
|
|
232
|
-
const opt = this.ui.render(this.ty);
|
|
233
|
-
this.form = [opt];
|
|
234
|
-
} else {
|
|
235
|
-
this.form = [];
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
public parse<T>(config: ParseConfig): [T] | [] | undefined {
|
|
239
|
-
if (this.form.length === 0) {
|
|
240
|
-
return [];
|
|
241
|
-
}
|
|
242
|
-
const value = this.form[0].parse(config);
|
|
243
|
-
if (value === undefined) {
|
|
244
|
-
return undefined;
|
|
245
|
-
}
|
|
246
|
-
return [value];
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
export class VecForm extends InputForm {
|
|
251
|
-
constructor(
|
|
252
|
-
public ty: IDL.Type,
|
|
253
|
-
public ui: FormConfig,
|
|
254
|
-
) {
|
|
255
|
-
super(ui);
|
|
256
|
-
}
|
|
257
|
-
public generateForm(): void {
|
|
258
|
-
const len = +(this.ui.open as HTMLInputElement).value;
|
|
259
|
-
this.form = [];
|
|
260
|
-
for (let i = 0; i < len; i++) {
|
|
261
|
-
const t = this.ui.render(this.ty);
|
|
262
|
-
this.form.push(t);
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
public parse<T>(config: ParseConfig): T[] | undefined {
|
|
266
|
-
const value = this.form.map(input => {
|
|
267
|
-
return input.parse(config);
|
|
268
|
-
});
|
|
269
|
-
if (this.form.some(input => input.isRejected())) {
|
|
270
|
-
return undefined;
|
|
271
|
-
}
|
|
272
|
-
return value;
|
|
273
|
-
}
|
|
274
|
-
}
|
package/src/candid/candid-ui.ts
DELETED
|
@@ -1,281 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
|
|
3
|
-
import * as IDL from './idl.ts';
|
|
4
|
-
import { Principal } from '#principal';
|
|
5
|
-
import * as UI from './candid-core.ts';
|
|
6
|
-
|
|
7
|
-
type InputBox = UI.InputBox;
|
|
8
|
-
|
|
9
|
-
const InputConfig: UI.UIConfig = { parse: parsePrimitive };
|
|
10
|
-
const FormConfig: UI.FormConfig = { render: renderInput };
|
|
11
|
-
|
|
12
|
-
export const inputBox = (t: IDL.Type, config: Partial<UI.UIConfig>) => {
|
|
13
|
-
return new UI.InputBox(t, { ...InputConfig, ...config });
|
|
14
|
-
};
|
|
15
|
-
export const recordForm = (fields: Array<[string, IDL.Type]>, config: Partial<UI.FormConfig>) => {
|
|
16
|
-
return new UI.RecordForm(fields, { ...FormConfig, ...config });
|
|
17
|
-
};
|
|
18
|
-
export const tupleForm = (components: IDL.Type[], config: Partial<UI.FormConfig>) => {
|
|
19
|
-
return new UI.TupleForm(components, { ...FormConfig, ...config });
|
|
20
|
-
};
|
|
21
|
-
export const variantForm = (fields: Array<[string, IDL.Type]>, config: Partial<UI.FormConfig>) => {
|
|
22
|
-
return new UI.VariantForm(fields, { ...FormConfig, ...config });
|
|
23
|
-
};
|
|
24
|
-
export const optForm = (ty: IDL.Type, config: Partial<UI.FormConfig>) => {
|
|
25
|
-
return new UI.OptionForm(ty, { ...FormConfig, ...config });
|
|
26
|
-
};
|
|
27
|
-
export const vecForm = (ty: IDL.Type, config: Partial<UI.FormConfig>) => {
|
|
28
|
-
return new UI.VecForm(ty, { ...FormConfig, ...config });
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
export class Render extends IDL.Visitor<null, InputBox> {
|
|
32
|
-
public visitType<T>(t: IDL.Type<T>, _d: null): InputBox {
|
|
33
|
-
const input = document.createElement('input');
|
|
34
|
-
input.classList.add('argument');
|
|
35
|
-
input.placeholder = t.display();
|
|
36
|
-
return inputBox(t, { input });
|
|
37
|
-
}
|
|
38
|
-
public visitNull(t: IDL.NullClass, _d: null): InputBox {
|
|
39
|
-
return inputBox(t, {});
|
|
40
|
-
}
|
|
41
|
-
public visitRecord(t: IDL.RecordClass, fields: Array<[string, IDL.Type]>, _d: null): InputBox {
|
|
42
|
-
let config = {};
|
|
43
|
-
if (fields.length > 1) {
|
|
44
|
-
const container = document.createElement('div');
|
|
45
|
-
container.classList.add('popup-form');
|
|
46
|
-
config = { container };
|
|
47
|
-
}
|
|
48
|
-
const form = recordForm(fields, config);
|
|
49
|
-
return inputBox(t as IDL.Type<any>, { form });
|
|
50
|
-
}
|
|
51
|
-
public visitTuple<T extends any[]>(
|
|
52
|
-
t: IDL.TupleClass<T>,
|
|
53
|
-
components: IDL.Type[],
|
|
54
|
-
_d: null,
|
|
55
|
-
): InputBox {
|
|
56
|
-
let config = {};
|
|
57
|
-
if (components.length > 1) {
|
|
58
|
-
const container = document.createElement('div');
|
|
59
|
-
container.classList.add('popup-form');
|
|
60
|
-
config = { container };
|
|
61
|
-
}
|
|
62
|
-
const form = tupleForm(components, config);
|
|
63
|
-
return inputBox(t as IDL.Type<any>, { form });
|
|
64
|
-
}
|
|
65
|
-
public visitVariant(t: IDL.VariantClass, fields: Array<[string, IDL.Type]>, _d: null): InputBox {
|
|
66
|
-
const select = document.createElement('select');
|
|
67
|
-
for (const [key, _type] of fields) {
|
|
68
|
-
const option = new Option(key);
|
|
69
|
-
select.add(option);
|
|
70
|
-
}
|
|
71
|
-
select.selectedIndex = -1;
|
|
72
|
-
select.classList.add('open');
|
|
73
|
-
const config: Partial<UI.FormConfig> = { open: select, event: 'change' };
|
|
74
|
-
const form = variantForm(fields, config);
|
|
75
|
-
return inputBox(t as IDL.Type<any>, { form });
|
|
76
|
-
}
|
|
77
|
-
public visitOpt<T>(t: IDL.OptClass<T>, ty: IDL.Type<T>, _d: null): InputBox {
|
|
78
|
-
const checkbox = document.createElement('input');
|
|
79
|
-
checkbox.type = 'checkbox';
|
|
80
|
-
checkbox.classList.add('open');
|
|
81
|
-
const form = optForm(ty, { open: checkbox, event: 'change' });
|
|
82
|
-
return inputBox(t as IDL.Type<any>, { form });
|
|
83
|
-
}
|
|
84
|
-
public visitVec<T>(t: IDL.VecClass<T>, ty: IDL.Type<T>, _d: null): InputBox {
|
|
85
|
-
const len = document.createElement('input');
|
|
86
|
-
len.type = 'number';
|
|
87
|
-
len.min = '0';
|
|
88
|
-
len.max = '100';
|
|
89
|
-
len.style.width = '8rem';
|
|
90
|
-
len.placeholder = 'len';
|
|
91
|
-
len.classList.add('open');
|
|
92
|
-
const container = document.createElement('div');
|
|
93
|
-
container.classList.add('popup-form');
|
|
94
|
-
const form = vecForm(ty, { open: len, event: 'change', container });
|
|
95
|
-
return inputBox(t, { form });
|
|
96
|
-
}
|
|
97
|
-
public visitRec<T>(_t: IDL.RecClass<T>, ty: IDL.ConstructType<T>, _d: null): InputBox {
|
|
98
|
-
return renderInput(ty);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
class Parse extends IDL.Visitor<string, any> {
|
|
103
|
-
public visitNull(_t: IDL.NullClass, _v: string): null {
|
|
104
|
-
return null;
|
|
105
|
-
}
|
|
106
|
-
public visitBool(_t: IDL.BoolClass, v: string): boolean {
|
|
107
|
-
if (v === 'true') {
|
|
108
|
-
return true;
|
|
109
|
-
}
|
|
110
|
-
if (v === 'false') {
|
|
111
|
-
return false;
|
|
112
|
-
}
|
|
113
|
-
throw new Error(`Cannot parse ${v} as boolean`);
|
|
114
|
-
}
|
|
115
|
-
public visitText(_t: IDL.TextClass, v: string): string {
|
|
116
|
-
return v;
|
|
117
|
-
}
|
|
118
|
-
public visitFloat(_t: IDL.FloatClass, v: string): number {
|
|
119
|
-
return parseFloat(v);
|
|
120
|
-
}
|
|
121
|
-
public visitFixedInt(t: IDL.FixedIntClass, v: string): number | bigint {
|
|
122
|
-
if (t._bits <= 32) {
|
|
123
|
-
return parseInt(v, 10);
|
|
124
|
-
}
|
|
125
|
-
return BigInt(v);
|
|
126
|
-
}
|
|
127
|
-
public visitFixedNat(t: IDL.FixedNatClass, v: string): number | bigint {
|
|
128
|
-
if (t._bits <= 32) {
|
|
129
|
-
return parseInt(v, 10);
|
|
130
|
-
}
|
|
131
|
-
return BigInt(v);
|
|
132
|
-
}
|
|
133
|
-
public visitNumber(_t: IDL.PrimitiveType, v: string): bigint {
|
|
134
|
-
return BigInt(v);
|
|
135
|
-
}
|
|
136
|
-
public visitPrincipal(_t: IDL.PrincipalClass, v: string): Principal {
|
|
137
|
-
return Principal.fromText(v);
|
|
138
|
-
}
|
|
139
|
-
public visitService(_t: IDL.ServiceClass, v: string): Principal {
|
|
140
|
-
return Principal.fromText(v);
|
|
141
|
-
}
|
|
142
|
-
public visitFunc(_t: IDL.FuncClass, v: string): [Principal, string] {
|
|
143
|
-
const x = v.split('.', 2);
|
|
144
|
-
return [Principal.fromText(x[0]), x[1]];
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
class Random extends IDL.Visitor<string, any> {
|
|
149
|
-
public visitNull(_t: IDL.NullClass, _v: string): null {
|
|
150
|
-
return null;
|
|
151
|
-
}
|
|
152
|
-
public visitBool(_t: IDL.BoolClass, _v: string): boolean {
|
|
153
|
-
return Math.random() < 0.5;
|
|
154
|
-
}
|
|
155
|
-
public visitText(_t: IDL.TextClass, _v: string): string {
|
|
156
|
-
return Math.random().toString(36).substring(6);
|
|
157
|
-
}
|
|
158
|
-
public visitFloat(_t: IDL.FloatClass, _v: string): number {
|
|
159
|
-
return Math.random();
|
|
160
|
-
}
|
|
161
|
-
public visitInt(_t: IDL.IntClass, _v: string): bigint {
|
|
162
|
-
return BigInt(this.generateNumber(true));
|
|
163
|
-
}
|
|
164
|
-
public visitNat(_t: IDL.NatClass, _v: string): bigint {
|
|
165
|
-
return BigInt(this.generateNumber(false));
|
|
166
|
-
}
|
|
167
|
-
public visitFixedInt(t: IDL.FixedIntClass, v: string): number | bigint {
|
|
168
|
-
const x = this.generateNumber(true);
|
|
169
|
-
if (t._bits <= 32) {
|
|
170
|
-
return x;
|
|
171
|
-
}
|
|
172
|
-
return BigInt(v);
|
|
173
|
-
}
|
|
174
|
-
public visitFixedNat(t: IDL.FixedNatClass, v: string): number | bigint {
|
|
175
|
-
const x = this.generateNumber(false);
|
|
176
|
-
if (t._bits <= 32) {
|
|
177
|
-
return x;
|
|
178
|
-
}
|
|
179
|
-
return BigInt(v);
|
|
180
|
-
}
|
|
181
|
-
private generateNumber(signed: boolean): number {
|
|
182
|
-
const num = Math.floor(Math.random() * 100);
|
|
183
|
-
if (signed && Math.random() < 0.5) {
|
|
184
|
-
return -num;
|
|
185
|
-
}
|
|
186
|
-
return num;
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
function parsePrimitive(t: IDL.Type, config: UI.ParseConfig, d: string) {
|
|
191
|
-
if (config.random && d === '') {
|
|
192
|
-
return t.accept(new Random(), d);
|
|
193
|
-
}
|
|
194
|
-
return t.accept(new Parse(), d);
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
/**
|
|
198
|
-
*
|
|
199
|
-
* @param t an IDL type
|
|
200
|
-
* @returns an input for that type
|
|
201
|
-
*/
|
|
202
|
-
export function renderInput(t: IDL.Type): InputBox {
|
|
203
|
-
return t.accept(new Render(), null);
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
interface ValueConfig {
|
|
207
|
-
input: InputBox;
|
|
208
|
-
value: any;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
*
|
|
213
|
-
* @param t an IDL Type
|
|
214
|
-
* @param input an InputBox
|
|
215
|
-
* @param value any
|
|
216
|
-
* @returns rendering that value to the provided input
|
|
217
|
-
*/
|
|
218
|
-
export function renderValue(t: IDL.Type, input: InputBox, value: any) {
|
|
219
|
-
return t.accept(new RenderValue(), { input, value });
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
class RenderValue extends IDL.Visitor<ValueConfig, void> {
|
|
223
|
-
public visitType<T>(t: IDL.Type<T>, d: ValueConfig) {
|
|
224
|
-
(d.input.ui.input as HTMLInputElement).value = t.valueToString(d.value);
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
public visitNull(_t: IDL.NullClass, _d: ValueConfig) {}
|
|
228
|
-
public visitText(_t: IDL.TextClass, d: ValueConfig) {
|
|
229
|
-
(d.input.ui.input as HTMLInputElement).value = d.value;
|
|
230
|
-
}
|
|
231
|
-
public visitRec<T>(_t: IDL.RecClass<T>, ty: IDL.ConstructType<T>, d: ValueConfig): void {
|
|
232
|
-
renderValue(ty, d.input, d.value);
|
|
233
|
-
}
|
|
234
|
-
public visitOpt<T>(_t: IDL.OptClass<T>, ty: IDL.Type<T>, d: ValueConfig): void {
|
|
235
|
-
if (d.value.length !== 0) {
|
|
236
|
-
const form = d.input.ui.form!;
|
|
237
|
-
const open = form.ui.open as HTMLInputElement;
|
|
238
|
-
open.checked = true;
|
|
239
|
-
open.dispatchEvent(new Event(form.ui.event!));
|
|
240
|
-
renderValue(ty, form.form[0], d.value[0]);
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
public visitRecord(_t: IDL.RecordClass, fields: Array<[string, IDL.Type]>, d: ValueConfig) {
|
|
244
|
-
const form = d.input.ui.form!;
|
|
245
|
-
fields.forEach(([key, type], i) => {
|
|
246
|
-
renderValue(type, form.form[i], d.value[key]);
|
|
247
|
-
});
|
|
248
|
-
}
|
|
249
|
-
public visitTuple<T extends any[]>(
|
|
250
|
-
_t: IDL.TupleClass<T>,
|
|
251
|
-
components: IDL.Type[],
|
|
252
|
-
d: ValueConfig,
|
|
253
|
-
) {
|
|
254
|
-
const form = d.input.ui.form!;
|
|
255
|
-
components.forEach((type, i) => {
|
|
256
|
-
renderValue(type, form.form[i], d.value[i]);
|
|
257
|
-
});
|
|
258
|
-
}
|
|
259
|
-
public visitVariant(_t: IDL.VariantClass, fields: Array<[string, IDL.Type]>, d: ValueConfig) {
|
|
260
|
-
const form = d.input.ui.form!;
|
|
261
|
-
const selected = Object.entries(d.value)[0];
|
|
262
|
-
fields.forEach(([key, type], i) => {
|
|
263
|
-
if (key === selected[0]) {
|
|
264
|
-
const open = form.ui.open as HTMLSelectElement;
|
|
265
|
-
open.selectedIndex = i;
|
|
266
|
-
open.dispatchEvent(new Event(form.ui.event!));
|
|
267
|
-
renderValue(type, form.form[0], selected[1]);
|
|
268
|
-
}
|
|
269
|
-
});
|
|
270
|
-
}
|
|
271
|
-
public visitVec<T>(_t: IDL.VecClass<T>, ty: IDL.Type<T>, d: ValueConfig) {
|
|
272
|
-
const form = d.input.ui.form!;
|
|
273
|
-
const len = d.value.length;
|
|
274
|
-
const open = form.ui.open as HTMLInputElement;
|
|
275
|
-
open.value = len;
|
|
276
|
-
open.dispatchEvent(new Event(form.ui.event!));
|
|
277
|
-
d.value.forEach((v: T, i: number) => {
|
|
278
|
-
renderValue(ty, form.form[i], v);
|
|
279
|
-
});
|
|
280
|
-
}
|
|
281
|
-
}
|