@icp-sdk/core 4.2.2 → 5.0.0-beta.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 +203 -0
- package/lib/cjs/agent/actor.d.ts.map +1 -0
- package/lib/cjs/agent/actor.js +341 -0
- package/lib/cjs/agent/actor.js.map +1 -0
- package/lib/cjs/agent/agent/api.d.ts +216 -0
- package/lib/cjs/agent/agent/api.d.ts.map +1 -0
- package/lib/cjs/agent/agent/api.js +39 -0
- package/lib/cjs/agent/agent/api.js.map +1 -0
- package/lib/cjs/agent/agent/http/__certificates__/goldenCertificates.d.ts +7 -0
- package/lib/cjs/agent/agent/http/__certificates__/goldenCertificates.d.ts.map +1 -0
- package/lib/cjs/agent/agent/http/__certificates__/goldenCertificates.js +19 -0
- package/lib/cjs/agent/agent/http/__certificates__/goldenCertificates.js.map +1 -0
- package/lib/cjs/agent/agent/http/index.d.ts +171 -0
- package/lib/cjs/agent/agent/http/index.d.ts.map +1 -0
- package/lib/cjs/agent/agent/http/index.js +1079 -0
- package/lib/cjs/agent/agent/http/index.js.map +1 -0
- package/lib/cjs/agent/agent/http/transforms.d.ts +54 -0
- package/lib/cjs/agent/agent/http/transforms.d.ts.map +1 -0
- package/lib/cjs/agent/agent/http/transforms.js +135 -0
- package/lib/cjs/agent/agent/http/transforms.js.map +1 -0
- package/lib/cjs/agent/agent/http/types.d.ts +81 -0
- package/lib/cjs/agent/agent/http/types.d.ts.map +1 -0
- package/lib/cjs/agent/agent/http/types.js +43 -0
- package/lib/cjs/agent/agent/http/types.js.map +1 -0
- package/lib/cjs/agent/agent/index.d.ts +3 -0
- package/lib/cjs/agent/agent/index.d.ts.map +1 -0
- package/lib/cjs/agent/agent/index.js +19 -0
- package/lib/cjs/agent/agent/index.js.map +1 -0
- package/lib/cjs/agent/auth.d.ts +91 -0
- package/lib/cjs/agent/auth.d.ts.map +1 -0
- package/lib/cjs/agent/auth.js +65 -0
- package/lib/cjs/agent/auth.js.map +1 -0
- package/lib/cjs/agent/canisterStatus/index.d.ts +93 -0
- package/lib/cjs/agent/canisterStatus/index.d.ts.map +1 -0
- package/lib/cjs/agent/canisterStatus/index.js +301 -0
- package/lib/cjs/agent/canisterStatus/index.js.map +1 -0
- package/lib/cjs/agent/cbor.d.ts +26 -0
- package/lib/cjs/agent/cbor.d.ts.map +1 -0
- package/lib/cjs/agent/cbor.js +93 -0
- package/lib/cjs/agent/cbor.js.map +1 -0
- package/lib/cjs/agent/certificate.d.ts +228 -0
- package/lib/cjs/agent/certificate.d.ts.map +1 -0
- package/lib/cjs/agent/certificate.js +591 -0
- package/lib/cjs/agent/certificate.js.map +1 -0
- package/lib/cjs/agent/constants.d.ts +14 -0
- package/lib/cjs/agent/constants.d.ts.map +1 -0
- package/lib/cjs/agent/constants.js +18 -0
- package/lib/cjs/agent/constants.js.map +1 -0
- package/lib/cjs/agent/der.d.ts +34 -0
- package/lib/cjs/agent/der.d.ts.map +1 -0
- package/lib/cjs/agent/der.js +175 -0
- package/lib/cjs/agent/der.js.map +1 -0
- package/lib/cjs/agent/errors.d.ts +380 -0
- package/lib/cjs/agent/errors.d.ts.map +1 -0
- package/lib/cjs/agent/errors.js +733 -0
- package/lib/cjs/agent/errors.js.map +1 -0
- package/lib/cjs/agent/fetch_candid.d.ts +9 -0
- package/lib/cjs/agent/fetch_candid.d.ts.map +1 -0
- package/lib/cjs/agent/fetch_candid.js +69 -0
- package/lib/cjs/agent/fetch_candid.js.map +1 -0
- package/lib/cjs/agent/index.d.ts +31 -1
- package/lib/cjs/agent/index.d.ts.map +1 -1
- package/lib/cjs/agent/index.js +59 -1
- package/lib/cjs/agent/index.js.map +1 -1
- package/lib/cjs/agent/observable.d.ts +24 -0
- package/lib/cjs/agent/observable.d.ts.map +1 -0
- package/lib/cjs/agent/observable.js +34 -0
- package/lib/cjs/agent/observable.js.map +1 -0
- package/lib/cjs/agent/polling/backoff.d.ts +50 -0
- package/lib/cjs/agent/polling/backoff.d.ts.map +1 -0
- package/lib/cjs/agent/polling/backoff.js +90 -0
- package/lib/cjs/agent/polling/backoff.js.map +1 -0
- package/lib/cjs/agent/polling/index.d.ts +63 -0
- package/lib/cjs/agent/polling/index.d.ts.map +1 -0
- package/lib/cjs/agent/polling/index.js +183 -0
- package/lib/cjs/agent/polling/index.js.map +1 -0
- package/lib/cjs/agent/polling/strategy.d.ts +52 -0
- package/lib/cjs/agent/polling/strategy.d.ts.map +1 -0
- package/lib/cjs/agent/polling/strategy.js +104 -0
- package/lib/cjs/agent/polling/strategy.js.map +1 -0
- package/lib/cjs/agent/public_key.d.ts +16 -0
- package/lib/cjs/agent/public_key.d.ts.map +1 -0
- package/lib/cjs/agent/public_key.js +52 -0
- package/lib/cjs/agent/public_key.js.map +1 -0
- package/lib/cjs/agent/request_id.d.ts +24 -0
- package/lib/cjs/agent/request_id.d.ts.map +1 -0
- package/lib/cjs/agent/request_id.js +88 -0
- package/lib/cjs/agent/request_id.js.map +1 -0
- package/lib/cjs/agent/utils/bls.d.ts +10 -0
- package/lib/cjs/agent/utils/bls.d.ts.map +1 -0
- package/lib/cjs/agent/utils/bls.js +20 -0
- package/lib/cjs/agent/utils/bls.js.map +1 -0
- package/lib/cjs/agent/utils/buffer.d.ts +22 -0
- package/lib/cjs/agent/utils/buffer.d.ts.map +1 -0
- package/lib/cjs/agent/utils/buffer.js +55 -0
- package/lib/cjs/agent/utils/buffer.js.map +1 -0
- package/lib/cjs/agent/utils/expirableMap.d.ts +80 -0
- package/lib/cjs/agent/utils/expirableMap.d.ts.map +1 -0
- package/lib/cjs/agent/utils/expirableMap.js +148 -0
- package/lib/cjs/agent/utils/expirableMap.js.map +1 -0
- package/lib/cjs/agent/utils/leb.d.ts +3 -0
- package/lib/cjs/agent/utils/leb.d.ts.map +1 -0
- package/lib/cjs/agent/utils/leb.js +17 -0
- package/lib/cjs/agent/utils/leb.js.map +1 -0
- package/lib/cjs/agent/utils/random.d.ts +6 -0
- package/lib/cjs/agent/utils/random.d.ts.map +1 -0
- package/lib/cjs/agent/utils/random.js +29 -0
- package/lib/cjs/agent/utils/random.js.map +1 -0
- package/lib/cjs/candid/candid-core.d.ts +72 -0
- package/lib/cjs/candid/candid-core.d.ts.map +1 -0
- package/lib/cjs/candid/candid-core.js +251 -0
- package/lib/cjs/candid/candid-core.js.map +1 -0
- package/lib/cjs/candid/candid-ui.d.ts +35 -0
- package/lib/cjs/candid/candid-ui.d.ts.map +1 -0
- package/lib/cjs/candid/candid-ui.js +317 -0
- package/lib/cjs/candid/candid-ui.js.map +1 -0
- package/lib/cjs/candid/idl.d.ts +579 -0
- package/lib/cjs/candid/idl.d.ts.map +1 -0
- package/lib/cjs/candid/idl.js +2228 -0
- package/lib/cjs/candid/idl.js.map +1 -0
- package/lib/cjs/candid/index.d.ts +11 -1
- package/lib/cjs/candid/index.d.ts.map +1 -1
- package/lib/cjs/candid/index.js +33 -1
- package/lib/cjs/candid/index.js.map +1 -1
- package/lib/cjs/candid/types.d.ts +6 -0
- package/lib/cjs/candid/types.d.ts.map +1 -0
- package/lib/cjs/candid/types.js +4 -0
- package/lib/cjs/candid/types.js.map +1 -0
- package/lib/cjs/candid/utils/bigint-math.d.ts +14 -0
- package/lib/cjs/candid/utils/bigint-math.d.ts.map +1 -0
- package/lib/cjs/candid/utils/bigint-math.js +30 -0
- package/lib/cjs/candid/utils/bigint-math.js.map +1 -0
- package/lib/cjs/candid/utils/buffer.d.ts +86 -0
- package/lib/cjs/candid/utils/buffer.d.ts.map +1 -0
- package/lib/cjs/candid/utils/buffer.js +191 -0
- package/lib/cjs/candid/utils/buffer.js.map +1 -0
- package/lib/cjs/candid/utils/hash.d.ts +7 -0
- package/lib/cjs/candid/utils/hash.d.ts.map +1 -0
- package/lib/cjs/candid/utils/hash.js +33 -0
- package/lib/cjs/candid/utils/hash.js.map +1 -0
- package/lib/cjs/candid/utils/leb128.d.ts +65 -0
- package/lib/cjs/candid/utils/leb128.d.ts.map +1 -0
- package/lib/cjs/candid/utils/leb128.js +228 -0
- package/lib/cjs/candid/utils/leb128.js.map +1 -0
- package/lib/cjs/identity/identity/delegation.d.ts +164 -0
- package/lib/cjs/identity/identity/delegation.d.ts.map +1 -0
- package/lib/cjs/identity/identity/delegation.js +302 -0
- package/lib/cjs/identity/identity/delegation.js.map +1 -0
- package/lib/cjs/identity/identity/ecdsa.d.ts +62 -0
- package/lib/cjs/identity/identity/ecdsa.d.ts.map +1 -0
- package/lib/cjs/identity/identity/ecdsa.js +117 -0
- package/lib/cjs/identity/identity/ecdsa.js.map +1 -0
- package/lib/cjs/identity/identity/ed25519.d.ts +67 -0
- package/lib/cjs/identity/identity/ed25519.d.ts.map +1 -0
- package/lib/cjs/identity/identity/ed25519.js +200 -0
- package/lib/cjs/identity/identity/ed25519.js.map +1 -0
- package/lib/cjs/identity/identity/partial.d.ts +34 -0
- package/lib/cjs/identity/identity/partial.d.ts.map +1 -0
- package/lib/cjs/identity/identity/partial.js +54 -0
- package/lib/cjs/identity/identity/partial.js.map +1 -0
- package/lib/cjs/identity/identity/webauthn.d.ts +51 -0
- package/lib/cjs/identity/identity/webauthn.d.ts.map +1 -0
- package/lib/cjs/identity/identity/webauthn.js +197 -0
- package/lib/cjs/identity/identity/webauthn.js.map +1 -0
- package/lib/cjs/identity/index.d.ts +15 -1
- package/lib/cjs/identity/index.d.ts.map +1 -1
- package/lib/cjs/identity/index.js +24 -1
- package/lib/cjs/identity/index.js.map +1 -1
- package/lib/cjs/identity/secp256k1/der.d.ts +33 -0
- package/lib/cjs/identity/secp256k1/der.d.ts.map +1 -0
- package/lib/cjs/identity/secp256k1/der.js +165 -0
- package/lib/cjs/identity/secp256k1/der.js.map +1 -0
- package/lib/cjs/identity/secp256k1/index.d.ts +4 -1
- package/lib/cjs/identity/secp256k1/index.d.ts.map +1 -1
- package/lib/cjs/identity/secp256k1/index.js +4 -1
- package/lib/cjs/identity/secp256k1/index.js.map +1 -1
- package/lib/cjs/identity/secp256k1/pem.d.ts +7 -0
- package/lib/cjs/identity/secp256k1/pem.d.ts.map +1 -0
- package/lib/cjs/identity/secp256k1/pem.js +75 -0
- package/lib/cjs/identity/secp256k1/pem.js.map +1 -0
- package/lib/cjs/identity/secp256k1/secp256k1.d.ts +87 -0
- package/lib/cjs/identity/secp256k1/secp256k1.d.ts.map +1 -0
- package/lib/cjs/identity/secp256k1/secp256k1.js +260 -0
- package/lib/cjs/identity/secp256k1/secp256k1.js.map +1 -0
- package/lib/cjs/principal/index.d.ts +3 -1
- package/lib/cjs/principal/index.d.ts.map +1 -1
- package/lib/cjs/principal/index.js +7 -1
- package/lib/cjs/principal/index.js.map +1 -1
- package/lib/cjs/principal/principal.d.ts +50 -0
- package/lib/cjs/principal/principal.d.ts.map +1 -0
- package/lib/cjs/principal/principal.js +148 -0
- package/lib/cjs/principal/principal.js.map +1 -0
- package/lib/cjs/principal/utils/base32.d.ts +10 -0
- package/lib/cjs/principal/utils/base32.d.ts.map +1 -0
- package/lib/cjs/principal/utils/base32.js +90 -0
- package/lib/cjs/principal/utils/base32.js.map +1 -0
- package/lib/cjs/principal/utils/getCrc.d.ts +6 -0
- package/lib/cjs/principal/utils/getCrc.d.ts.map +1 -0
- package/lib/cjs/principal/utils/getCrc.js +53 -0
- package/lib/cjs/principal/utils/getCrc.js.map +1 -0
- package/lib/esm/agent/actor.d.ts +203 -0
- package/lib/esm/agent/actor.d.ts.map +1 -0
- package/lib/esm/agent/actor.js +337 -0
- package/lib/esm/agent/actor.js.map +1 -0
- package/lib/esm/agent/agent/api.d.ts +216 -0
- package/lib/esm/agent/agent/api.d.ts.map +1 -0
- package/lib/esm/agent/agent/api.js +34 -0
- package/lib/esm/agent/agent/api.js.map +1 -0
- package/lib/esm/agent/agent/http/__certificates__/goldenCertificates.d.ts +7 -0
- package/lib/esm/agent/agent/http/__certificates__/goldenCertificates.d.ts.map +1 -0
- package/lib/esm/agent/agent/http/__certificates__/goldenCertificates.js +16 -0
- package/lib/esm/agent/agent/http/__certificates__/goldenCertificates.js.map +1 -0
- package/lib/esm/agent/agent/http/index.d.ts +171 -0
- package/lib/esm/agent/agent/http/index.d.ts.map +1 -0
- package/lib/esm/agent/agent/http/index.js +1037 -0
- package/lib/esm/agent/agent/http/index.js.map +1 -0
- package/lib/esm/agent/agent/http/transforms.d.ts +54 -0
- package/lib/esm/agent/agent/http/transforms.d.ts.map +1 -0
- package/lib/esm/agent/agent/http/transforms.js +128 -0
- package/lib/esm/agent/agent/http/transforms.js.map +1 -0
- package/lib/esm/agent/agent/http/types.d.ts +81 -0
- package/lib/esm/agent/agent/http/types.d.ts.map +1 -0
- package/lib/esm/agent/agent/http/types.js +39 -0
- package/lib/esm/agent/agent/http/types.js.map +1 -0
- package/lib/esm/agent/agent/index.d.ts +3 -0
- package/lib/esm/agent/agent/index.d.ts.map +1 -0
- package/lib/esm/agent/agent/index.js +3 -0
- package/lib/esm/agent/agent/index.js.map +1 -0
- package/lib/esm/agent/auth.d.ts +91 -0
- package/lib/esm/agent/auth.d.ts.map +1 -0
- package/lib/esm/agent/auth.js +59 -0
- package/lib/esm/agent/auth.js.map +1 -0
- package/lib/esm/agent/canister-env/index.d.ts +110 -1
- package/lib/esm/agent/canister-env/index.d.ts.map +1 -1
- package/lib/esm/agent/canister-env/index.js +110 -1
- package/lib/esm/agent/canister-env/index.js.map +1 -1
- package/lib/esm/agent/canisterStatus/index.d.ts +93 -0
- package/lib/esm/agent/canisterStatus/index.d.ts.map +1 -0
- package/lib/esm/agent/canisterStatus/index.js +261 -0
- package/lib/esm/agent/canisterStatus/index.js.map +1 -0
- package/lib/esm/agent/cbor.d.ts +26 -0
- package/lib/esm/agent/cbor.d.ts.map +1 -0
- package/lib/esm/agent/cbor.js +54 -0
- package/lib/esm/agent/cbor.js.map +1 -0
- package/lib/esm/agent/certificate.d.ts +228 -0
- package/lib/esm/agent/certificate.d.ts.map +1 -0
- package/lib/esm/agent/certificate.js +545 -0
- package/lib/esm/agent/certificate.js.map +1 -0
- package/lib/esm/agent/constants.d.ts +14 -0
- package/lib/esm/agent/constants.d.ts.map +1 -0
- package/lib/esm/agent/constants.js +15 -0
- package/lib/esm/agent/constants.js.map +1 -0
- package/lib/esm/agent/der.d.ts +34 -0
- package/lib/esm/agent/der.d.ts.map +1 -0
- package/lib/esm/agent/der.js +166 -0
- package/lib/esm/agent/der.js.map +1 -0
- package/lib/esm/agent/errors.d.ts +380 -0
- package/lib/esm/agent/errors.d.ts.map +1 -0
- package/lib/esm/agent/errors.js +678 -0
- package/lib/esm/agent/errors.js.map +1 -0
- package/lib/esm/agent/fetch_candid.d.ts +9 -0
- package/lib/esm/agent/fetch_candid.d.ts.map +1 -0
- package/lib/esm/agent/fetch_candid.js +33 -0
- package/lib/esm/agent/fetch_candid.js.map +1 -0
- package/lib/esm/agent/index.d.ts +31 -1
- package/lib/esm/agent/index.d.ts.map +1 -1
- package/lib/esm/agent/index.js +31 -1
- package/lib/esm/agent/index.js.map +1 -1
- package/lib/esm/agent/observable.d.ts +24 -0
- package/lib/esm/agent/observable.d.ts.map +1 -0
- package/lib/esm/agent/observable.js +29 -0
- package/lib/esm/agent/observable.js.map +1 -0
- package/lib/esm/agent/polling/backoff.d.ts +50 -0
- package/lib/esm/agent/polling/backoff.d.ts.map +1 -0
- package/lib/esm/agent/polling/backoff.js +85 -0
- package/lib/esm/agent/polling/backoff.js.map +1 -0
- package/lib/esm/agent/polling/index.d.ts +63 -0
- package/lib/esm/agent/polling/index.d.ts.map +1 -0
- package/lib/esm/agent/polling/index.js +144 -0
- package/lib/esm/agent/polling/index.js.map +1 -0
- package/lib/esm/agent/polling/strategy.d.ts +52 -0
- package/lib/esm/agent/polling/strategy.d.ts.map +1 -0
- package/lib/esm/agent/polling/strategy.js +94 -0
- package/lib/esm/agent/polling/strategy.js.map +1 -0
- package/lib/esm/agent/public_key.d.ts +16 -0
- package/lib/esm/agent/public_key.d.ts.map +1 -0
- package/lib/esm/agent/public_key.js +48 -0
- package/lib/esm/agent/public_key.js.map +1 -0
- package/lib/esm/agent/request_id.d.ts +24 -0
- package/lib/esm/agent/request_id.d.ts.map +1 -0
- package/lib/esm/agent/request_id.js +83 -0
- package/lib/esm/agent/request_id.js.map +1 -0
- package/lib/esm/agent/utils/bls.d.ts +10 -0
- package/lib/esm/agent/utils/bls.d.ts.map +1 -0
- package/lib/esm/agent/utils/bls.js +17 -0
- package/lib/esm/agent/utils/bls.js.map +1 -0
- package/lib/esm/agent/utils/buffer.d.ts +22 -0
- package/lib/esm/agent/utils/buffer.d.ts.map +1 -0
- package/lib/esm/agent/utils/buffer.js +50 -0
- package/lib/esm/agent/utils/buffer.js.map +1 -0
- package/lib/esm/agent/utils/expirableMap.d.ts +80 -0
- package/lib/esm/agent/utils/expirableMap.d.ts.map +1 -0
- package/lib/esm/agent/utils/expirableMap.js +144 -0
- package/lib/esm/agent/utils/expirableMap.js.map +1 -0
- package/lib/esm/agent/utils/leb.d.ts +3 -0
- package/lib/esm/agent/utils/leb.d.ts.map +1 -0
- package/lib/esm/agent/utils/leb.js +12 -0
- package/lib/esm/agent/utils/leb.js.map +1 -0
- package/lib/esm/agent/utils/random.d.ts +6 -0
- package/lib/esm/agent/utils/random.d.ts.map +1 -0
- package/lib/esm/agent/utils/random.js +25 -0
- package/lib/esm/agent/utils/random.js.map +1 -0
- package/lib/esm/candid/candid-core.d.ts +72 -0
- package/lib/esm/candid/candid-core.d.ts.map +1 -0
- package/lib/esm/candid/candid-core.js +241 -0
- package/lib/esm/candid/candid-core.js.map +1 -0
- package/lib/esm/candid/candid-ui.d.ts +35 -0
- package/lib/esm/candid/candid-ui.d.ts.map +1 -0
- package/lib/esm/candid/candid-ui.js +272 -0
- package/lib/esm/candid/candid-ui.js.map +1 -0
- package/lib/esm/candid/idl.d.ts +579 -0
- package/lib/esm/candid/idl.d.ts.map +1 -0
- package/lib/esm/candid/idl.js +2189 -0
- package/lib/esm/candid/idl.js.map +1 -0
- package/lib/esm/candid/index.d.ts +11 -1
- package/lib/esm/candid/index.d.ts.map +1 -1
- package/lib/esm/candid/index.js +10 -1
- package/lib/esm/candid/index.js.map +1 -1
- package/lib/esm/candid/types.d.ts +6 -0
- package/lib/esm/candid/types.d.ts.map +1 -0
- package/lib/esm/candid/types.js +3 -0
- package/lib/esm/candid/types.js.map +1 -0
- package/lib/esm/candid/utils/bigint-math.d.ts +14 -0
- package/lib/esm/candid/utils/bigint-math.d.ts.map +1 -0
- package/lib/esm/candid/utils/bigint-math.js +26 -0
- package/lib/esm/candid/utils/bigint-math.js.map +1 -0
- package/lib/esm/candid/utils/buffer.d.ts +86 -0
- package/lib/esm/candid/utils/buffer.d.ts.map +1 -0
- package/lib/esm/candid/utils/buffer.js +182 -0
- package/lib/esm/candid/utils/buffer.js.map +1 -0
- package/lib/esm/candid/utils/hash.d.ts +7 -0
- package/lib/esm/candid/utils/hash.d.ts.map +1 -0
- package/lib/esm/candid/utils/hash.js +30 -0
- package/lib/esm/candid/utils/hash.js.map +1 -0
- package/lib/esm/candid/utils/leb128.d.ts +65 -0
- package/lib/esm/candid/utils/leb128.d.ts.map +1 -0
- package/lib/esm/candid/utils/leb128.js +216 -0
- package/lib/esm/candid/utils/leb128.js.map +1 -0
- package/lib/esm/identity/identity/delegation.d.ts +164 -0
- package/lib/esm/identity/identity/delegation.d.ts.map +1 -0
- package/lib/esm/identity/identity/delegation.js +294 -0
- package/lib/esm/identity/identity/delegation.js.map +1 -0
- package/lib/esm/identity/identity/ecdsa.d.ts +62 -0
- package/lib/esm/identity/identity/ecdsa.d.ts.map +1 -0
- package/lib/esm/identity/identity/ecdsa.js +112 -0
- package/lib/esm/identity/identity/ecdsa.js.map +1 -0
- package/lib/esm/identity/identity/ed25519.d.ts +67 -0
- package/lib/esm/identity/identity/ed25519.d.ts.map +1 -0
- package/lib/esm/identity/identity/ed25519.js +195 -0
- package/lib/esm/identity/identity/ed25519.js.map +1 -0
- package/lib/esm/identity/identity/partial.d.ts +34 -0
- package/lib/esm/identity/identity/partial.d.ts.map +1 -0
- package/lib/esm/identity/identity/partial.js +50 -0
- package/lib/esm/identity/identity/partial.js.map +1 -0
- package/lib/esm/identity/identity/webauthn.d.ts +51 -0
- package/lib/esm/identity/identity/webauthn.d.ts.map +1 -0
- package/lib/esm/identity/identity/webauthn.js +192 -0
- package/lib/esm/identity/identity/webauthn.js.map +1 -0
- package/lib/esm/identity/index.d.ts +15 -1
- package/lib/esm/identity/index.d.ts.map +1 -1
- package/lib/esm/identity/index.js +17 -1
- package/lib/esm/identity/index.js.map +1 -1
- package/lib/esm/identity/secp256k1/der.d.ts +33 -0
- package/lib/esm/identity/secp256k1/der.d.ts.map +1 -0
- package/lib/esm/identity/secp256k1/der.js +156 -0
- package/lib/esm/identity/secp256k1/der.js.map +1 -0
- package/lib/esm/identity/secp256k1/index.d.ts +4 -1
- package/lib/esm/identity/secp256k1/index.d.ts.map +1 -1
- package/lib/esm/identity/secp256k1/index.js +4 -1
- package/lib/esm/identity/secp256k1/index.js.map +1 -1
- package/lib/esm/identity/secp256k1/pem.d.ts +7 -0
- package/lib/esm/identity/secp256k1/pem.d.ts.map +1 -0
- package/lib/esm/identity/secp256k1/pem.js +39 -0
- package/lib/esm/identity/secp256k1/pem.js.map +1 -0
- package/lib/esm/identity/secp256k1/secp256k1.d.ts +87 -0
- package/lib/esm/identity/secp256k1/secp256k1.d.ts.map +1 -0
- package/lib/esm/identity/secp256k1/secp256k1.js +222 -0
- package/lib/esm/identity/secp256k1/secp256k1.js.map +1 -0
- package/lib/esm/principal/index.d.ts +3 -1
- package/lib/esm/principal/index.d.ts.map +1 -1
- package/lib/esm/principal/index.js +3 -1
- package/lib/esm/principal/index.js.map +1 -1
- package/lib/esm/principal/principal.d.ts +50 -0
- package/lib/esm/principal/principal.d.ts.map +1 -0
- package/lib/esm/principal/principal.js +144 -0
- package/lib/esm/principal/principal.js.map +1 -0
- package/lib/esm/principal/utils/base32.d.ts +10 -0
- package/lib/esm/principal/utils/base32.d.ts.map +1 -0
- package/lib/esm/principal/utils/base32.js +86 -0
- package/lib/esm/principal/utils/base32.js.map +1 -0
- package/lib/esm/principal/utils/getCrc.d.ts +6 -0
- package/lib/esm/principal/utils/getCrc.d.ts.map +1 -0
- package/lib/esm/principal/utils/getCrc.js +50 -0
- package/lib/esm/principal/utils/getCrc.js.map +1 -0
- package/package.json +17 -11
- package/src/agent/actor.ts +576 -0
- package/src/agent/agent/api.ts +262 -0
- package/src/agent/agent/http/index.ts +1405 -0
- package/src/agent/agent/http/transforms.ts +158 -0
- package/src/agent/agent/http/types.ts +126 -0
- package/src/agent/agent/index.ts +2 -0
- package/src/agent/auth.ts +147 -0
- package/src/agent/canister-env/index.ts +191 -1
- package/src/agent/canisterStatus/index.ts +412 -0
- package/src/agent/cbor.ts +63 -0
- package/src/agent/certificate.ts +841 -0
- package/src/agent/constants.ts +19 -0
- package/src/agent/der.ts +170 -0
- package/src/agent/errors.ts +868 -0
- package/src/agent/fetch_candid.ts +37 -0
- package/src/agent/index.ts +37 -1
- package/src/agent/observable.ts +49 -0
- package/src/agent/polling/backoff.ts +130 -0
- package/src/agent/polling/index.ts +250 -0
- package/src/agent/polling/strategy.ts +144 -0
- package/src/agent/public_key.ts +65 -0
- package/src/agent/request_id.ts +92 -0
- package/src/agent/utils/bls.ts +18 -0
- package/src/agent/utils/buffer.ts +60 -0
- package/src/agent/utils/expirableMap.ts +163 -0
- package/src/agent/utils/leb.ts +14 -0
- package/src/agent/utils/random.ts +30 -0
- package/src/candid/candid-core.ts +276 -0
- package/src/candid/candid-ui.ts +289 -0
- package/src/candid/idl.ts +2556 -0
- package/src/candid/index.ts +16 -1
- package/src/candid/types.ts +7 -0
- package/src/candid/utils/bigint-math.ts +26 -0
- package/src/candid/utils/buffer.ts +221 -0
- package/src/candid/utils/hash.ts +31 -0
- package/src/candid/utils/leb128.ts +234 -0
- package/src/identity/identity/delegation.ts +411 -0
- package/src/identity/identity/ecdsa.ts +158 -0
- package/src/identity/identity/ed25519.ts +239 -0
- package/src/identity/identity/partial.ts +60 -0
- package/src/identity/identity/webauthn.ts +265 -0
- package/src/identity/index.ts +19 -1
- package/src/identity/secp256k1/der.ts +156 -0
- package/src/identity/secp256k1/index.ts +5 -1
- package/src/identity/secp256k1/pem.ts +40 -0
- package/src/identity/secp256k1/secp256k1.ts +270 -0
- package/src/package.json +4 -0
- package/src/principal/index.ts +3 -1
- package/src/principal/principal.ts +171 -0
- package/src/principal/utils/base32.ts +101 -0
- package/src/principal/utils/getCrc.ts +52 -0
|
@@ -0,0 +1,2556 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
3
|
+
import { Principal as PrincipalId } from '#principal';
|
|
4
|
+
import { type JsonValue } from './types.ts';
|
|
5
|
+
import { concat, PipeArrayBuffer as Pipe, uint8ToDataView } from './utils/buffer.ts';
|
|
6
|
+
import { idlLabelToId } from './utils/hash.ts';
|
|
7
|
+
import {
|
|
8
|
+
lebDecode,
|
|
9
|
+
lebEncode,
|
|
10
|
+
readIntLE,
|
|
11
|
+
readUIntLE,
|
|
12
|
+
safeRead,
|
|
13
|
+
safeReadUint8,
|
|
14
|
+
slebDecode,
|
|
15
|
+
slebEncode,
|
|
16
|
+
writeIntLE,
|
|
17
|
+
writeUIntLE,
|
|
18
|
+
} from './utils/leb128.ts';
|
|
19
|
+
import { iexp2 } from './utils/bigint-math.ts';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* This module provides a combinator library to create serializers/deserializers
|
|
23
|
+
* between JavaScript values and IDL used by canisters on the Internet Computer,
|
|
24
|
+
* as documented at https://github.com/dfinity/candid/blob/119703ba342d2fef6ab4972d2541b9fe36ae8e36/spec/Candid.md
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
enum IDLTypeIds {
|
|
28
|
+
Null = -1,
|
|
29
|
+
Bool = -2,
|
|
30
|
+
Nat = -3,
|
|
31
|
+
Int = -4,
|
|
32
|
+
Float32 = -13,
|
|
33
|
+
Float64 = -14,
|
|
34
|
+
Text = -15,
|
|
35
|
+
Reserved = -16,
|
|
36
|
+
Empty = -17,
|
|
37
|
+
Opt = -18,
|
|
38
|
+
Vector = -19,
|
|
39
|
+
Record = -20,
|
|
40
|
+
Variant = -21,
|
|
41
|
+
Func = -22,
|
|
42
|
+
Service = -23,
|
|
43
|
+
Principal = -24,
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const magicNumber = 'DIDL';
|
|
47
|
+
const magicNumberBytes = new TextEncoder().encode(magicNumber);
|
|
48
|
+
const toReadableString_max = 400; // will not display arguments after 400chars. Makes sure 2mb blobs don't get inside the error
|
|
49
|
+
|
|
50
|
+
function zipWith<TX, TY, TR>(xs: TX[], ys: TY[], f: (a: TX, b: TY) => TR): TR[] {
|
|
51
|
+
return xs.map((x, i) => f(x, ys[i]));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* An IDL Type Table, which precedes the data in the stream.
|
|
56
|
+
*/
|
|
57
|
+
class TypeTable {
|
|
58
|
+
// List of types. Needs to be an array as the index needs to be stable.
|
|
59
|
+
private _typs: Uint8Array[] = [];
|
|
60
|
+
private _idx = new Map<string, number>();
|
|
61
|
+
private _idxRefCount = new Map<number, number>();
|
|
62
|
+
|
|
63
|
+
public has(obj: ConstructType) {
|
|
64
|
+
return this._idx.has(obj.name);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
public add<T>(type: ConstructType<T>, buf: Uint8Array) {
|
|
68
|
+
const idx = this._typs.length;
|
|
69
|
+
this._idx.set(type.name, idx);
|
|
70
|
+
this._idxRefCount.set(idx, 1);
|
|
71
|
+
this._typs.push(buf);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
public merge<T>(obj: ConstructType<T>, knot: string) {
|
|
75
|
+
const idx = this._idx.get(obj.name);
|
|
76
|
+
const knotIdx = this._idx.get(knot);
|
|
77
|
+
if (idx === undefined) {
|
|
78
|
+
throw new Error('Missing type index for ' + obj);
|
|
79
|
+
}
|
|
80
|
+
if (knotIdx === undefined) {
|
|
81
|
+
throw new Error('Missing type index for ' + knot);
|
|
82
|
+
}
|
|
83
|
+
// Point the recursive placeholder (obj) to the concrete type bytes
|
|
84
|
+
this._typs[idx] = this._typs[knotIdx];
|
|
85
|
+
|
|
86
|
+
// Merge mappings so BOTH names point to the same index
|
|
87
|
+
// This avoids losing lookups for the concrete type name (e.g. "opt nat8")
|
|
88
|
+
// which may still be referenced elsewhere in the type table build.
|
|
89
|
+
const idxRefCount = this._getIdxRefCount(idx);
|
|
90
|
+
const knotRefCount = this._getIdxRefCount(knotIdx);
|
|
91
|
+
this._idxRefCount.set(idx, idxRefCount + knotRefCount);
|
|
92
|
+
|
|
93
|
+
// Re-point the knot name to the resolved index and mark the old index as unused
|
|
94
|
+
this._idx.set(knot, idx);
|
|
95
|
+
this._idxRefCount.set(knotIdx, 0);
|
|
96
|
+
|
|
97
|
+
// Remove unused trailing entries if possible
|
|
98
|
+
this._compactFromEnd();
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
private _getIdxRefCount(idx: number): number {
|
|
102
|
+
return this._idxRefCount.get(idx) || 0;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
private _compactFromEnd() {
|
|
106
|
+
// Remove unused entries from the end of the array
|
|
107
|
+
while (this._typs.length > 0) {
|
|
108
|
+
const lastIndex = this._typs.length - 1;
|
|
109
|
+
if (this._getIdxRefCount(lastIndex) > 0) {
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
this._typs.pop();
|
|
113
|
+
this._idxRefCount.delete(lastIndex);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
public encode(): Uint8Array {
|
|
118
|
+
const len = lebEncode(this._typs.length);
|
|
119
|
+
const buf = concat(...this._typs);
|
|
120
|
+
return concat(len, buf);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
public indexOf(typeName: string): Uint8Array {
|
|
124
|
+
if (!this._idx.has(typeName)) {
|
|
125
|
+
throw new Error('Missing type index for ' + typeName);
|
|
126
|
+
}
|
|
127
|
+
return slebEncode(this._idx.get(typeName) || 0);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export abstract class Visitor<D, R> {
|
|
132
|
+
public visitType<T>(_t: Type<T>, _data: D): R {
|
|
133
|
+
throw new Error('Not implemented');
|
|
134
|
+
}
|
|
135
|
+
public visitPrimitive<T>(t: PrimitiveType<T>, data: D): R {
|
|
136
|
+
return this.visitType(t, data);
|
|
137
|
+
}
|
|
138
|
+
public visitEmpty(t: EmptyClass, data: D): R {
|
|
139
|
+
return this.visitPrimitive(t, data);
|
|
140
|
+
}
|
|
141
|
+
public visitBool(t: BoolClass, data: D): R {
|
|
142
|
+
return this.visitPrimitive(t, data);
|
|
143
|
+
}
|
|
144
|
+
public visitNull(t: NullClass, data: D): R {
|
|
145
|
+
return this.visitPrimitive(t, data);
|
|
146
|
+
}
|
|
147
|
+
public visitReserved(t: ReservedClass, data: D): R {
|
|
148
|
+
return this.visitPrimitive(t, data);
|
|
149
|
+
}
|
|
150
|
+
public visitText(t: TextClass, data: D): R {
|
|
151
|
+
return this.visitPrimitive(t, data);
|
|
152
|
+
}
|
|
153
|
+
public visitNumber<T>(t: PrimitiveType<T>, data: D): R {
|
|
154
|
+
return this.visitPrimitive(t, data);
|
|
155
|
+
}
|
|
156
|
+
public visitInt(t: IntClass, data: D): R {
|
|
157
|
+
return this.visitNumber(t, data);
|
|
158
|
+
}
|
|
159
|
+
public visitNat(t: NatClass, data: D): R {
|
|
160
|
+
return this.visitNumber(t, data);
|
|
161
|
+
}
|
|
162
|
+
public visitFloat(t: FloatClass, data: D): R {
|
|
163
|
+
return this.visitPrimitive(t, data);
|
|
164
|
+
}
|
|
165
|
+
public visitFixedInt(t: FixedIntClass, data: D): R {
|
|
166
|
+
return this.visitNumber(t, data);
|
|
167
|
+
}
|
|
168
|
+
public visitFixedNat(t: FixedNatClass, data: D): R {
|
|
169
|
+
return this.visitNumber(t, data);
|
|
170
|
+
}
|
|
171
|
+
public visitPrincipal(t: PrincipalClass, data: D): R {
|
|
172
|
+
return this.visitPrimitive(t, data);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
public visitConstruct<T>(t: ConstructType<T>, data: D): R {
|
|
176
|
+
return this.visitType(t, data);
|
|
177
|
+
}
|
|
178
|
+
public visitVec<T>(t: VecClass<T>, _ty: Type<T>, data: D): R {
|
|
179
|
+
return this.visitConstruct(t, data);
|
|
180
|
+
}
|
|
181
|
+
public visitOpt<T>(t: OptClass<T>, _ty: Type<T>, data: D): R {
|
|
182
|
+
return this.visitConstruct(t, data);
|
|
183
|
+
}
|
|
184
|
+
public visitRecord(t: RecordClass, _fields: Array<[string, Type]>, data: D): R {
|
|
185
|
+
return this.visitConstruct(t, data);
|
|
186
|
+
}
|
|
187
|
+
public visitTuple<T extends any[]>(t: TupleClass<T>, components: Type[], data: D): R {
|
|
188
|
+
const fields: Array<[string, Type]> = components.map((ty, i) => [`_${i}_`, ty]);
|
|
189
|
+
return this.visitRecord(t, fields, data);
|
|
190
|
+
}
|
|
191
|
+
public visitVariant(t: VariantClass, _fields: Array<[string, Type]>, data: D): R {
|
|
192
|
+
return this.visitConstruct(t, data);
|
|
193
|
+
}
|
|
194
|
+
public visitRec<T>(_t: RecClass<T>, ty: ConstructType<T>, data: D): R {
|
|
195
|
+
return this.visitConstruct(ty, data);
|
|
196
|
+
}
|
|
197
|
+
public visitFunc(t: FuncClass, data: D): R {
|
|
198
|
+
return this.visitConstruct(t, data);
|
|
199
|
+
}
|
|
200
|
+
public visitService(t: ServiceClass, data: D): R {
|
|
201
|
+
return this.visitConstruct(t, data);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// We try to use hard-to-accidentally-pick names to avoid potential collisions with other types.
|
|
206
|
+
enum IdlTypeName {
|
|
207
|
+
EmptyClass = '__IDL_EmptyClass__',
|
|
208
|
+
UnknownClass = '__IDL_UnknownClass__',
|
|
209
|
+
BoolClass = '__IDL_BoolClass__',
|
|
210
|
+
NullClass = '__IDL_NullClass__',
|
|
211
|
+
ReservedClass = '__IDL_ReservedClass__',
|
|
212
|
+
TextClass = '__IDL_TextClass__',
|
|
213
|
+
IntClass = '__IDL_IntClass__',
|
|
214
|
+
NatClass = '__IDL_NatClass__',
|
|
215
|
+
FloatClass = '__IDL_FloatClass__',
|
|
216
|
+
FixedIntClass = '__IDL_FixedIntClass__',
|
|
217
|
+
FixedNatClass = '__IDL_FixedNatClass__',
|
|
218
|
+
VecClass = '__IDL_VecClass__',
|
|
219
|
+
OptClass = '__IDL_OptClass__',
|
|
220
|
+
RecordClass = '__IDL_RecordClass__',
|
|
221
|
+
TupleClass = '__IDL_TupleClass__',
|
|
222
|
+
VariantClass = '__IDL_VariantClass__',
|
|
223
|
+
RecClass = '__IDL_RecClass__',
|
|
224
|
+
PrincipalClass = '__IDL_PrincipalClass__',
|
|
225
|
+
FuncClass = '__IDL_FuncClass__',
|
|
226
|
+
ServiceClass = '__IDL_ServiceClass__',
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Represents an IDL type.
|
|
231
|
+
*/
|
|
232
|
+
export abstract class Type<T = any> {
|
|
233
|
+
public abstract readonly typeName: IdlTypeName;
|
|
234
|
+
public abstract readonly name: string;
|
|
235
|
+
public abstract accept<D, R>(v: Visitor<D, R>, d: D): R;
|
|
236
|
+
|
|
237
|
+
/* Display type name */
|
|
238
|
+
public display(): string {
|
|
239
|
+
return this.name;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
public valueToString(x: T): string {
|
|
243
|
+
return toReadableString(x);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/* Implement `T` in the IDL spec, only needed for non-primitive types */
|
|
247
|
+
public buildTypeTable(typeTable: TypeTable): void {
|
|
248
|
+
if (!typeTable.has(this)) {
|
|
249
|
+
this._buildTypeTableImpl(typeTable);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Assert that JavaScript's `x` is the proper type represented by this
|
|
255
|
+
* Type.
|
|
256
|
+
*/
|
|
257
|
+
public abstract covariant(x: any): x is T;
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Encode the value. This needs to be public because it is used by
|
|
261
|
+
* encodeValue() from different types.
|
|
262
|
+
* @internal
|
|
263
|
+
*/
|
|
264
|
+
public abstract encodeValue(x: T): Uint8Array;
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Implement `I` in the IDL spec.
|
|
268
|
+
* Encode this type for the type table.
|
|
269
|
+
*/
|
|
270
|
+
public abstract encodeType(typeTable: TypeTable): Uint8Array;
|
|
271
|
+
|
|
272
|
+
public abstract checkType(t: Type): Type;
|
|
273
|
+
|
|
274
|
+
public abstract decodeValue(x: Pipe, t: Type): T;
|
|
275
|
+
|
|
276
|
+
protected abstract _buildTypeTableImpl(typeTable: TypeTable): void;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
export abstract class PrimitiveType<T = any> extends Type<T> {
|
|
280
|
+
public checkType(t: Type): Type {
|
|
281
|
+
if (this.name !== t.name) {
|
|
282
|
+
throw new Error(`type mismatch: type on the wire ${t.name}, expect type ${this.name}`);
|
|
283
|
+
}
|
|
284
|
+
return t;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
public _buildTypeTableImpl(_typeTable: TypeTable): void {
|
|
288
|
+
// No type table encoding for Primitive types.
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
export abstract class ConstructType<T = any> extends Type<T> {
|
|
294
|
+
public checkType(t: Type): ConstructType<T> {
|
|
295
|
+
if (t instanceof RecClass) {
|
|
296
|
+
const ty = t.getType();
|
|
297
|
+
if (typeof ty === 'undefined') {
|
|
298
|
+
throw new Error('type mismatch with uninitialized type');
|
|
299
|
+
}
|
|
300
|
+
return ty;
|
|
301
|
+
}
|
|
302
|
+
throw new Error(`type mismatch: type on the wire ${t.name}, expect type ${this.name}`);
|
|
303
|
+
}
|
|
304
|
+
public encodeType(typeTable: TypeTable) {
|
|
305
|
+
return typeTable.indexOf(this.name);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Represents an IDL Empty, a type which has no inhabitants.
|
|
311
|
+
* Since no values exist for this type, it cannot be serialised or deserialised.
|
|
312
|
+
* Result types like `Result<Text, Empty>` should always succeed.
|
|
313
|
+
*/
|
|
314
|
+
export class EmptyClass extends PrimitiveType<never> {
|
|
315
|
+
get typeName() {
|
|
316
|
+
return IdlTypeName.EmptyClass;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
static [Symbol.hasInstance](instance: any): instance is EmptyClass {
|
|
320
|
+
return instance.typeName === IdlTypeName.EmptyClass;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
public accept<D, R>(v: Visitor<D, R>, d: D): R {
|
|
324
|
+
return v.visitEmpty(this, d);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
public covariant(x: any): x is never {
|
|
328
|
+
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
public encodeValue(): never {
|
|
332
|
+
throw new Error('Empty cannot appear as a function argument');
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
public valueToString(): never {
|
|
336
|
+
throw new Error('Empty cannot appear as a value');
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
public encodeType() {
|
|
340
|
+
return slebEncode(IDLTypeIds.Empty);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
public decodeValue(): never {
|
|
344
|
+
throw new Error('Empty cannot appear as an output');
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
get name() {
|
|
348
|
+
return 'empty';
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Represents an IDL Unknown, a placeholder type for deserialization only.
|
|
354
|
+
* When decoding a value as Unknown, all fields will be retained but the names are only available in
|
|
355
|
+
* hashed form.
|
|
356
|
+
* A deserialized unknown will offer it's actual type by calling the `type()` function.
|
|
357
|
+
* Unknown cannot be serialized and attempting to do so will throw an error.
|
|
358
|
+
*/
|
|
359
|
+
export class UnknownClass extends Type {
|
|
360
|
+
get typeName() {
|
|
361
|
+
return IdlTypeName.UnknownClass;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
static [Symbol.hasInstance](instance: any): instance is UnknownClass {
|
|
365
|
+
return instance.typeName === IdlTypeName.UnknownClass;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
public checkType(_t: Type): Type {
|
|
369
|
+
throw new Error('Method not implemented for unknown.');
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
public accept<D, R>(v: Visitor<D, R>, d: D): R {
|
|
373
|
+
throw v.visitType(this, d);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
public covariant(x: any): x is any {
|
|
377
|
+
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
public encodeValue(): never {
|
|
381
|
+
throw new Error('Unknown cannot appear as a function argument');
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
public valueToString(): never {
|
|
385
|
+
throw new Error('Unknown cannot appear as a value');
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
public encodeType(): never {
|
|
389
|
+
throw new Error('Unknown cannot be serialized');
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
public decodeValue(b: Pipe, t: Type): any {
|
|
393
|
+
let decodedValue = t.decodeValue(b, t);
|
|
394
|
+
|
|
395
|
+
if (Object(decodedValue) !== decodedValue) {
|
|
396
|
+
// decodedValue is primitive. Box it, otherwise we cannot add the type() function.
|
|
397
|
+
// The type() function is important for primitives because otherwise we cannot tell apart the
|
|
398
|
+
// different number types.
|
|
399
|
+
decodedValue = Object(decodedValue);
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
let typeFunc;
|
|
403
|
+
if (t instanceof RecClass) {
|
|
404
|
+
typeFunc = () => t.getType();
|
|
405
|
+
} else {
|
|
406
|
+
typeFunc = () => t;
|
|
407
|
+
}
|
|
408
|
+
// Do not use 'decodedValue.type = typeFunc' because this would lead to an enumerable property
|
|
409
|
+
// 'type' which means it would be serialized if the value would be candid encoded again.
|
|
410
|
+
// This in turn leads to problems if the decoded value is a variant because these values are
|
|
411
|
+
// only allowed to have a single property.
|
|
412
|
+
Object.defineProperty(decodedValue, 'type', {
|
|
413
|
+
value: typeFunc,
|
|
414
|
+
writable: true,
|
|
415
|
+
enumerable: false,
|
|
416
|
+
configurable: true,
|
|
417
|
+
});
|
|
418
|
+
return decodedValue;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
protected _buildTypeTableImpl(): void {
|
|
422
|
+
throw new Error('Unknown cannot be serialized');
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
get name() {
|
|
426
|
+
return 'Unknown';
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* Represents an IDL Bool
|
|
432
|
+
*/
|
|
433
|
+
export class BoolClass extends PrimitiveType<boolean> {
|
|
434
|
+
get typeName() {
|
|
435
|
+
return IdlTypeName.BoolClass;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
static [Symbol.hasInstance](instance: any): instance is BoolClass {
|
|
439
|
+
return instance.typeName === IdlTypeName.BoolClass;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
public accept<D, R>(v: Visitor<D, R>, d: D): R {
|
|
443
|
+
return v.visitBool(this, d);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
public covariant(x: any): x is boolean {
|
|
447
|
+
if (typeof x === 'boolean') return true;
|
|
448
|
+
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
public encodeValue(x: boolean): Uint8Array {
|
|
452
|
+
return new Uint8Array([x ? 1 : 0]);
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
public encodeType(): Uint8Array {
|
|
456
|
+
return slebEncode(IDLTypeIds.Bool);
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
public decodeValue(b: Pipe, t: Type) {
|
|
460
|
+
this.checkType(t);
|
|
461
|
+
switch (safeReadUint8(b)) {
|
|
462
|
+
case 0:
|
|
463
|
+
return false;
|
|
464
|
+
case 1:
|
|
465
|
+
return true;
|
|
466
|
+
default:
|
|
467
|
+
throw new Error('Boolean value out of range');
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
get name() {
|
|
472
|
+
return 'bool';
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
/**
|
|
477
|
+
* Represents an IDL Null
|
|
478
|
+
*/
|
|
479
|
+
export class NullClass extends PrimitiveType<null> {
|
|
480
|
+
get typeName() {
|
|
481
|
+
return IdlTypeName.NullClass;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
static [Symbol.hasInstance](instance: any): instance is NullClass {
|
|
485
|
+
return instance.typeName === IdlTypeName.NullClass;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
public accept<D, R>(v: Visitor<D, R>, d: D): R {
|
|
489
|
+
return v.visitNull(this, d);
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
public covariant(x: any): x is null {
|
|
493
|
+
if (x === null) return true;
|
|
494
|
+
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
public encodeValue(): Uint8Array {
|
|
498
|
+
return new Uint8Array(0);
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
public encodeType(): Uint8Array {
|
|
502
|
+
return slebEncode(IDLTypeIds.Null);
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
public decodeValue(_b: Pipe, t: Type) {
|
|
506
|
+
this.checkType(t);
|
|
507
|
+
return null;
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
get name() {
|
|
511
|
+
return 'null';
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/**
|
|
516
|
+
* Represents an IDL Reserved
|
|
517
|
+
*/
|
|
518
|
+
export class ReservedClass extends PrimitiveType<any> {
|
|
519
|
+
get typeName() {
|
|
520
|
+
return IdlTypeName.ReservedClass;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
static [Symbol.hasInstance](instance: any): instance is ReservedClass {
|
|
524
|
+
return instance.typeName === IdlTypeName.ReservedClass;
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
public accept<D, R>(v: Visitor<D, R>, d: D): R {
|
|
528
|
+
return v.visitReserved(this, d);
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
public covariant(_x: any): _x is any {
|
|
532
|
+
return true;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
public encodeValue(): Uint8Array {
|
|
536
|
+
return new Uint8Array(0);
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
public encodeType(): Uint8Array {
|
|
540
|
+
return slebEncode(IDLTypeIds.Reserved);
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
public decodeValue(b: Pipe, t: Type) {
|
|
544
|
+
if (t.name !== this.name) {
|
|
545
|
+
t.decodeValue(b, t);
|
|
546
|
+
}
|
|
547
|
+
return null;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
get name() {
|
|
551
|
+
return 'reserved';
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
/**
|
|
556
|
+
* Represents an IDL Text
|
|
557
|
+
*/
|
|
558
|
+
export class TextClass extends PrimitiveType<string> {
|
|
559
|
+
get typeName() {
|
|
560
|
+
return IdlTypeName.TextClass;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
static [Symbol.hasInstance](instance: any): instance is TextClass {
|
|
564
|
+
return instance.typeName === IdlTypeName.TextClass;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
public accept<D, R>(v: Visitor<D, R>, d: D): R {
|
|
568
|
+
return v.visitText(this, d);
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
public covariant(x: any): x is string {
|
|
572
|
+
if (typeof x === 'string') return true;
|
|
573
|
+
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
public encodeValue(x: string) {
|
|
577
|
+
const buf = new TextEncoder().encode(x);
|
|
578
|
+
const len = lebEncode(buf.byteLength);
|
|
579
|
+
return concat(len, buf);
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
public encodeType() {
|
|
583
|
+
return slebEncode(IDLTypeIds.Text);
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
public decodeValue(b: Pipe, t: Type) {
|
|
587
|
+
this.checkType(t);
|
|
588
|
+
const len = lebDecode(b);
|
|
589
|
+
const buf = safeRead(b, Number(len));
|
|
590
|
+
const decoder = new TextDecoder('utf8', { fatal: true });
|
|
591
|
+
return decoder.decode(buf);
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
get name() {
|
|
595
|
+
return 'text';
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
public valueToString(x: string) {
|
|
599
|
+
return '"' + x + '"';
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
/**
|
|
604
|
+
* Represents an IDL Int
|
|
605
|
+
*/
|
|
606
|
+
export class IntClass extends PrimitiveType<bigint> {
|
|
607
|
+
get typeName() {
|
|
608
|
+
return IdlTypeName.IntClass;
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
static [Symbol.hasInstance](instance: any): instance is IntClass {
|
|
612
|
+
return instance.typeName === IdlTypeName.IntClass;
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
public accept<D, R>(v: Visitor<D, R>, d: D): R {
|
|
616
|
+
return v.visitInt(this, d);
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
public covariant(x: any): x is bigint {
|
|
620
|
+
// We allow encoding of JavaScript plain numbers.
|
|
621
|
+
// But we will always decode to bigint.
|
|
622
|
+
if (typeof x === 'bigint' || Number.isInteger(x)) return true;
|
|
623
|
+
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
public encodeValue(x: bigint | number): Uint8Array {
|
|
627
|
+
return slebEncode(x);
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
public encodeType() {
|
|
631
|
+
return slebEncode(IDLTypeIds.Int);
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
public decodeValue(b: Pipe, t: Type) {
|
|
635
|
+
this.checkType(t);
|
|
636
|
+
return slebDecode(b);
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
get name() {
|
|
640
|
+
return 'int';
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
public valueToString(x: bigint) {
|
|
644
|
+
return x.toString();
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
/**
|
|
649
|
+
* Represents an IDL Nat
|
|
650
|
+
*/
|
|
651
|
+
export class NatClass extends PrimitiveType<bigint> {
|
|
652
|
+
get typeName() {
|
|
653
|
+
return IdlTypeName.NatClass;
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
static [Symbol.hasInstance](instance: any): instance is NatClass {
|
|
657
|
+
return instance.typeName === IdlTypeName.NatClass;
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
public accept<D, R>(v: Visitor<D, R>, d: D): R {
|
|
661
|
+
return v.visitNat(this, d);
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
public covariant(x: any): x is bigint {
|
|
665
|
+
// We allow encoding of JavaScript plain numbers.
|
|
666
|
+
// But we will always decode to bigint.
|
|
667
|
+
if ((typeof x === 'bigint' && x >= BigInt(0)) || (Number.isInteger(x) && x >= 0)) return true;
|
|
668
|
+
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
public encodeValue(x: bigint | number): Uint8Array {
|
|
672
|
+
return lebEncode(x);
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
public encodeType() {
|
|
676
|
+
return slebEncode(IDLTypeIds.Nat);
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
public decodeValue(b: Pipe, t: Type) {
|
|
680
|
+
this.checkType(t);
|
|
681
|
+
return lebDecode(b);
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
get name() {
|
|
685
|
+
return 'nat';
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
public valueToString(x: bigint) {
|
|
689
|
+
return x.toString();
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
/**
|
|
694
|
+
* Represents an IDL Float
|
|
695
|
+
*/
|
|
696
|
+
export class FloatClass extends PrimitiveType<number> {
|
|
697
|
+
get typeName() {
|
|
698
|
+
return IdlTypeName.FloatClass;
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
static [Symbol.hasInstance](instance: any): instance is FloatClass {
|
|
702
|
+
return instance.typeName === IdlTypeName.FloatClass;
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
constructor(public readonly _bits: number) {
|
|
706
|
+
super();
|
|
707
|
+
if (_bits !== 32 && _bits !== 64) {
|
|
708
|
+
throw new Error('not a valid float type');
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
public accept<D, R>(v: Visitor<D, R>, d: D): R {
|
|
712
|
+
return v.visitFloat(this, d);
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
public covariant(x: any): x is number {
|
|
716
|
+
if (typeof x === 'number' || x instanceof Number) return true;
|
|
717
|
+
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
public encodeValue(x: number) {
|
|
721
|
+
const buf = new ArrayBuffer(this._bits / 8);
|
|
722
|
+
const view = new DataView(buf);
|
|
723
|
+
if (this._bits === 32) {
|
|
724
|
+
view.setFloat32(0, x, true);
|
|
725
|
+
} else {
|
|
726
|
+
view.setFloat64(0, x, true);
|
|
727
|
+
}
|
|
728
|
+
return new Uint8Array(buf);
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
public encodeType(): Uint8Array {
|
|
732
|
+
const opcode = this._bits === 32 ? IDLTypeIds.Float32 : IDLTypeIds.Float64;
|
|
733
|
+
return slebEncode(opcode);
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
public decodeValue(b: Pipe, t: Type) {
|
|
737
|
+
this.checkType(t);
|
|
738
|
+
const bytes = safeRead(b, this._bits / 8);
|
|
739
|
+
const view = uint8ToDataView(bytes);
|
|
740
|
+
if (this._bits === 32) {
|
|
741
|
+
return view.getFloat32(0, true);
|
|
742
|
+
} else {
|
|
743
|
+
return view.getFloat64(0, true);
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
get name() {
|
|
748
|
+
return 'float' + this._bits;
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
public valueToString(x: number) {
|
|
752
|
+
return x.toString();
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
/**
|
|
757
|
+
* Represents an IDL fixed-width Int(n)
|
|
758
|
+
*/
|
|
759
|
+
export class FixedIntClass extends PrimitiveType<bigint | number> {
|
|
760
|
+
get typeName() {
|
|
761
|
+
return IdlTypeName.FixedIntClass;
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
static [Symbol.hasInstance](instance: any): instance is FixedIntClass {
|
|
765
|
+
return instance.typeName === IdlTypeName.FixedIntClass;
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
constructor(public readonly _bits: number) {
|
|
769
|
+
super();
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
public accept<D, R>(v: Visitor<D, R>, d: D): R {
|
|
773
|
+
return v.visitFixedInt(this, d);
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
public covariant(x: any): x is bigint {
|
|
777
|
+
const min = iexp2(this._bits - 1) * BigInt(-1);
|
|
778
|
+
const max = iexp2(this._bits - 1) - BigInt(1);
|
|
779
|
+
let ok = false;
|
|
780
|
+
if (typeof x === 'bigint') {
|
|
781
|
+
ok = x >= min && x <= max;
|
|
782
|
+
} else if (Number.isInteger(x)) {
|
|
783
|
+
const v = BigInt(x);
|
|
784
|
+
ok = v >= min && v <= max;
|
|
785
|
+
} else {
|
|
786
|
+
ok = false;
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
if (ok) return true;
|
|
790
|
+
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
public encodeValue(x: bigint | number) {
|
|
794
|
+
return writeIntLE(x, this._bits / 8);
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
public encodeType() {
|
|
798
|
+
const offset = Math.log2(this._bits) - 3;
|
|
799
|
+
return slebEncode(-9 - offset);
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
public decodeValue(b: Pipe, t: Type) {
|
|
803
|
+
this.checkType(t);
|
|
804
|
+
const num = readIntLE(b, this._bits / 8);
|
|
805
|
+
if (this._bits <= 32) {
|
|
806
|
+
return Number(num);
|
|
807
|
+
} else {
|
|
808
|
+
return num;
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
get name() {
|
|
813
|
+
return `int${this._bits}`;
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
public valueToString(x: bigint | number) {
|
|
817
|
+
return x.toString();
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
/**
|
|
822
|
+
* Represents an IDL fixed-width Nat(n)
|
|
823
|
+
*/
|
|
824
|
+
export class FixedNatClass extends PrimitiveType<bigint | number> {
|
|
825
|
+
get typeName() {
|
|
826
|
+
return IdlTypeName.FixedNatClass;
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
static [Symbol.hasInstance](instance: any): instance is FixedNatClass {
|
|
830
|
+
return instance.typeName === IdlTypeName.FixedNatClass;
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
constructor(public readonly _bits: number) {
|
|
834
|
+
super();
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
public accept<D, R>(v: Visitor<D, R>, d: D): R {
|
|
838
|
+
return v.visitFixedNat(this, d);
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
public covariant(x: any): x is bigint {
|
|
842
|
+
const max = iexp2(this._bits);
|
|
843
|
+
let ok = false;
|
|
844
|
+
if (typeof x === 'bigint' && x >= BigInt(0)) {
|
|
845
|
+
ok = x < max;
|
|
846
|
+
} else if (Number.isInteger(x) && x >= 0) {
|
|
847
|
+
const v = BigInt(x);
|
|
848
|
+
ok = v < max;
|
|
849
|
+
} else {
|
|
850
|
+
ok = false;
|
|
851
|
+
}
|
|
852
|
+
if (ok) return true;
|
|
853
|
+
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
public encodeValue(x: bigint | number) {
|
|
857
|
+
return writeUIntLE(x, this._bits / 8);
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
public encodeType() {
|
|
861
|
+
const offset = Math.log2(this._bits) - 3;
|
|
862
|
+
return slebEncode(-5 - offset);
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
public decodeValue(b: Pipe, t: Type) {
|
|
866
|
+
this.checkType(t);
|
|
867
|
+
const num = readUIntLE(b, this._bits / 8);
|
|
868
|
+
if (this._bits <= 32) {
|
|
869
|
+
return Number(num);
|
|
870
|
+
} else {
|
|
871
|
+
return num;
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
get name() {
|
|
876
|
+
return `nat${this._bits}`;
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
public valueToString(x: bigint | number) {
|
|
880
|
+
return x.toString();
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
/**
|
|
885
|
+
* Represents an IDL Array
|
|
886
|
+
*
|
|
887
|
+
* Arrays of fixed-sized nat/int type (e.g. nat8), are encoded from and decoded to TypedArrays (e.g. Uint8Array).
|
|
888
|
+
* Arrays of float or other non-primitive types are encoded/decoded as untyped array in Javascript.
|
|
889
|
+
* @param {Type} t
|
|
890
|
+
*/
|
|
891
|
+
export class VecClass<T> extends ConstructType<T[]> {
|
|
892
|
+
get typeName() {
|
|
893
|
+
return IdlTypeName.VecClass;
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
static [Symbol.hasInstance]<T>(instance: any): instance is VecClass<T> {
|
|
897
|
+
return instance.typeName === IdlTypeName.VecClass;
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
// If true, this vector is really a blob and we can just use memcpy.
|
|
901
|
+
//
|
|
902
|
+
// NOTE:
|
|
903
|
+
// With support of encoding/dencoding of TypedArrays, this optimization is
|
|
904
|
+
// only used when plain array of bytes are passed as encoding input in order
|
|
905
|
+
// to be backward compatible.
|
|
906
|
+
private _blobOptimization = false;
|
|
907
|
+
|
|
908
|
+
constructor(public _type: Type<T>) {
|
|
909
|
+
super();
|
|
910
|
+
if (_type instanceof FixedNatClass && _type._bits === 8) {
|
|
911
|
+
this._blobOptimization = true;
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
public accept<D, R>(v: Visitor<D, R>, d: D): R {
|
|
916
|
+
return v.visitVec(this, this._type, d);
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
public covariant(x: any): x is T[] {
|
|
920
|
+
// Special case for ArrayBuffer
|
|
921
|
+
const bits =
|
|
922
|
+
this._type instanceof FixedNatClass
|
|
923
|
+
? this._type._bits
|
|
924
|
+
: this._type instanceof FixedIntClass
|
|
925
|
+
? this._type._bits
|
|
926
|
+
: 0;
|
|
927
|
+
|
|
928
|
+
if (
|
|
929
|
+
(ArrayBuffer.isView(x) && bits == (x as any).BYTES_PER_ELEMENT * 8) ||
|
|
930
|
+
(Array.isArray(x) &&
|
|
931
|
+
x.every((v, idx) => {
|
|
932
|
+
try {
|
|
933
|
+
return this._type.covariant(v);
|
|
934
|
+
} catch (e: any) {
|
|
935
|
+
throw new Error(`Invalid ${this.display()} argument: \n\nindex ${idx} -> ${e.message}`);
|
|
936
|
+
}
|
|
937
|
+
}))
|
|
938
|
+
)
|
|
939
|
+
return true;
|
|
940
|
+
|
|
941
|
+
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
public encodeValue(x: T[]): Uint8Array {
|
|
945
|
+
const len = lebEncode(x.length);
|
|
946
|
+
if (this._blobOptimization) {
|
|
947
|
+
return concat(len, new Uint8Array(x as unknown as number[]));
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
if (ArrayBuffer.isView(x)) {
|
|
951
|
+
// Handle TypedArrays with endianness concerns
|
|
952
|
+
if (x instanceof Int16Array || x instanceof Uint16Array) {
|
|
953
|
+
const buffer = new DataView(new ArrayBuffer(x.length * 2));
|
|
954
|
+
for (let i = 0; i < x.length; i++) {
|
|
955
|
+
if (x instanceof Int16Array) {
|
|
956
|
+
buffer.setInt16(i * 2, x[i], true); // true = little-endian
|
|
957
|
+
} else {
|
|
958
|
+
buffer.setUint16(i * 2, x[i], true);
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
return concat(len, new Uint8Array(buffer.buffer));
|
|
962
|
+
} else if (x instanceof Int32Array || x instanceof Uint32Array) {
|
|
963
|
+
const buffer = new DataView(new ArrayBuffer(x.length * 4));
|
|
964
|
+
for (let i = 0; i < x.length; i++) {
|
|
965
|
+
if (x instanceof Int32Array) {
|
|
966
|
+
buffer.setInt32(i * 4, x[i], true);
|
|
967
|
+
} else {
|
|
968
|
+
buffer.setUint32(i * 4, x[i], true);
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
return concat(len, new Uint8Array(buffer.buffer));
|
|
972
|
+
} else if (x instanceof BigInt64Array || x instanceof BigUint64Array) {
|
|
973
|
+
const buffer = new DataView(new ArrayBuffer(x.length * 8));
|
|
974
|
+
for (let i = 0; i < x.length; i++) {
|
|
975
|
+
if (x instanceof BigInt64Array) {
|
|
976
|
+
buffer.setBigInt64(i * 8, x[i], true);
|
|
977
|
+
} else {
|
|
978
|
+
buffer.setBigUint64(i * 8, x[i], true);
|
|
979
|
+
}
|
|
980
|
+
}
|
|
981
|
+
return concat(len, new Uint8Array(buffer.buffer));
|
|
982
|
+
} else {
|
|
983
|
+
// For Uint8Array, Int8Array, etc. that don't have endianness concerns
|
|
984
|
+
return concat(len, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
const buf = new Pipe(new Uint8Array(len.byteLength + x.length), 0);
|
|
988
|
+
buf.write(len);
|
|
989
|
+
for (const d of x) {
|
|
990
|
+
const encoded = this._type.encodeValue(d);
|
|
991
|
+
buf.write(new Uint8Array(encoded));
|
|
992
|
+
}
|
|
993
|
+
return buf.buffer;
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
public _buildTypeTableImpl(typeTable: TypeTable) {
|
|
997
|
+
this._type.buildTypeTable(typeTable);
|
|
998
|
+
|
|
999
|
+
const opCode = slebEncode(IDLTypeIds.Vector);
|
|
1000
|
+
const buffer = this._type.encodeType(typeTable);
|
|
1001
|
+
typeTable.add(this, concat(opCode, buffer));
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
public decodeValue(b: Pipe, t: Type): T[] {
|
|
1005
|
+
const vec = this.checkType(t);
|
|
1006
|
+
if (!(vec instanceof VecClass)) {
|
|
1007
|
+
throw new Error('Not a vector type');
|
|
1008
|
+
}
|
|
1009
|
+
const len = Number(lebDecode(b));
|
|
1010
|
+
|
|
1011
|
+
if (this._type instanceof FixedNatClass) {
|
|
1012
|
+
if (this._type._bits == 8) {
|
|
1013
|
+
return new Uint8Array(b.read(len)) as unknown as T[];
|
|
1014
|
+
}
|
|
1015
|
+
if (this._type._bits == 16) {
|
|
1016
|
+
const bytes = b.read(len * 2);
|
|
1017
|
+
// Check if we need to swap bytes for endianness
|
|
1018
|
+
const u16 = new Uint16Array(bytes.buffer, bytes.byteOffset, len);
|
|
1019
|
+
return u16 as unknown as T[];
|
|
1020
|
+
}
|
|
1021
|
+
if (this._type._bits == 32) {
|
|
1022
|
+
const bytes = b.read(len * 4);
|
|
1023
|
+
const u32 = new Uint32Array(bytes.buffer, bytes.byteOffset, len);
|
|
1024
|
+
return u32 as unknown as T[];
|
|
1025
|
+
}
|
|
1026
|
+
if (this._type._bits == 64) {
|
|
1027
|
+
return new BigUint64Array(b.read(len * 8).buffer) as unknown as T[];
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
1030
|
+
|
|
1031
|
+
if (this._type instanceof FixedIntClass) {
|
|
1032
|
+
if (this._type._bits == 8) {
|
|
1033
|
+
return new Int8Array(b.read(len)) as unknown as T[];
|
|
1034
|
+
}
|
|
1035
|
+
if (this._type._bits == 16) {
|
|
1036
|
+
const bytes = b.read(len * 2);
|
|
1037
|
+
// Create a DataView to properly handle endianness
|
|
1038
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
1039
|
+
|
|
1040
|
+
// Create result array with correct endianness
|
|
1041
|
+
const result = new Int16Array(len);
|
|
1042
|
+
for (let i = 0; i < len; i++) {
|
|
1043
|
+
// Read each value as little-endian (Candid wire format is little-endian)
|
|
1044
|
+
result[i] = view.getInt16(i * 2, true);
|
|
1045
|
+
}
|
|
1046
|
+
return result as unknown as T[];
|
|
1047
|
+
}
|
|
1048
|
+
if (this._type._bits == 32) {
|
|
1049
|
+
const bytes = b.read(len * 4);
|
|
1050
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
1051
|
+
|
|
1052
|
+
const result = new Int32Array(len);
|
|
1053
|
+
for (let i = 0; i < len; i++) {
|
|
1054
|
+
result[i] = view.getInt32(i * 4, true);
|
|
1055
|
+
}
|
|
1056
|
+
return result as unknown as T[];
|
|
1057
|
+
}
|
|
1058
|
+
if (this._type._bits == 64) {
|
|
1059
|
+
const bytes = b.read(len * 8);
|
|
1060
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
1061
|
+
|
|
1062
|
+
const result = new BigInt64Array(len);
|
|
1063
|
+
for (let i = 0; i < len; i++) {
|
|
1064
|
+
result[i] = view.getBigInt64(i * 8, true);
|
|
1065
|
+
}
|
|
1066
|
+
return result as unknown as T[];
|
|
1067
|
+
}
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
const rets: T[] = [];
|
|
1071
|
+
for (let i = 0; i < len; i++) {
|
|
1072
|
+
rets.push(this._type.decodeValue(b, vec._type));
|
|
1073
|
+
}
|
|
1074
|
+
return rets;
|
|
1075
|
+
}
|
|
1076
|
+
|
|
1077
|
+
get name() {
|
|
1078
|
+
return `vec ${this._type.name}`;
|
|
1079
|
+
}
|
|
1080
|
+
|
|
1081
|
+
public display() {
|
|
1082
|
+
return `vec ${this._type.display()}`;
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
public valueToString(x: T[]) {
|
|
1086
|
+
const elements = x.map(e => this._type.valueToString(e));
|
|
1087
|
+
return 'vec {' + elements.join('; ') + '}';
|
|
1088
|
+
}
|
|
1089
|
+
}
|
|
1090
|
+
|
|
1091
|
+
/**
|
|
1092
|
+
* Represents an IDL Option
|
|
1093
|
+
* @param {Type} t
|
|
1094
|
+
*/
|
|
1095
|
+
export class OptClass<T> extends ConstructType<[T] | []> {
|
|
1096
|
+
get typeName() {
|
|
1097
|
+
return IdlTypeName.OptClass;
|
|
1098
|
+
}
|
|
1099
|
+
|
|
1100
|
+
static [Symbol.hasInstance]<T>(instance: any): instance is OptClass<T> {
|
|
1101
|
+
return instance.typeName === IdlTypeName.OptClass;
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
constructor(public _type: Type<T>) {
|
|
1105
|
+
super();
|
|
1106
|
+
}
|
|
1107
|
+
|
|
1108
|
+
public accept<D, R>(v: Visitor<D, R>, d: D): R {
|
|
1109
|
+
return v.visitOpt(this, this._type, d);
|
|
1110
|
+
}
|
|
1111
|
+
|
|
1112
|
+
public covariant(x: any): x is [T] | [] {
|
|
1113
|
+
try {
|
|
1114
|
+
if (Array.isArray(x) && (x.length === 0 || (x.length === 1 && this._type.covariant(x[0]))))
|
|
1115
|
+
return true;
|
|
1116
|
+
} catch (e: any) {
|
|
1117
|
+
throw new Error(
|
|
1118
|
+
`Invalid ${this.display()} argument: ${toReadableString(x)} \n\n-> ${e.message}`,
|
|
1119
|
+
);
|
|
1120
|
+
}
|
|
1121
|
+
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
1122
|
+
}
|
|
1123
|
+
|
|
1124
|
+
public encodeValue(x: [T] | []): Uint8Array {
|
|
1125
|
+
if (x.length === 0) {
|
|
1126
|
+
return new Uint8Array([0]);
|
|
1127
|
+
} else {
|
|
1128
|
+
return concat(new Uint8Array([1]), this._type.encodeValue(x[0]));
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1131
|
+
|
|
1132
|
+
public _buildTypeTableImpl(typeTable: TypeTable) {
|
|
1133
|
+
this._type.buildTypeTable(typeTable);
|
|
1134
|
+
|
|
1135
|
+
const opCode = slebEncode(IDLTypeIds.Opt);
|
|
1136
|
+
const buffer = this._type.encodeType(typeTable);
|
|
1137
|
+
typeTable.add(this, concat(opCode, buffer));
|
|
1138
|
+
}
|
|
1139
|
+
|
|
1140
|
+
public decodeValue(b: Pipe, t: Type): [T] | [] {
|
|
1141
|
+
if (t instanceof NullClass) {
|
|
1142
|
+
return [];
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1145
|
+
if (t instanceof ReservedClass) {
|
|
1146
|
+
return [];
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
let wireType = t;
|
|
1150
|
+
// unfold wireType, if needed
|
|
1151
|
+
if (t instanceof RecClass) {
|
|
1152
|
+
const ty = t.getType();
|
|
1153
|
+
if (typeof ty === 'undefined') {
|
|
1154
|
+
throw new Error('type mismatch with uninitialized type');
|
|
1155
|
+
} else wireType = ty;
|
|
1156
|
+
}
|
|
1157
|
+
|
|
1158
|
+
if (wireType instanceof OptClass) {
|
|
1159
|
+
switch (safeReadUint8(b)) {
|
|
1160
|
+
case 0:
|
|
1161
|
+
return [];
|
|
1162
|
+
case 1: {
|
|
1163
|
+
// Save the current state of the Pipe `b` to allow rollback in case of an error
|
|
1164
|
+
const checkpoint = b.save();
|
|
1165
|
+
try {
|
|
1166
|
+
// Attempt to decode a value using the `_type` of the current instance
|
|
1167
|
+
const v = this._type.decodeValue(b, wireType._type);
|
|
1168
|
+
return [v];
|
|
1169
|
+
} catch (e: any) {
|
|
1170
|
+
// If an error occurs during decoding, restore the Pipe `b` to its previous state
|
|
1171
|
+
b.restore(checkpoint);
|
|
1172
|
+
// Skip the value at the current wire type to advance the Pipe `b` position
|
|
1173
|
+
wireType._type.decodeValue(b, wireType._type);
|
|
1174
|
+
// Return an empty array to indicate a `none` value
|
|
1175
|
+
return [];
|
|
1176
|
+
}
|
|
1177
|
+
}
|
|
1178
|
+
default:
|
|
1179
|
+
throw new Error('Not an option value');
|
|
1180
|
+
}
|
|
1181
|
+
} else if (
|
|
1182
|
+
// this check corresponds to `not (null <: <t>)` in the spec
|
|
1183
|
+
this._type instanceof NullClass ||
|
|
1184
|
+
this._type instanceof OptClass ||
|
|
1185
|
+
this._type instanceof ReservedClass
|
|
1186
|
+
) {
|
|
1187
|
+
// null <: <t> :
|
|
1188
|
+
// skip value at wire type (to advance b) and return "null", i.e. []
|
|
1189
|
+
wireType.decodeValue(b, wireType);
|
|
1190
|
+
return [];
|
|
1191
|
+
} else {
|
|
1192
|
+
// not (null <: t) :
|
|
1193
|
+
// try constituent type
|
|
1194
|
+
const checkpoint = b.save();
|
|
1195
|
+
try {
|
|
1196
|
+
const v = this._type.decodeValue(b, t);
|
|
1197
|
+
return [v];
|
|
1198
|
+
} catch (e: any) {
|
|
1199
|
+
// decoding failed, but this is opt, so return "null", i.e. []
|
|
1200
|
+
b.restore(checkpoint);
|
|
1201
|
+
// skip value at wire type (to advance b)
|
|
1202
|
+
wireType.decodeValue(b, t);
|
|
1203
|
+
// return "null"
|
|
1204
|
+
return [];
|
|
1205
|
+
}
|
|
1206
|
+
}
|
|
1207
|
+
}
|
|
1208
|
+
|
|
1209
|
+
get name() {
|
|
1210
|
+
return `opt ${this._type.name}`;
|
|
1211
|
+
}
|
|
1212
|
+
|
|
1213
|
+
public display() {
|
|
1214
|
+
return `opt ${this._type.display()}`;
|
|
1215
|
+
}
|
|
1216
|
+
|
|
1217
|
+
public valueToString(x: [T] | []) {
|
|
1218
|
+
if (x.length === 0) {
|
|
1219
|
+
return 'null';
|
|
1220
|
+
} else {
|
|
1221
|
+
return `opt ${this._type.valueToString(x[0])}`;
|
|
1222
|
+
}
|
|
1223
|
+
}
|
|
1224
|
+
}
|
|
1225
|
+
|
|
1226
|
+
/**
|
|
1227
|
+
* Represents an IDL Record
|
|
1228
|
+
* @param {object} [fields] - mapping of function name to Type
|
|
1229
|
+
*/
|
|
1230
|
+
export class RecordClass extends ConstructType<Record<string, any>> {
|
|
1231
|
+
get typeName() {
|
|
1232
|
+
return IdlTypeName.RecordClass;
|
|
1233
|
+
}
|
|
1234
|
+
|
|
1235
|
+
static [Symbol.hasInstance](instance: any): instance is RecordClass {
|
|
1236
|
+
// TupleClass extends RecordClass, so we need to check both here
|
|
1237
|
+
return (
|
|
1238
|
+
instance.typeName === IdlTypeName.RecordClass || instance.typeName === IdlTypeName.TupleClass
|
|
1239
|
+
);
|
|
1240
|
+
}
|
|
1241
|
+
|
|
1242
|
+
public readonly _fields: Array<[string, Type]>;
|
|
1243
|
+
|
|
1244
|
+
constructor(fields: Record<string, Type> = {}) {
|
|
1245
|
+
super();
|
|
1246
|
+
this._fields = Object.entries(fields).sort((a, b) => idlLabelToId(a[0]) - idlLabelToId(b[0]));
|
|
1247
|
+
}
|
|
1248
|
+
|
|
1249
|
+
public accept<D, R>(v: Visitor<D, R>, d: D): R {
|
|
1250
|
+
return v.visitRecord(this, this._fields, d);
|
|
1251
|
+
}
|
|
1252
|
+
|
|
1253
|
+
public tryAsTuple(): Type[] | null {
|
|
1254
|
+
const res: Type[] = [];
|
|
1255
|
+
for (let i = 0; i < this._fields.length; i++) {
|
|
1256
|
+
const [key, type] = this._fields[i];
|
|
1257
|
+
if (key !== `_${i}_`) {
|
|
1258
|
+
return null;
|
|
1259
|
+
}
|
|
1260
|
+
res.push(type);
|
|
1261
|
+
}
|
|
1262
|
+
return res;
|
|
1263
|
+
}
|
|
1264
|
+
|
|
1265
|
+
public covariant(x: any): x is Record<string, any> {
|
|
1266
|
+
if (
|
|
1267
|
+
typeof x === 'object' &&
|
|
1268
|
+
this._fields.every(([k, t]) => {
|
|
1269
|
+
// eslint-disable-next-line
|
|
1270
|
+
if (!x.hasOwnProperty(k)) {
|
|
1271
|
+
throw new Error(`Record is missing key "${k}".`);
|
|
1272
|
+
}
|
|
1273
|
+
try {
|
|
1274
|
+
return t.covariant(x[k]);
|
|
1275
|
+
} catch (e: any) {
|
|
1276
|
+
throw new Error(`Invalid ${this.display()} argument: \n\nfield ${k} -> ${e.message}`);
|
|
1277
|
+
}
|
|
1278
|
+
})
|
|
1279
|
+
)
|
|
1280
|
+
return true;
|
|
1281
|
+
|
|
1282
|
+
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
1283
|
+
}
|
|
1284
|
+
|
|
1285
|
+
public encodeValue(x: Record<string, any>): Uint8Array {
|
|
1286
|
+
const values = this._fields.map(([key]) => x[key]);
|
|
1287
|
+
const bufs = zipWith(this._fields, values, ([, c], d) => c.encodeValue(d));
|
|
1288
|
+
return concat(...bufs);
|
|
1289
|
+
}
|
|
1290
|
+
|
|
1291
|
+
public _buildTypeTableImpl(T: TypeTable) {
|
|
1292
|
+
this._fields.forEach(([_, value]) => value.buildTypeTable(T));
|
|
1293
|
+
const opCode = slebEncode(IDLTypeIds.Record);
|
|
1294
|
+
const len = lebEncode(this._fields.length);
|
|
1295
|
+
const fields = this._fields.map(([key, value]) =>
|
|
1296
|
+
concat(lebEncode(idlLabelToId(key)), value.encodeType(T)),
|
|
1297
|
+
);
|
|
1298
|
+
|
|
1299
|
+
T.add(this, concat(opCode, len, concat(...fields)));
|
|
1300
|
+
}
|
|
1301
|
+
|
|
1302
|
+
public decodeValue(b: Pipe, t: Type) {
|
|
1303
|
+
const record = this.checkType(t);
|
|
1304
|
+
if (!(record instanceof RecordClass)) {
|
|
1305
|
+
throw new Error('Not a record type');
|
|
1306
|
+
}
|
|
1307
|
+
const x: Record<string, any> = {};
|
|
1308
|
+
|
|
1309
|
+
let expectedRecordIdx = 0;
|
|
1310
|
+
let actualRecordIdx = 0;
|
|
1311
|
+
while (actualRecordIdx < record._fields.length) {
|
|
1312
|
+
const [hash, type] = record._fields[actualRecordIdx];
|
|
1313
|
+
|
|
1314
|
+
if (expectedRecordIdx >= this._fields.length) {
|
|
1315
|
+
// skip unexpected left over fields present on the wire
|
|
1316
|
+
type.decodeValue(b, type);
|
|
1317
|
+
actualRecordIdx++;
|
|
1318
|
+
continue;
|
|
1319
|
+
}
|
|
1320
|
+
|
|
1321
|
+
const [expectKey, expectType] = this._fields[expectedRecordIdx];
|
|
1322
|
+
const expectedId = idlLabelToId(this._fields[expectedRecordIdx][0]);
|
|
1323
|
+
const actualId = idlLabelToId(hash);
|
|
1324
|
+
if (expectedId === actualId) {
|
|
1325
|
+
// the current field on the wire matches the expected field
|
|
1326
|
+
x[expectKey] = expectType.decodeValue(b, type);
|
|
1327
|
+
expectedRecordIdx++;
|
|
1328
|
+
actualRecordIdx++;
|
|
1329
|
+
} else if (actualId > expectedId) {
|
|
1330
|
+
// The expected field does not exist on the wire
|
|
1331
|
+
if (expectType instanceof OptClass || expectType instanceof ReservedClass) {
|
|
1332
|
+
x[expectKey] = [];
|
|
1333
|
+
expectedRecordIdx++;
|
|
1334
|
+
} else {
|
|
1335
|
+
throw new Error('Cannot find required field ' + expectKey);
|
|
1336
|
+
}
|
|
1337
|
+
} else {
|
|
1338
|
+
// The field on the wire does not exist in the output type, so we can skip it
|
|
1339
|
+
type.decodeValue(b, type);
|
|
1340
|
+
actualRecordIdx++;
|
|
1341
|
+
}
|
|
1342
|
+
}
|
|
1343
|
+
|
|
1344
|
+
// initialize left over expected optional fields
|
|
1345
|
+
for (const [expectKey, expectType] of this._fields.slice(expectedRecordIdx)) {
|
|
1346
|
+
if (expectType instanceof OptClass || expectType instanceof ReservedClass) {
|
|
1347
|
+
// TODO this assumes null value in opt is represented as []
|
|
1348
|
+
x[expectKey] = [];
|
|
1349
|
+
} else {
|
|
1350
|
+
throw new Error('Cannot find required field ' + expectKey);
|
|
1351
|
+
}
|
|
1352
|
+
}
|
|
1353
|
+
return x;
|
|
1354
|
+
}
|
|
1355
|
+
|
|
1356
|
+
get fieldsAsObject(): Record<number, Type> {
|
|
1357
|
+
const fields: Record<number, Type> = {};
|
|
1358
|
+
for (const [name, ty] of this._fields) {
|
|
1359
|
+
fields[idlLabelToId(name)] = ty;
|
|
1360
|
+
}
|
|
1361
|
+
return fields;
|
|
1362
|
+
}
|
|
1363
|
+
|
|
1364
|
+
get name() {
|
|
1365
|
+
const fields = this._fields.map(([key, value]) => key + ':' + value.name);
|
|
1366
|
+
return `record {${fields.join('; ')}}`;
|
|
1367
|
+
}
|
|
1368
|
+
|
|
1369
|
+
public display() {
|
|
1370
|
+
const fields = this._fields.map(([key, value]) => key + ':' + value.display());
|
|
1371
|
+
return `record {${fields.join('; ')}}`;
|
|
1372
|
+
}
|
|
1373
|
+
|
|
1374
|
+
public valueToString(x: Record<string, any>) {
|
|
1375
|
+
const values = this._fields.map(([key]) => x[key]);
|
|
1376
|
+
const fields = zipWith(this._fields, values, ([k, c], d) => k + '=' + c.valueToString(d));
|
|
1377
|
+
return `record {${fields.join('; ')}}`;
|
|
1378
|
+
}
|
|
1379
|
+
}
|
|
1380
|
+
|
|
1381
|
+
/**
|
|
1382
|
+
* Represents Tuple, a syntactic sugar for Record.
|
|
1383
|
+
* @param {Type} components
|
|
1384
|
+
*/
|
|
1385
|
+
export class TupleClass<T extends any[]> extends RecordClass {
|
|
1386
|
+
get typeName() {
|
|
1387
|
+
return IdlTypeName.TupleClass;
|
|
1388
|
+
}
|
|
1389
|
+
|
|
1390
|
+
static [Symbol.hasInstance]<T extends any[]>(instance: any): instance is TupleClass<T> {
|
|
1391
|
+
return instance.typeName === IdlTypeName.TupleClass;
|
|
1392
|
+
}
|
|
1393
|
+
|
|
1394
|
+
protected readonly _components: Type[];
|
|
1395
|
+
|
|
1396
|
+
constructor(_components: Type[]) {
|
|
1397
|
+
const x: Record<string, any> = {};
|
|
1398
|
+
_components.forEach((e, i) => (x['_' + i + '_'] = e));
|
|
1399
|
+
super(x);
|
|
1400
|
+
this._components = _components;
|
|
1401
|
+
}
|
|
1402
|
+
|
|
1403
|
+
public accept<D, R>(v: Visitor<D, R>, d: D): R {
|
|
1404
|
+
return v.visitTuple(this, this._components, d);
|
|
1405
|
+
}
|
|
1406
|
+
|
|
1407
|
+
public covariant(x: any): x is T {
|
|
1408
|
+
// `>=` because tuples can be covariant when encoded.
|
|
1409
|
+
|
|
1410
|
+
if (
|
|
1411
|
+
Array.isArray(x) &&
|
|
1412
|
+
x.length >= this._fields.length &&
|
|
1413
|
+
this._components.every((t, i) => {
|
|
1414
|
+
try {
|
|
1415
|
+
return t.covariant(x[i]);
|
|
1416
|
+
} catch (e: any) {
|
|
1417
|
+
throw new Error(`Invalid ${this.display()} argument: \n\nindex ${i} -> ${e.message}`);
|
|
1418
|
+
}
|
|
1419
|
+
})
|
|
1420
|
+
)
|
|
1421
|
+
return true;
|
|
1422
|
+
|
|
1423
|
+
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
1424
|
+
}
|
|
1425
|
+
|
|
1426
|
+
public encodeValue(x: any[]): Uint8Array {
|
|
1427
|
+
const bufs = zipWith(this._components, x, (c, d) => c.encodeValue(d));
|
|
1428
|
+
return concat(...bufs);
|
|
1429
|
+
}
|
|
1430
|
+
|
|
1431
|
+
public decodeValue(b: Pipe, t: Type): T {
|
|
1432
|
+
const tuple = this.checkType(t);
|
|
1433
|
+
if (!(tuple instanceof TupleClass)) {
|
|
1434
|
+
throw new Error('not a tuple type');
|
|
1435
|
+
}
|
|
1436
|
+
if (tuple._components.length < this._components.length) {
|
|
1437
|
+
throw new Error('tuple mismatch');
|
|
1438
|
+
}
|
|
1439
|
+
const res = [];
|
|
1440
|
+
for (const [i, wireType] of tuple._components.entries()) {
|
|
1441
|
+
if (i >= this._components.length) {
|
|
1442
|
+
// skip value
|
|
1443
|
+
wireType.decodeValue(b, wireType);
|
|
1444
|
+
} else {
|
|
1445
|
+
res.push(this._components[i].decodeValue(b, wireType));
|
|
1446
|
+
}
|
|
1447
|
+
}
|
|
1448
|
+
return res as T;
|
|
1449
|
+
}
|
|
1450
|
+
|
|
1451
|
+
public display() {
|
|
1452
|
+
const fields = this._components.map(value => value.display());
|
|
1453
|
+
return `record {${fields.join('; ')}}`;
|
|
1454
|
+
}
|
|
1455
|
+
|
|
1456
|
+
public valueToString(values: any[]) {
|
|
1457
|
+
const fields = zipWith(this._components, values, (c, d) => c.valueToString(d));
|
|
1458
|
+
return `record {${fields.join('; ')}}`;
|
|
1459
|
+
}
|
|
1460
|
+
}
|
|
1461
|
+
|
|
1462
|
+
/**
|
|
1463
|
+
* Represents an IDL Variant
|
|
1464
|
+
* @param {object} [fields] - mapping of function name to Type
|
|
1465
|
+
*/
|
|
1466
|
+
export class VariantClass extends ConstructType<Record<string, any>> {
|
|
1467
|
+
get typeName() {
|
|
1468
|
+
return IdlTypeName.VariantClass;
|
|
1469
|
+
}
|
|
1470
|
+
|
|
1471
|
+
static [Symbol.hasInstance](instance: any): instance is VariantClass {
|
|
1472
|
+
return instance.typeName === IdlTypeName.VariantClass;
|
|
1473
|
+
}
|
|
1474
|
+
|
|
1475
|
+
public readonly _fields: Array<[string, Type]>;
|
|
1476
|
+
|
|
1477
|
+
constructor(fields: Record<string, Type> = {}) {
|
|
1478
|
+
super();
|
|
1479
|
+
this._fields = Object.entries(fields).sort((a, b) => idlLabelToId(a[0]) - idlLabelToId(b[0]));
|
|
1480
|
+
}
|
|
1481
|
+
|
|
1482
|
+
public accept<D, R>(v: Visitor<D, R>, d: D): R {
|
|
1483
|
+
return v.visitVariant(this, this._fields, d);
|
|
1484
|
+
}
|
|
1485
|
+
|
|
1486
|
+
public covariant(x: any): x is Record<string, any> {
|
|
1487
|
+
if (
|
|
1488
|
+
typeof x === 'object' &&
|
|
1489
|
+
Object.entries(x).length === 1 &&
|
|
1490
|
+
this._fields.every(([k, v]) => {
|
|
1491
|
+
try {
|
|
1492
|
+
// eslint-disable-next-line
|
|
1493
|
+
return !x.hasOwnProperty(k) || v.covariant(x[k]);
|
|
1494
|
+
} catch (e: any) {
|
|
1495
|
+
throw new Error(`Invalid ${this.display()} argument: \n\nvariant ${k} -> ${e.message}`);
|
|
1496
|
+
}
|
|
1497
|
+
})
|
|
1498
|
+
)
|
|
1499
|
+
return true;
|
|
1500
|
+
|
|
1501
|
+
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
1502
|
+
}
|
|
1503
|
+
|
|
1504
|
+
public encodeValue(x: Record<string, any>) {
|
|
1505
|
+
for (let i = 0; i < this._fields.length; i++) {
|
|
1506
|
+
const [name, type] = this._fields[i];
|
|
1507
|
+
// eslint-disable-next-line
|
|
1508
|
+
if (x.hasOwnProperty(name)) {
|
|
1509
|
+
const idx = lebEncode(i);
|
|
1510
|
+
const buf = type.encodeValue(x[name]);
|
|
1511
|
+
|
|
1512
|
+
return concat(idx, buf);
|
|
1513
|
+
}
|
|
1514
|
+
}
|
|
1515
|
+
throw Error('Variant has no data: ' + x);
|
|
1516
|
+
}
|
|
1517
|
+
|
|
1518
|
+
public _buildTypeTableImpl(typeTable: TypeTable) {
|
|
1519
|
+
this._fields.forEach(([, type]) => {
|
|
1520
|
+
type.buildTypeTable(typeTable);
|
|
1521
|
+
});
|
|
1522
|
+
const opCode = slebEncode(IDLTypeIds.Variant);
|
|
1523
|
+
const len = lebEncode(this._fields.length);
|
|
1524
|
+
const fields = this._fields.map(([key, value]) =>
|
|
1525
|
+
concat(lebEncode(idlLabelToId(key)), value.encodeType(typeTable)),
|
|
1526
|
+
);
|
|
1527
|
+
typeTable.add(this, concat(opCode, len, ...fields));
|
|
1528
|
+
}
|
|
1529
|
+
|
|
1530
|
+
public decodeValue(b: Pipe, t: Type) {
|
|
1531
|
+
const variant = this.checkType(t);
|
|
1532
|
+
if (!(variant instanceof VariantClass)) {
|
|
1533
|
+
throw new Error('Not a variant type');
|
|
1534
|
+
}
|
|
1535
|
+
const idx = Number(lebDecode(b));
|
|
1536
|
+
if (idx >= variant._fields.length) {
|
|
1537
|
+
throw Error('Invalid variant index: ' + idx);
|
|
1538
|
+
}
|
|
1539
|
+
const [wireHash, wireType] = variant._fields[idx];
|
|
1540
|
+
for (const [key, expectType] of this._fields) {
|
|
1541
|
+
if (idlLabelToId(wireHash) === idlLabelToId(key)) {
|
|
1542
|
+
const value = expectType.decodeValue(b, wireType);
|
|
1543
|
+
return { [key]: value };
|
|
1544
|
+
}
|
|
1545
|
+
}
|
|
1546
|
+
throw new Error('Cannot find field hash ' + wireHash);
|
|
1547
|
+
}
|
|
1548
|
+
|
|
1549
|
+
get name() {
|
|
1550
|
+
const fields = this._fields.map(([key, type]) => key + ':' + type.name);
|
|
1551
|
+
return `variant {${fields.join('; ')}}`;
|
|
1552
|
+
}
|
|
1553
|
+
|
|
1554
|
+
public display() {
|
|
1555
|
+
const fields = this._fields.map(
|
|
1556
|
+
([key, type]) => key + (type.name === 'null' ? '' : `:${type.display()}`),
|
|
1557
|
+
);
|
|
1558
|
+
return `variant {${fields.join('; ')}}`;
|
|
1559
|
+
}
|
|
1560
|
+
|
|
1561
|
+
public valueToString(x: Record<string, any>) {
|
|
1562
|
+
for (const [name, type] of this._fields) {
|
|
1563
|
+
// eslint-disable-next-line
|
|
1564
|
+
if (x.hasOwnProperty(name)) {
|
|
1565
|
+
const value = type.valueToString(x[name]);
|
|
1566
|
+
if (value === 'null') {
|
|
1567
|
+
return `variant {${name}}`;
|
|
1568
|
+
} else {
|
|
1569
|
+
return `variant {${name}=${value}}`;
|
|
1570
|
+
}
|
|
1571
|
+
}
|
|
1572
|
+
}
|
|
1573
|
+
throw new Error('Variant has no data: ' + x);
|
|
1574
|
+
}
|
|
1575
|
+
|
|
1576
|
+
get alternativesAsObject(): Record<number, Type> {
|
|
1577
|
+
const alternatives: Record<number, Type> = {};
|
|
1578
|
+
for (const [name, ty] of this._fields) {
|
|
1579
|
+
alternatives[idlLabelToId(name)] = ty;
|
|
1580
|
+
}
|
|
1581
|
+
return alternatives;
|
|
1582
|
+
}
|
|
1583
|
+
}
|
|
1584
|
+
|
|
1585
|
+
/**
|
|
1586
|
+
* Represents a reference to an IDL type, used for defining recursive data
|
|
1587
|
+
* types.
|
|
1588
|
+
*/
|
|
1589
|
+
export class RecClass<T = any> extends ConstructType<T> {
|
|
1590
|
+
get typeName() {
|
|
1591
|
+
return IdlTypeName.RecClass;
|
|
1592
|
+
}
|
|
1593
|
+
|
|
1594
|
+
private static _counter = 0;
|
|
1595
|
+
private _id = RecClass._counter++;
|
|
1596
|
+
private _type: ConstructType<T> | undefined;
|
|
1597
|
+
|
|
1598
|
+
static [Symbol.hasInstance](instance: any): instance is RecClass {
|
|
1599
|
+
return instance.typeName === IdlTypeName.RecClass;
|
|
1600
|
+
}
|
|
1601
|
+
|
|
1602
|
+
public accept<D, R>(v: Visitor<D, R>, d: D): R {
|
|
1603
|
+
if (!this._type) {
|
|
1604
|
+
throw Error('Recursive type uninitialized.');
|
|
1605
|
+
}
|
|
1606
|
+
return v.visitRec(this, this._type, d);
|
|
1607
|
+
}
|
|
1608
|
+
|
|
1609
|
+
public fill(t: ConstructType<T>) {
|
|
1610
|
+
this._type = t;
|
|
1611
|
+
}
|
|
1612
|
+
|
|
1613
|
+
public getType() {
|
|
1614
|
+
return this._type;
|
|
1615
|
+
}
|
|
1616
|
+
|
|
1617
|
+
public covariant(x: any): x is T {
|
|
1618
|
+
if (this._type ? this._type.covariant(x) : false) return true;
|
|
1619
|
+
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
1620
|
+
}
|
|
1621
|
+
|
|
1622
|
+
public encodeValue(x: T) {
|
|
1623
|
+
if (!this._type) {
|
|
1624
|
+
throw Error('Recursive type uninitialized.');
|
|
1625
|
+
}
|
|
1626
|
+
return this._type.encodeValue(x);
|
|
1627
|
+
}
|
|
1628
|
+
|
|
1629
|
+
public _buildTypeTableImpl(typeTable: TypeTable) {
|
|
1630
|
+
if (!this._type) {
|
|
1631
|
+
throw Error('Recursive type uninitialized.');
|
|
1632
|
+
}
|
|
1633
|
+
typeTable.add(this, new Uint8Array([]));
|
|
1634
|
+
this._type.buildTypeTable(typeTable);
|
|
1635
|
+
typeTable.merge(this, this._type.name);
|
|
1636
|
+
}
|
|
1637
|
+
|
|
1638
|
+
public decodeValue(b: Pipe, t: Type) {
|
|
1639
|
+
if (!this._type) {
|
|
1640
|
+
throw Error('Recursive type uninitialized.');
|
|
1641
|
+
}
|
|
1642
|
+
return this._type.decodeValue(b, t);
|
|
1643
|
+
}
|
|
1644
|
+
|
|
1645
|
+
get name() {
|
|
1646
|
+
return `rec_${this._id}`;
|
|
1647
|
+
}
|
|
1648
|
+
|
|
1649
|
+
public display() {
|
|
1650
|
+
if (!this._type) {
|
|
1651
|
+
throw Error('Recursive type uninitialized.');
|
|
1652
|
+
}
|
|
1653
|
+
return `μ${this.name}.${this._type.name}`;
|
|
1654
|
+
}
|
|
1655
|
+
|
|
1656
|
+
public valueToString(x: T) {
|
|
1657
|
+
if (!this._type) {
|
|
1658
|
+
throw Error('Recursive type uninitialized.');
|
|
1659
|
+
}
|
|
1660
|
+
return this._type.valueToString(x);
|
|
1661
|
+
}
|
|
1662
|
+
}
|
|
1663
|
+
|
|
1664
|
+
function decodePrincipalId(b: Pipe): PrincipalId {
|
|
1665
|
+
const x = safeReadUint8(b);
|
|
1666
|
+
if (x !== 1) {
|
|
1667
|
+
throw new Error('Cannot decode principal');
|
|
1668
|
+
}
|
|
1669
|
+
|
|
1670
|
+
const len = Number(lebDecode(b));
|
|
1671
|
+
return PrincipalId.fromUint8Array(new Uint8Array(safeRead(b, len)));
|
|
1672
|
+
}
|
|
1673
|
+
|
|
1674
|
+
/**
|
|
1675
|
+
* Represents an IDL principal reference
|
|
1676
|
+
*/
|
|
1677
|
+
export class PrincipalClass extends PrimitiveType<PrincipalId> {
|
|
1678
|
+
get typeName() {
|
|
1679
|
+
return IdlTypeName.PrincipalClass;
|
|
1680
|
+
}
|
|
1681
|
+
|
|
1682
|
+
static [Symbol.hasInstance](instance: any): instance is PrincipalClass {
|
|
1683
|
+
return instance.typeName === IdlTypeName.PrincipalClass;
|
|
1684
|
+
}
|
|
1685
|
+
|
|
1686
|
+
public accept<D, R>(v: Visitor<D, R>, d: D): R {
|
|
1687
|
+
return v.visitPrincipal(this, d);
|
|
1688
|
+
}
|
|
1689
|
+
|
|
1690
|
+
public covariant(x: any): x is PrincipalId {
|
|
1691
|
+
if (x && x._isPrincipal) return true;
|
|
1692
|
+
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
1693
|
+
}
|
|
1694
|
+
|
|
1695
|
+
public encodeValue(x: PrincipalId): Uint8Array {
|
|
1696
|
+
const buf = x.toUint8Array();
|
|
1697
|
+
const len = lebEncode(buf.byteLength);
|
|
1698
|
+
return concat(new Uint8Array([1]), len, buf);
|
|
1699
|
+
}
|
|
1700
|
+
public encodeType(): Uint8Array {
|
|
1701
|
+
return slebEncode(IDLTypeIds.Principal);
|
|
1702
|
+
}
|
|
1703
|
+
|
|
1704
|
+
public decodeValue(b: Pipe, t: Type): PrincipalId {
|
|
1705
|
+
this.checkType(t);
|
|
1706
|
+
return decodePrincipalId(b);
|
|
1707
|
+
}
|
|
1708
|
+
|
|
1709
|
+
get name() {
|
|
1710
|
+
return 'principal';
|
|
1711
|
+
}
|
|
1712
|
+
public valueToString(x: PrincipalId) {
|
|
1713
|
+
return `${this.name} "${x.toText()}"`;
|
|
1714
|
+
}
|
|
1715
|
+
}
|
|
1716
|
+
|
|
1717
|
+
/**
|
|
1718
|
+
* The generic type of the arguments of an {@link Func|IDL Function}.
|
|
1719
|
+
*/
|
|
1720
|
+
export type GenericIdlFuncArgs = [Type, ...Type[]] | [];
|
|
1721
|
+
|
|
1722
|
+
/**
|
|
1723
|
+
* The generic type of the return values of an {@link Func|IDL Function}.
|
|
1724
|
+
*/
|
|
1725
|
+
export type GenericIdlFuncRets = [Type, ...Type[]] | [];
|
|
1726
|
+
|
|
1727
|
+
/**
|
|
1728
|
+
* Represents an IDL function reference.
|
|
1729
|
+
* @param argTypes Argument types.
|
|
1730
|
+
* @param retTypes Return types.
|
|
1731
|
+
* @param annotations Function annotations.
|
|
1732
|
+
*/
|
|
1733
|
+
export class FuncClass<
|
|
1734
|
+
Args extends GenericIdlFuncArgs = GenericIdlFuncArgs,
|
|
1735
|
+
Rets extends GenericIdlFuncRets = GenericIdlFuncRets,
|
|
1736
|
+
> extends ConstructType<[PrincipalId, string]> {
|
|
1737
|
+
get typeName() {
|
|
1738
|
+
return IdlTypeName.FuncClass;
|
|
1739
|
+
}
|
|
1740
|
+
|
|
1741
|
+
static [Symbol.hasInstance](instance: any): instance is FuncClass {
|
|
1742
|
+
return instance.typeName === IdlTypeName.FuncClass;
|
|
1743
|
+
}
|
|
1744
|
+
|
|
1745
|
+
public static argsToString(types: Type[], v: any[]) {
|
|
1746
|
+
if (types.length !== v.length) {
|
|
1747
|
+
throw new Error('arity mismatch');
|
|
1748
|
+
}
|
|
1749
|
+
return '(' + types.map((t, i) => t.valueToString(v[i])).join(', ') + ')';
|
|
1750
|
+
}
|
|
1751
|
+
|
|
1752
|
+
constructor(
|
|
1753
|
+
public argTypes: Args,
|
|
1754
|
+
public retTypes: Rets,
|
|
1755
|
+
public annotations: string[] = [],
|
|
1756
|
+
) {
|
|
1757
|
+
super();
|
|
1758
|
+
}
|
|
1759
|
+
|
|
1760
|
+
public accept<D, R>(v: Visitor<D, R>, d: D): R {
|
|
1761
|
+
return v.visitFunc(this, d);
|
|
1762
|
+
}
|
|
1763
|
+
public covariant(x: any): x is [PrincipalId, string] {
|
|
1764
|
+
if (Array.isArray(x) && x.length === 2 && x[0] && x[0]._isPrincipal && typeof x[1] === 'string')
|
|
1765
|
+
return true;
|
|
1766
|
+
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
1767
|
+
}
|
|
1768
|
+
|
|
1769
|
+
public encodeValue([principal, methodName]: [PrincipalId, string]) {
|
|
1770
|
+
const buf = principal.toUint8Array();
|
|
1771
|
+
const len = lebEncode(buf.byteLength);
|
|
1772
|
+
const canister = concat(new Uint8Array([1]), len, buf);
|
|
1773
|
+
|
|
1774
|
+
const method = new TextEncoder().encode(methodName);
|
|
1775
|
+
const methodLen = lebEncode(method.byteLength);
|
|
1776
|
+
return concat(new Uint8Array([1]), canister, methodLen, method);
|
|
1777
|
+
}
|
|
1778
|
+
|
|
1779
|
+
public _buildTypeTableImpl(T: TypeTable) {
|
|
1780
|
+
this.argTypes.forEach(arg => arg.buildTypeTable(T));
|
|
1781
|
+
this.retTypes.forEach(arg => arg.buildTypeTable(T));
|
|
1782
|
+
|
|
1783
|
+
const opCode = slebEncode(IDLTypeIds.Func);
|
|
1784
|
+
const argLen = lebEncode(this.argTypes.length);
|
|
1785
|
+
const args = concat(...this.argTypes.map(arg => arg.encodeType(T)));
|
|
1786
|
+
const retLen = lebEncode(this.retTypes.length);
|
|
1787
|
+
const rets = concat(...this.retTypes.map(arg => arg.encodeType(T)));
|
|
1788
|
+
const annLen = lebEncode(this.annotations.length);
|
|
1789
|
+
const anns = concat(...this.annotations.map(a => this.encodeAnnotation(a)));
|
|
1790
|
+
|
|
1791
|
+
T.add(this, concat(opCode, argLen, args, retLen, rets, annLen, anns));
|
|
1792
|
+
}
|
|
1793
|
+
|
|
1794
|
+
public decodeValue(b: Pipe, t: Type): [PrincipalId, string] {
|
|
1795
|
+
const tt = t instanceof RecClass ? (t.getType() ?? t) : t;
|
|
1796
|
+
if (!subtype(tt, this)) {
|
|
1797
|
+
throw new Error(
|
|
1798
|
+
`Cannot decode function reference at type ${this.display()} from wire type ${tt.display()}`,
|
|
1799
|
+
);
|
|
1800
|
+
}
|
|
1801
|
+
const x = safeReadUint8(b);
|
|
1802
|
+
if (x !== 1) {
|
|
1803
|
+
throw new Error('Cannot decode function reference');
|
|
1804
|
+
}
|
|
1805
|
+
const canister = decodePrincipalId(b);
|
|
1806
|
+
|
|
1807
|
+
const mLen = Number(lebDecode(b));
|
|
1808
|
+
const buf = safeRead(b, mLen);
|
|
1809
|
+
const decoder = new TextDecoder('utf8', { fatal: true });
|
|
1810
|
+
const method = decoder.decode(buf);
|
|
1811
|
+
|
|
1812
|
+
return [canister, method];
|
|
1813
|
+
}
|
|
1814
|
+
|
|
1815
|
+
get name() {
|
|
1816
|
+
const args = this.argTypes.map(arg => arg.name).join(', ');
|
|
1817
|
+
const rets = this.retTypes.map(arg => arg.name).join(', ');
|
|
1818
|
+
const annon = ' ' + this.annotations.join(' ');
|
|
1819
|
+
return `(${args}) -> (${rets})${annon}`;
|
|
1820
|
+
}
|
|
1821
|
+
|
|
1822
|
+
public valueToString([principal, str]: [PrincipalId, string]) {
|
|
1823
|
+
return `func "${principal.toText()}".${str}`;
|
|
1824
|
+
}
|
|
1825
|
+
|
|
1826
|
+
public display(): string {
|
|
1827
|
+
const args = this.argTypes.map(arg => arg.display()).join(', ');
|
|
1828
|
+
const rets = this.retTypes.map(arg => arg.display()).join(', ');
|
|
1829
|
+
const annon = ' ' + this.annotations.join(' ');
|
|
1830
|
+
return `(${args}) → (${rets})${annon}`;
|
|
1831
|
+
}
|
|
1832
|
+
|
|
1833
|
+
private encodeAnnotation(ann: string): Uint8Array {
|
|
1834
|
+
if (ann === 'query') {
|
|
1835
|
+
return new Uint8Array([1]);
|
|
1836
|
+
} else if (ann === 'oneway') {
|
|
1837
|
+
return new Uint8Array([2]);
|
|
1838
|
+
} else if (ann === 'composite_query') {
|
|
1839
|
+
return new Uint8Array([3]);
|
|
1840
|
+
} else {
|
|
1841
|
+
throw new Error('Illegal function annotation');
|
|
1842
|
+
}
|
|
1843
|
+
}
|
|
1844
|
+
}
|
|
1845
|
+
|
|
1846
|
+
/**
|
|
1847
|
+
* The generic type of the fields of an {@link Service|IDL Service}.
|
|
1848
|
+
*/
|
|
1849
|
+
export type GenericIdlServiceFields = Record<string, FuncClass>;
|
|
1850
|
+
|
|
1851
|
+
export class ServiceClass<
|
|
1852
|
+
K extends string = string,
|
|
1853
|
+
Fields extends GenericIdlServiceFields = GenericIdlServiceFields,
|
|
1854
|
+
> extends ConstructType<PrincipalId> {
|
|
1855
|
+
get typeName() {
|
|
1856
|
+
return IdlTypeName.ServiceClass;
|
|
1857
|
+
}
|
|
1858
|
+
|
|
1859
|
+
static [Symbol.hasInstance](instance: any): instance is ServiceClass {
|
|
1860
|
+
return instance.typeName === IdlTypeName.ServiceClass;
|
|
1861
|
+
}
|
|
1862
|
+
|
|
1863
|
+
public readonly _fields: Array<[K, Fields[K]]>;
|
|
1864
|
+
constructor(fields: Fields) {
|
|
1865
|
+
super();
|
|
1866
|
+
this._fields = Object.entries(fields).sort((a, b) => {
|
|
1867
|
+
if (a[0] < b[0]) {
|
|
1868
|
+
return -1;
|
|
1869
|
+
}
|
|
1870
|
+
if (a[0] > b[0]) {
|
|
1871
|
+
return 1;
|
|
1872
|
+
}
|
|
1873
|
+
return 0;
|
|
1874
|
+
}) as Array<[K, Fields[K]]>;
|
|
1875
|
+
}
|
|
1876
|
+
public accept<D, R>(v: Visitor<D, R>, d: D): R {
|
|
1877
|
+
return v.visitService(this, d);
|
|
1878
|
+
}
|
|
1879
|
+
public covariant(x: any): x is PrincipalId {
|
|
1880
|
+
if (x && x._isPrincipal) return true;
|
|
1881
|
+
throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
|
|
1882
|
+
}
|
|
1883
|
+
|
|
1884
|
+
public encodeValue(x: PrincipalId): Uint8Array {
|
|
1885
|
+
const buf = x.toUint8Array();
|
|
1886
|
+
const len = lebEncode(buf.length);
|
|
1887
|
+
return concat(new Uint8Array([1]), len, buf);
|
|
1888
|
+
}
|
|
1889
|
+
|
|
1890
|
+
public _buildTypeTableImpl(T: TypeTable) {
|
|
1891
|
+
this._fields.forEach(([_, func]) => func.buildTypeTable(T));
|
|
1892
|
+
const opCode = slebEncode(IDLTypeIds.Service);
|
|
1893
|
+
const len = lebEncode(this._fields.length);
|
|
1894
|
+
const meths = this._fields.map(([label, func]) => {
|
|
1895
|
+
const labelBuf = new TextEncoder().encode(label);
|
|
1896
|
+
const labelLen = lebEncode(labelBuf.length);
|
|
1897
|
+
return concat(labelLen, labelBuf, func.encodeType(T));
|
|
1898
|
+
});
|
|
1899
|
+
|
|
1900
|
+
T.add(this, concat(opCode, len, ...meths));
|
|
1901
|
+
}
|
|
1902
|
+
|
|
1903
|
+
public decodeValue(b: Pipe, t: Type): PrincipalId {
|
|
1904
|
+
const tt = t instanceof RecClass ? (t.getType() ?? t) : t;
|
|
1905
|
+
if (!subtype(tt, this)) {
|
|
1906
|
+
throw new Error(
|
|
1907
|
+
`Cannot decode service reference at type ${this.display()} from wire type ${tt.display()}`,
|
|
1908
|
+
);
|
|
1909
|
+
}
|
|
1910
|
+
return decodePrincipalId(b);
|
|
1911
|
+
}
|
|
1912
|
+
get name() {
|
|
1913
|
+
const fields = this._fields.map(([key, value]) => key + ':' + value.name);
|
|
1914
|
+
return `service {${fields.join('; ')}}`;
|
|
1915
|
+
}
|
|
1916
|
+
|
|
1917
|
+
public valueToString(x: PrincipalId) {
|
|
1918
|
+
return `service "${x.toText()}"`;
|
|
1919
|
+
}
|
|
1920
|
+
|
|
1921
|
+
public fieldsAsObject(): Fields {
|
|
1922
|
+
const fields = {} as Fields;
|
|
1923
|
+
for (const [name, ty] of this._fields) {
|
|
1924
|
+
fields[name] = ty;
|
|
1925
|
+
}
|
|
1926
|
+
return fields;
|
|
1927
|
+
}
|
|
1928
|
+
}
|
|
1929
|
+
|
|
1930
|
+
/**
|
|
1931
|
+
* Takes an unknown value and returns a string representation of it.
|
|
1932
|
+
* @param x - unknown value
|
|
1933
|
+
* @returns {string} string representation of the value
|
|
1934
|
+
*/
|
|
1935
|
+
function toReadableString(x: unknown): string {
|
|
1936
|
+
const str = JSON.stringify(x, (_key, value) =>
|
|
1937
|
+
typeof value === 'bigint' ? `BigInt(${value})` : value,
|
|
1938
|
+
);
|
|
1939
|
+
|
|
1940
|
+
return str && str.length > toReadableString_max
|
|
1941
|
+
? str.substring(0, toReadableString_max - 3) + '...'
|
|
1942
|
+
: str;
|
|
1943
|
+
}
|
|
1944
|
+
|
|
1945
|
+
/**
|
|
1946
|
+
* Encode a array of values
|
|
1947
|
+
* @param argTypes - array of Types
|
|
1948
|
+
* @param args - array of values
|
|
1949
|
+
* @returns {Uint8Array} serialised value
|
|
1950
|
+
*/
|
|
1951
|
+
export function encode(argTypes: Array<Type<any>>, args: any[]): Uint8Array {
|
|
1952
|
+
if (args.length < argTypes.length) {
|
|
1953
|
+
throw Error('Wrong number of message arguments');
|
|
1954
|
+
}
|
|
1955
|
+
|
|
1956
|
+
const typeTable = new TypeTable();
|
|
1957
|
+
argTypes.forEach(t => t.buildTypeTable(typeTable));
|
|
1958
|
+
|
|
1959
|
+
const table = typeTable.encode();
|
|
1960
|
+
const len = lebEncode(args.length);
|
|
1961
|
+
const typs = concat(...argTypes.map(t => t.encodeType(typeTable)));
|
|
1962
|
+
const vals = concat(
|
|
1963
|
+
...zipWith(argTypes, args, (t, x) => {
|
|
1964
|
+
try {
|
|
1965
|
+
t.covariant(x);
|
|
1966
|
+
} catch (e: any) {
|
|
1967
|
+
const err = new Error(e.message + '\n\n');
|
|
1968
|
+
throw err;
|
|
1969
|
+
}
|
|
1970
|
+
|
|
1971
|
+
return t.encodeValue(x);
|
|
1972
|
+
}),
|
|
1973
|
+
);
|
|
1974
|
+
|
|
1975
|
+
return concat(magicNumberBytes, table, len, typs, vals);
|
|
1976
|
+
}
|
|
1977
|
+
|
|
1978
|
+
/**
|
|
1979
|
+
* Decode a binary value
|
|
1980
|
+
* @param retTypes - Types expected in the buffer.
|
|
1981
|
+
* @param bytes - hex-encoded string, or buffer.
|
|
1982
|
+
* @returns Value deserialised to JS type
|
|
1983
|
+
*/
|
|
1984
|
+
export function decode(retTypes: Type[], bytes: Uint8Array): JsonValue[] {
|
|
1985
|
+
const b = new Pipe(bytes);
|
|
1986
|
+
|
|
1987
|
+
if (bytes.byteLength < magicNumber.length) {
|
|
1988
|
+
throw new Error('Message length smaller than magic number');
|
|
1989
|
+
}
|
|
1990
|
+
const magicBuffer = safeRead(b, magicNumber.length);
|
|
1991
|
+
const magic = new TextDecoder().decode(magicBuffer);
|
|
1992
|
+
if (magic !== magicNumber) {
|
|
1993
|
+
throw new Error('Wrong magic number: ' + JSON.stringify(magic));
|
|
1994
|
+
}
|
|
1995
|
+
|
|
1996
|
+
function readTypeTable(pipe: Pipe): [Array<[IDLTypeIds, any]>, number[]] {
|
|
1997
|
+
const typeTable: Array<[IDLTypeIds, any]> = [];
|
|
1998
|
+
const len = Number(lebDecode(pipe));
|
|
1999
|
+
|
|
2000
|
+
for (let i = 0; i < len; i++) {
|
|
2001
|
+
const ty = Number(slebDecode(pipe));
|
|
2002
|
+
switch (ty) {
|
|
2003
|
+
case IDLTypeIds.Opt:
|
|
2004
|
+
case IDLTypeIds.Vector: {
|
|
2005
|
+
const t = Number(slebDecode(pipe));
|
|
2006
|
+
typeTable.push([ty, t]);
|
|
2007
|
+
break;
|
|
2008
|
+
}
|
|
2009
|
+
case IDLTypeIds.Record:
|
|
2010
|
+
case IDLTypeIds.Variant: {
|
|
2011
|
+
const fields = [];
|
|
2012
|
+
let objectLength = Number(lebDecode(pipe));
|
|
2013
|
+
let prevHash;
|
|
2014
|
+
while (objectLength--) {
|
|
2015
|
+
const hash = Number(lebDecode(pipe));
|
|
2016
|
+
if (hash >= Math.pow(2, 32)) {
|
|
2017
|
+
throw new Error('field id out of 32-bit range');
|
|
2018
|
+
}
|
|
2019
|
+
if (typeof prevHash === 'number' && prevHash >= hash) {
|
|
2020
|
+
throw new Error('field id collision or not sorted');
|
|
2021
|
+
}
|
|
2022
|
+
prevHash = hash;
|
|
2023
|
+
const t = Number(slebDecode(pipe));
|
|
2024
|
+
fields.push([hash, t]);
|
|
2025
|
+
}
|
|
2026
|
+
typeTable.push([ty, fields]);
|
|
2027
|
+
break;
|
|
2028
|
+
}
|
|
2029
|
+
case IDLTypeIds.Func: {
|
|
2030
|
+
const args = [];
|
|
2031
|
+
let argLength = Number(lebDecode(pipe));
|
|
2032
|
+
while (argLength--) {
|
|
2033
|
+
args.push(Number(slebDecode(pipe)));
|
|
2034
|
+
}
|
|
2035
|
+
const returnValues = [];
|
|
2036
|
+
let returnValuesLength = Number(lebDecode(pipe));
|
|
2037
|
+
while (returnValuesLength--) {
|
|
2038
|
+
returnValues.push(Number(slebDecode(pipe)));
|
|
2039
|
+
}
|
|
2040
|
+
const annotations = [];
|
|
2041
|
+
let annotationLength = Number(lebDecode(pipe));
|
|
2042
|
+
while (annotationLength--) {
|
|
2043
|
+
const annotation = Number(lebDecode(pipe));
|
|
2044
|
+
switch (annotation) {
|
|
2045
|
+
case 1: {
|
|
2046
|
+
annotations.push('query');
|
|
2047
|
+
break;
|
|
2048
|
+
}
|
|
2049
|
+
case 2: {
|
|
2050
|
+
annotations.push('oneway');
|
|
2051
|
+
break;
|
|
2052
|
+
}
|
|
2053
|
+
case 3: {
|
|
2054
|
+
annotations.push('composite_query');
|
|
2055
|
+
break;
|
|
2056
|
+
}
|
|
2057
|
+
default:
|
|
2058
|
+
throw new Error('unknown annotation');
|
|
2059
|
+
}
|
|
2060
|
+
}
|
|
2061
|
+
typeTable.push([ty, [args, returnValues, annotations]]);
|
|
2062
|
+
break;
|
|
2063
|
+
}
|
|
2064
|
+
case IDLTypeIds.Service: {
|
|
2065
|
+
let servLength = Number(lebDecode(pipe));
|
|
2066
|
+
const methods = [];
|
|
2067
|
+
while (servLength--) {
|
|
2068
|
+
const nameLength = Number(lebDecode(pipe));
|
|
2069
|
+
const funcName = new TextDecoder().decode(safeRead(pipe, nameLength));
|
|
2070
|
+
const funcType = slebDecode(pipe);
|
|
2071
|
+
methods.push([funcName, funcType]);
|
|
2072
|
+
}
|
|
2073
|
+
typeTable.push([ty, methods]);
|
|
2074
|
+
break;
|
|
2075
|
+
}
|
|
2076
|
+
default:
|
|
2077
|
+
throw new Error('Illegal op_code: ' + ty);
|
|
2078
|
+
}
|
|
2079
|
+
}
|
|
2080
|
+
|
|
2081
|
+
const rawList: number[] = [];
|
|
2082
|
+
const length = Number(lebDecode(pipe));
|
|
2083
|
+
for (let i = 0; i < length; i++) {
|
|
2084
|
+
rawList.push(Number(slebDecode(pipe)));
|
|
2085
|
+
}
|
|
2086
|
+
return [typeTable, rawList];
|
|
2087
|
+
}
|
|
2088
|
+
const [rawTable, rawTypes] = readTypeTable(b);
|
|
2089
|
+
if (rawTypes.length < retTypes.length) {
|
|
2090
|
+
throw new Error('Wrong number of return values');
|
|
2091
|
+
}
|
|
2092
|
+
|
|
2093
|
+
const table: RecClass[] = rawTable.map(_ => Rec());
|
|
2094
|
+
function getType(t: number): Type {
|
|
2095
|
+
if (t < -24) {
|
|
2096
|
+
throw new Error('future value not supported');
|
|
2097
|
+
}
|
|
2098
|
+
if (t < 0) {
|
|
2099
|
+
switch (t) {
|
|
2100
|
+
case -1:
|
|
2101
|
+
return Null;
|
|
2102
|
+
case -2:
|
|
2103
|
+
return Bool;
|
|
2104
|
+
case -3:
|
|
2105
|
+
return Nat;
|
|
2106
|
+
case -4:
|
|
2107
|
+
return Int;
|
|
2108
|
+
case -5:
|
|
2109
|
+
return Nat8;
|
|
2110
|
+
case -6:
|
|
2111
|
+
return Nat16;
|
|
2112
|
+
case -7:
|
|
2113
|
+
return Nat32;
|
|
2114
|
+
case -8:
|
|
2115
|
+
return Nat64;
|
|
2116
|
+
case -9:
|
|
2117
|
+
return Int8;
|
|
2118
|
+
case -10:
|
|
2119
|
+
return Int16;
|
|
2120
|
+
case -11:
|
|
2121
|
+
return Int32;
|
|
2122
|
+
case -12:
|
|
2123
|
+
return Int64;
|
|
2124
|
+
case -13:
|
|
2125
|
+
return Float32;
|
|
2126
|
+
case -14:
|
|
2127
|
+
return Float64;
|
|
2128
|
+
case -15:
|
|
2129
|
+
return Text;
|
|
2130
|
+
case -16:
|
|
2131
|
+
return Reserved;
|
|
2132
|
+
case -17:
|
|
2133
|
+
return Empty;
|
|
2134
|
+
case -24:
|
|
2135
|
+
return Principal;
|
|
2136
|
+
default:
|
|
2137
|
+
throw new Error('Illegal op_code: ' + t);
|
|
2138
|
+
}
|
|
2139
|
+
}
|
|
2140
|
+
if (t >= rawTable.length) {
|
|
2141
|
+
throw new Error('type index out of range');
|
|
2142
|
+
}
|
|
2143
|
+
return table[t];
|
|
2144
|
+
}
|
|
2145
|
+
function buildType(entry: [IDLTypeIds, any]): Type {
|
|
2146
|
+
switch (entry[0]) {
|
|
2147
|
+
case IDLTypeIds.Vector: {
|
|
2148
|
+
const ty = getType(entry[1]);
|
|
2149
|
+
return Vec(ty);
|
|
2150
|
+
}
|
|
2151
|
+
case IDLTypeIds.Opt: {
|
|
2152
|
+
const ty = getType(entry[1]);
|
|
2153
|
+
return Opt(ty);
|
|
2154
|
+
}
|
|
2155
|
+
case IDLTypeIds.Record: {
|
|
2156
|
+
const fields: Record<string, Type> = {};
|
|
2157
|
+
for (const [hash, ty] of entry[1]) {
|
|
2158
|
+
const name = `_${hash}_`;
|
|
2159
|
+
fields[name] = getType(ty);
|
|
2160
|
+
}
|
|
2161
|
+
const record = Record(fields);
|
|
2162
|
+
const tuple = record.tryAsTuple();
|
|
2163
|
+
if (Array.isArray(tuple)) {
|
|
2164
|
+
return Tuple(...tuple);
|
|
2165
|
+
} else {
|
|
2166
|
+
return record;
|
|
2167
|
+
}
|
|
2168
|
+
}
|
|
2169
|
+
case IDLTypeIds.Variant: {
|
|
2170
|
+
const fields: Record<string, Type> = {};
|
|
2171
|
+
for (const [hash, ty] of entry[1]) {
|
|
2172
|
+
const name = `_${hash}_`;
|
|
2173
|
+
fields[name] = getType(ty);
|
|
2174
|
+
}
|
|
2175
|
+
return Variant(fields);
|
|
2176
|
+
}
|
|
2177
|
+
case IDLTypeIds.Func: {
|
|
2178
|
+
const [args, returnValues, annotations] = entry[1];
|
|
2179
|
+
return Func(
|
|
2180
|
+
args.map((t: number) => getType(t)),
|
|
2181
|
+
returnValues.map((t: number) => getType(t)),
|
|
2182
|
+
annotations,
|
|
2183
|
+
);
|
|
2184
|
+
}
|
|
2185
|
+
case IDLTypeIds.Service: {
|
|
2186
|
+
const rec: Record<string, FuncClass> = {};
|
|
2187
|
+
const methods = entry[1] as [[string, number]];
|
|
2188
|
+
for (const [name, typeRef] of methods) {
|
|
2189
|
+
let type: Type | undefined = getType(typeRef);
|
|
2190
|
+
|
|
2191
|
+
if (type instanceof RecClass) {
|
|
2192
|
+
// unpack reference type
|
|
2193
|
+
type = type.getType();
|
|
2194
|
+
}
|
|
2195
|
+
if (!(type instanceof FuncClass)) {
|
|
2196
|
+
throw new Error('Illegal service definition: services can only contain functions');
|
|
2197
|
+
}
|
|
2198
|
+
rec[name] = type;
|
|
2199
|
+
}
|
|
2200
|
+
return Service(rec);
|
|
2201
|
+
}
|
|
2202
|
+
default:
|
|
2203
|
+
throw new Error('Illegal op_code: ' + entry[0]);
|
|
2204
|
+
}
|
|
2205
|
+
}
|
|
2206
|
+
|
|
2207
|
+
rawTable.forEach((entry, i) => {
|
|
2208
|
+
// Process function type first, so that we can construct the correct service type
|
|
2209
|
+
if (entry[0] === IDLTypeIds.Func) {
|
|
2210
|
+
const t = buildType(entry);
|
|
2211
|
+
table[i].fill(t);
|
|
2212
|
+
}
|
|
2213
|
+
});
|
|
2214
|
+
rawTable.forEach((entry, i) => {
|
|
2215
|
+
if (entry[0] !== IDLTypeIds.Func) {
|
|
2216
|
+
const t = buildType(entry);
|
|
2217
|
+
table[i].fill(t);
|
|
2218
|
+
}
|
|
2219
|
+
});
|
|
2220
|
+
|
|
2221
|
+
resetSubtypeCache();
|
|
2222
|
+
const types = rawTypes.map(t => getType(t));
|
|
2223
|
+
try {
|
|
2224
|
+
const output = retTypes.map((t, i) => {
|
|
2225
|
+
return t.decodeValue(b, types[i]);
|
|
2226
|
+
});
|
|
2227
|
+
|
|
2228
|
+
// skip unused values
|
|
2229
|
+
for (let ind = retTypes.length; ind < types.length; ind++) {
|
|
2230
|
+
types[ind].decodeValue(b, types[ind]);
|
|
2231
|
+
}
|
|
2232
|
+
|
|
2233
|
+
if (b.byteLength > 0) {
|
|
2234
|
+
throw new Error('decode: Left-over bytes');
|
|
2235
|
+
}
|
|
2236
|
+
|
|
2237
|
+
return output;
|
|
2238
|
+
} finally {
|
|
2239
|
+
resetSubtypeCache();
|
|
2240
|
+
}
|
|
2241
|
+
}
|
|
2242
|
+
|
|
2243
|
+
/**
|
|
2244
|
+
* An Interface Factory, normally provided by a Candid code generation.
|
|
2245
|
+
*/
|
|
2246
|
+
export type InterfaceFactory = (idl: {
|
|
2247
|
+
IDL: {
|
|
2248
|
+
Empty: EmptyClass;
|
|
2249
|
+
Reserved: ReservedClass;
|
|
2250
|
+
Unknown: UnknownClass;
|
|
2251
|
+
Bool: BoolClass;
|
|
2252
|
+
Null: NullClass;
|
|
2253
|
+
Text: TextClass;
|
|
2254
|
+
Int: IntClass;
|
|
2255
|
+
Nat: NatClass;
|
|
2256
|
+
|
|
2257
|
+
Float32: FloatClass;
|
|
2258
|
+
Float64: FloatClass;
|
|
2259
|
+
|
|
2260
|
+
Int8: FixedIntClass;
|
|
2261
|
+
Int16: FixedIntClass;
|
|
2262
|
+
Int32: FixedIntClass;
|
|
2263
|
+
Int64: FixedIntClass;
|
|
2264
|
+
|
|
2265
|
+
Nat8: FixedNatClass;
|
|
2266
|
+
Nat16: FixedNatClass;
|
|
2267
|
+
Nat32: FixedNatClass;
|
|
2268
|
+
Nat64: FixedNatClass;
|
|
2269
|
+
|
|
2270
|
+
Principal: PrincipalClass;
|
|
2271
|
+
|
|
2272
|
+
Tuple: typeof Tuple;
|
|
2273
|
+
Vec: typeof Vec;
|
|
2274
|
+
Opt: typeof Opt;
|
|
2275
|
+
Record: typeof Record;
|
|
2276
|
+
Variant: typeof Variant;
|
|
2277
|
+
Rec: typeof Rec;
|
|
2278
|
+
Func: typeof Func;
|
|
2279
|
+
|
|
2280
|
+
Service(t: Record<string, FuncClass>): ServiceClass;
|
|
2281
|
+
};
|
|
2282
|
+
}) => ServiceClass;
|
|
2283
|
+
|
|
2284
|
+
// Export Types instances.
|
|
2285
|
+
export const Empty = new EmptyClass();
|
|
2286
|
+
export const Reserved = new ReservedClass();
|
|
2287
|
+
/**
|
|
2288
|
+
* Client-only type for deserializing unknown data. Not supported by Candid, and its use is discouraged.
|
|
2289
|
+
*/
|
|
2290
|
+
export const Unknown = new UnknownClass();
|
|
2291
|
+
export const Bool = new BoolClass();
|
|
2292
|
+
export const Null = new NullClass();
|
|
2293
|
+
export const Text = new TextClass();
|
|
2294
|
+
export const Int = new IntClass();
|
|
2295
|
+
export const Nat = new NatClass();
|
|
2296
|
+
|
|
2297
|
+
export const Float32 = new FloatClass(32);
|
|
2298
|
+
export const Float64 = new FloatClass(64);
|
|
2299
|
+
|
|
2300
|
+
export const Int8 = new FixedIntClass(8);
|
|
2301
|
+
export const Int16 = new FixedIntClass(16);
|
|
2302
|
+
export const Int32 = new FixedIntClass(32);
|
|
2303
|
+
export const Int64 = new FixedIntClass(64);
|
|
2304
|
+
|
|
2305
|
+
export const Nat8 = new FixedNatClass(8);
|
|
2306
|
+
export const Nat16 = new FixedNatClass(16);
|
|
2307
|
+
export const Nat32 = new FixedNatClass(32);
|
|
2308
|
+
export const Nat64 = new FixedNatClass(64);
|
|
2309
|
+
|
|
2310
|
+
export const Principal = new PrincipalClass();
|
|
2311
|
+
|
|
2312
|
+
/**
|
|
2313
|
+
*
|
|
2314
|
+
* @param types array of any types
|
|
2315
|
+
* @returns TupleClass from those types
|
|
2316
|
+
*/
|
|
2317
|
+
export function Tuple<T extends any[]>(...types: T): TupleClass<T> {
|
|
2318
|
+
return new TupleClass(types);
|
|
2319
|
+
}
|
|
2320
|
+
/**
|
|
2321
|
+
*
|
|
2322
|
+
* @param t IDL Type
|
|
2323
|
+
* @returns VecClass from that type
|
|
2324
|
+
*/
|
|
2325
|
+
export function Vec<T>(t: Type<T>): VecClass<T> {
|
|
2326
|
+
return new VecClass(t);
|
|
2327
|
+
}
|
|
2328
|
+
/**
|
|
2329
|
+
*
|
|
2330
|
+
* @param t IDL Type
|
|
2331
|
+
* @returns OptClass of Type
|
|
2332
|
+
*/
|
|
2333
|
+
export function Opt<T>(t: Type<T>): OptClass<T> {
|
|
2334
|
+
return new OptClass(t);
|
|
2335
|
+
}
|
|
2336
|
+
/**
|
|
2337
|
+
*
|
|
2338
|
+
* @param t Record of string and IDL Type
|
|
2339
|
+
* @returns RecordClass of string and Type
|
|
2340
|
+
*/
|
|
2341
|
+
export function Record(t: Record<string, Type>): RecordClass {
|
|
2342
|
+
return new RecordClass(t);
|
|
2343
|
+
}
|
|
2344
|
+
|
|
2345
|
+
/**
|
|
2346
|
+
*
|
|
2347
|
+
* @param fields Record of string and IDL Type
|
|
2348
|
+
* @returns VariantClass
|
|
2349
|
+
*/
|
|
2350
|
+
export function Variant(fields: Record<string, Type>): VariantClass {
|
|
2351
|
+
return new VariantClass(fields);
|
|
2352
|
+
}
|
|
2353
|
+
/**
|
|
2354
|
+
*
|
|
2355
|
+
* @returns new RecClass
|
|
2356
|
+
*/
|
|
2357
|
+
export function Rec(): RecClass {
|
|
2358
|
+
return new RecClass();
|
|
2359
|
+
}
|
|
2360
|
+
|
|
2361
|
+
/**
|
|
2362
|
+
*
|
|
2363
|
+
* @param args array of IDL Types
|
|
2364
|
+
* @param ret array of IDL Types
|
|
2365
|
+
* @param annotations array of strings, [] by default
|
|
2366
|
+
* @returns new FuncClass
|
|
2367
|
+
*/
|
|
2368
|
+
export function Func<
|
|
2369
|
+
Args extends GenericIdlFuncArgs = GenericIdlFuncArgs,
|
|
2370
|
+
Ret extends GenericIdlFuncRets = GenericIdlFuncRets,
|
|
2371
|
+
>(args: Args, ret: Ret, annotations: string[] = []): FuncClass<Args, Ret> {
|
|
2372
|
+
return new FuncClass(args, ret, annotations);
|
|
2373
|
+
}
|
|
2374
|
+
|
|
2375
|
+
/**
|
|
2376
|
+
*
|
|
2377
|
+
* @param t Record of string and FuncClass
|
|
2378
|
+
* @returns ServiceClass
|
|
2379
|
+
*/
|
|
2380
|
+
export function Service<
|
|
2381
|
+
K extends string = string,
|
|
2382
|
+
Fields extends GenericIdlServiceFields = GenericIdlServiceFields,
|
|
2383
|
+
>(t: Fields): ServiceClass<K, Fields> {
|
|
2384
|
+
return new ServiceClass(t);
|
|
2385
|
+
}
|
|
2386
|
+
|
|
2387
|
+
/**
|
|
2388
|
+
* The list of relations between types we assume to hold. Uses the types .name property as key
|
|
2389
|
+
*/
|
|
2390
|
+
class Relations {
|
|
2391
|
+
private rels: Map<string, Map<string, boolean>>;
|
|
2392
|
+
|
|
2393
|
+
constructor(relations = new Map()) {
|
|
2394
|
+
this.rels = relations;
|
|
2395
|
+
}
|
|
2396
|
+
|
|
2397
|
+
copy(): Relations {
|
|
2398
|
+
const copy = new Map();
|
|
2399
|
+
for (const [key, value] of this.rels.entries()) {
|
|
2400
|
+
const valCopy = new Map(value);
|
|
2401
|
+
copy.set(key, valCopy);
|
|
2402
|
+
}
|
|
2403
|
+
return new Relations(copy);
|
|
2404
|
+
}
|
|
2405
|
+
|
|
2406
|
+
/// Returns whether we know for sure that a relation holds or doesn't (`true` or `false`), or
|
|
2407
|
+
/// if we don't know yet (`undefined`)
|
|
2408
|
+
known(t1: Type, t2: Type): boolean | undefined {
|
|
2409
|
+
return this.rels.get(t1.name)?.get(t2.name);
|
|
2410
|
+
}
|
|
2411
|
+
|
|
2412
|
+
addNegative(t1: Type, t2: Type) {
|
|
2413
|
+
this.addNames(t1.name, t2.name, false);
|
|
2414
|
+
}
|
|
2415
|
+
|
|
2416
|
+
add(t1: Type, t2: Type) {
|
|
2417
|
+
this.addNames(t1.name, t2.name, true);
|
|
2418
|
+
}
|
|
2419
|
+
|
|
2420
|
+
display(): string {
|
|
2421
|
+
let result = '';
|
|
2422
|
+
for (const [t1, v] of this.rels) {
|
|
2423
|
+
for (const [t2, known] of v) {
|
|
2424
|
+
const subty = known ? ':<' : '!<:';
|
|
2425
|
+
result += `${t1} ${subty} ${t2}\n`;
|
|
2426
|
+
}
|
|
2427
|
+
}
|
|
2428
|
+
return result;
|
|
2429
|
+
}
|
|
2430
|
+
|
|
2431
|
+
private addNames(t1: string, t2: string, isSubtype: boolean) {
|
|
2432
|
+
const t1Map = this.rels.get(t1);
|
|
2433
|
+
if (t1Map == undefined) {
|
|
2434
|
+
const newMap = new Map();
|
|
2435
|
+
newMap.set(t2, isSubtype);
|
|
2436
|
+
this.rels.set(t1, newMap);
|
|
2437
|
+
} else {
|
|
2438
|
+
t1Map.set(t2, isSubtype);
|
|
2439
|
+
}
|
|
2440
|
+
}
|
|
2441
|
+
}
|
|
2442
|
+
|
|
2443
|
+
/// `subtypeCache` holds subtyping relations we've previously computed while decoding a message
|
|
2444
|
+
let subtypeCache: Relations = new Relations();
|
|
2445
|
+
|
|
2446
|
+
/** Resets the global subtyping cache */
|
|
2447
|
+
export function resetSubtypeCache() {
|
|
2448
|
+
subtypeCache = new Relations();
|
|
2449
|
+
}
|
|
2450
|
+
|
|
2451
|
+
function eqFunctionAnnotations(t1: FuncClass, t2: FuncClass): boolean {
|
|
2452
|
+
const t1Annotations = new Set(t1.annotations);
|
|
2453
|
+
const t2Annotations = new Set(t2.annotations);
|
|
2454
|
+
if (t1Annotations.size !== t2Annotations.size) {
|
|
2455
|
+
return false;
|
|
2456
|
+
}
|
|
2457
|
+
for (const a of t1Annotations) {
|
|
2458
|
+
if (!t2Annotations.has(a)) return false;
|
|
2459
|
+
}
|
|
2460
|
+
return true;
|
|
2461
|
+
}
|
|
2462
|
+
|
|
2463
|
+
function canBeOmmitted(t: Type) {
|
|
2464
|
+
return t instanceof OptClass || t instanceof NullClass || t instanceof ReservedClass;
|
|
2465
|
+
}
|
|
2466
|
+
|
|
2467
|
+
/**
|
|
2468
|
+
* Subtyping on Candid types t1 <: t2 (Exported for testing)
|
|
2469
|
+
* @param t1 The potential subtype
|
|
2470
|
+
* @param t2 The potential supertype
|
|
2471
|
+
*/
|
|
2472
|
+
export function subtype(t1: Type, t2: Type): boolean {
|
|
2473
|
+
const relations = subtypeCache.copy();
|
|
2474
|
+
const isSubtype = subtype_(relations, t1, t2);
|
|
2475
|
+
if (isSubtype) {
|
|
2476
|
+
subtypeCache.add(t1, t2);
|
|
2477
|
+
} else {
|
|
2478
|
+
subtypeCache.addNegative(t1, t2);
|
|
2479
|
+
}
|
|
2480
|
+
return isSubtype;
|
|
2481
|
+
}
|
|
2482
|
+
|
|
2483
|
+
function subtype_(relations: Relations, t1: Type, t2: Type): boolean {
|
|
2484
|
+
if (t1.name === t2.name) return true;
|
|
2485
|
+
const known = relations.known(t1, t2);
|
|
2486
|
+
if (known !== undefined) return known;
|
|
2487
|
+
relations.add(t1, t2);
|
|
2488
|
+
|
|
2489
|
+
if (t2 instanceof ReservedClass) return true;
|
|
2490
|
+
if (t1 instanceof EmptyClass) return true;
|
|
2491
|
+
if (t1 instanceof NatClass && t2 instanceof IntClass) return true;
|
|
2492
|
+
if (t1 instanceof VecClass && t2 instanceof VecClass)
|
|
2493
|
+
return subtype_(relations, t1._type, t2._type);
|
|
2494
|
+
if (t2 instanceof OptClass) return true;
|
|
2495
|
+
if (t1 instanceof RecordClass && t2 instanceof RecordClass) {
|
|
2496
|
+
const t1Object = t1.fieldsAsObject;
|
|
2497
|
+
for (const [label, ty2] of t2._fields) {
|
|
2498
|
+
const ty1 = t1Object[idlLabelToId(label)];
|
|
2499
|
+
if (!ty1) {
|
|
2500
|
+
if (!canBeOmmitted(ty2)) return false;
|
|
2501
|
+
} else {
|
|
2502
|
+
if (!subtype_(relations, ty1, ty2)) return false;
|
|
2503
|
+
}
|
|
2504
|
+
}
|
|
2505
|
+
return true;
|
|
2506
|
+
}
|
|
2507
|
+
|
|
2508
|
+
if (t1 instanceof FuncClass && t2 instanceof FuncClass) {
|
|
2509
|
+
if (!eqFunctionAnnotations(t1, t2)) return false;
|
|
2510
|
+
for (let i = 0; i < t1.argTypes.length; i++) {
|
|
2511
|
+
const argTy1 = t1.argTypes[i];
|
|
2512
|
+
if (i < t2.argTypes.length) {
|
|
2513
|
+
if (!subtype_(relations, t2.argTypes[i], argTy1)) return false;
|
|
2514
|
+
} else {
|
|
2515
|
+
if (!canBeOmmitted(argTy1)) return false;
|
|
2516
|
+
}
|
|
2517
|
+
}
|
|
2518
|
+
for (let i = 0; i < t2.retTypes.length; i++) {
|
|
2519
|
+
const retTy2 = t2.retTypes[i];
|
|
2520
|
+
if (i < t1.retTypes.length) {
|
|
2521
|
+
if (!subtype_(relations, t1.retTypes[i], retTy2)) return false;
|
|
2522
|
+
} else {
|
|
2523
|
+
if (!canBeOmmitted(retTy2)) return false;
|
|
2524
|
+
}
|
|
2525
|
+
}
|
|
2526
|
+
return true;
|
|
2527
|
+
}
|
|
2528
|
+
|
|
2529
|
+
if (t1 instanceof VariantClass && t2 instanceof VariantClass) {
|
|
2530
|
+
const t2Object = t2.alternativesAsObject;
|
|
2531
|
+
for (const [label, ty1] of t1._fields) {
|
|
2532
|
+
const ty2 = t2Object[idlLabelToId(label)];
|
|
2533
|
+
if (!ty2) return false;
|
|
2534
|
+
if (!subtype_(relations, ty1, ty2)) return false;
|
|
2535
|
+
}
|
|
2536
|
+
return true;
|
|
2537
|
+
}
|
|
2538
|
+
|
|
2539
|
+
if (t1 instanceof ServiceClass && t2 instanceof ServiceClass) {
|
|
2540
|
+
const t1Object = t1.fieldsAsObject();
|
|
2541
|
+
for (const [name, ty2] of t2._fields) {
|
|
2542
|
+
const ty1 = t1Object[name];
|
|
2543
|
+
if (!ty1) return false;
|
|
2544
|
+
if (!subtype_(relations, ty1, ty2)) return false;
|
|
2545
|
+
}
|
|
2546
|
+
return true;
|
|
2547
|
+
}
|
|
2548
|
+
|
|
2549
|
+
if (t1 instanceof RecClass) {
|
|
2550
|
+
return subtype_(relations, t1.getType()!, t2);
|
|
2551
|
+
}
|
|
2552
|
+
if (t2 instanceof RecClass) {
|
|
2553
|
+
return subtype_(relations, t1, t2.getType()!);
|
|
2554
|
+
}
|
|
2555
|
+
return false;
|
|
2556
|
+
}
|