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