@icp-sdk/core 5.3.1 → 6.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/agent/actor.d.ts +7 -3
- package/lib/cjs/agent/actor.d.ts.map +1 -1
- package/lib/cjs/agent/actor.js +18 -5
- package/lib/cjs/agent/actor.js.map +1 -1
- package/lib/cjs/agent/agent/api.d.ts +35 -6
- package/lib/cjs/agent/agent/api.d.ts.map +1 -1
- package/lib/cjs/agent/agent/api.js.map +1 -1
- package/lib/cjs/agent/agent/http/index.d.ts +11 -25
- package/lib/cjs/agent/agent/http/index.d.ts.map +1 -1
- package/lib/cjs/agent/agent/http/index.js +198 -173
- package/lib/cjs/agent/agent/http/index.js.map +1 -1
- package/lib/cjs/agent/auth.d.ts +6 -5
- package/lib/cjs/agent/auth.d.ts.map +1 -1
- package/lib/cjs/agent/auth.js +3 -3
- package/lib/cjs/agent/auth.js.map +1 -1
- package/lib/cjs/agent/canisterStatus/index.d.ts.map +1 -1
- package/lib/cjs/agent/canisterStatus/index.js +10 -10
- package/lib/cjs/agent/canisterStatus/index.js.map +1 -1
- package/lib/cjs/agent/cbor.d.ts +2 -1
- package/lib/cjs/agent/cbor.d.ts.map +1 -1
- package/lib/cjs/agent/cbor.js +4 -1
- package/lib/cjs/agent/cbor.js.map +1 -1
- package/lib/cjs/agent/certificate.d.ts +5 -4
- package/lib/cjs/agent/certificate.d.ts.map +1 -1
- package/lib/cjs/agent/certificate.js +14 -18
- package/lib/cjs/agent/certificate.js.map +1 -1
- package/lib/cjs/agent/der.d.ts +3 -2
- package/lib/cjs/agent/der.d.ts.map +1 -1
- package/lib/cjs/agent/der.js.map +1 -1
- package/lib/cjs/agent/errors.d.ts +7 -1
- package/lib/cjs/agent/errors.d.ts.map +1 -1
- package/lib/cjs/agent/errors.js +34 -16
- package/lib/cjs/agent/errors.js.map +1 -1
- package/lib/cjs/agent/polling/index.d.ts +3 -4
- package/lib/cjs/agent/polling/index.d.ts.map +1 -1
- package/lib/cjs/agent/polling/index.js +17 -10
- package/lib/cjs/agent/polling/index.js.map +1 -1
- package/lib/cjs/agent/polling/strategy.d.ts.map +1 -1
- package/lib/cjs/agent/polling/strategy.js +6 -6
- package/lib/cjs/agent/polling/strategy.js.map +1 -1
- package/lib/cjs/agent/polling/types.d.ts +3 -4
- package/lib/cjs/agent/polling/types.d.ts.map +1 -1
- package/lib/cjs/agent/public_key.d.ts +3 -2
- package/lib/cjs/agent/public_key.d.ts.map +1 -1
- package/lib/cjs/agent/public_key.js +2 -2
- package/lib/cjs/agent/public_key.js.map +1 -1
- package/lib/cjs/agent/request_id.js +10 -10
- package/lib/cjs/agent/request_id.js.map +1 -1
- package/lib/cjs/agent/subnetStatus/index.js +7 -7
- package/lib/cjs/agent/subnetStatus/index.js.map +1 -1
- package/lib/cjs/agent/utils/bls.d.ts.map +1 -1
- package/lib/cjs/agent/utils/bls.js +7 -6
- package/lib/cjs/agent/utils/bls.js.map +1 -1
- package/lib/cjs/agent/utils/readState.d.ts +2 -2
- package/lib/cjs/agent/utils/readState.d.ts.map +1 -1
- package/lib/cjs/agent/utils/readState.js +4 -4
- package/lib/cjs/agent/utils/readState.js.map +1 -1
- package/lib/cjs/agent/utils/target.d.ts +15 -0
- package/lib/cjs/agent/utils/target.d.ts.map +1 -0
- package/lib/cjs/agent/utils/target.js +27 -0
- package/lib/cjs/agent/utils/target.js.map +1 -0
- package/lib/cjs/candid/utils/buffer.d.ts +2 -1
- package/lib/cjs/candid/utils/buffer.d.ts.map +1 -1
- package/lib/cjs/candid/utils/buffer.js +5 -2
- package/lib/cjs/candid/utils/buffer.js.map +1 -1
- package/lib/cjs/identity/identity/attributes.d.ts +6 -2
- package/lib/cjs/identity/identity/attributes.d.ts.map +1 -1
- package/lib/cjs/identity/identity/attributes.js +9 -1
- package/lib/cjs/identity/identity/attributes.js.map +1 -1
- package/lib/cjs/identity/identity/delegation.d.ts +10 -3
- package/lib/cjs/identity/identity/delegation.d.ts.map +1 -1
- package/lib/cjs/identity/identity/delegation.js +25 -12
- package/lib/cjs/identity/identity/delegation.js.map +1 -1
- package/lib/cjs/identity/identity/ecdsa.d.ts +2 -2
- package/lib/cjs/identity/identity/ecdsa.d.ts.map +1 -1
- package/lib/cjs/identity/identity/ecdsa.js +3 -2
- package/lib/cjs/identity/identity/ecdsa.js.map +1 -1
- package/lib/cjs/identity/identity/ed25519.d.ts +3 -2
- package/lib/cjs/identity/identity/ed25519.d.ts.map +1 -1
- package/lib/cjs/identity/identity/ed25519.js +18 -15
- package/lib/cjs/identity/identity/ed25519.js.map +1 -1
- package/lib/cjs/identity/identity/webauthn.d.ts +4 -3
- package/lib/cjs/identity/identity/webauthn.d.ts.map +1 -1
- package/lib/cjs/identity/identity/webauthn.js +12 -11
- package/lib/cjs/identity/identity/webauthn.js.map +1 -1
- package/lib/cjs/identity/secp256k1/der.d.ts +2 -1
- package/lib/cjs/identity/secp256k1/der.d.ts.map +1 -1
- package/lib/cjs/identity/secp256k1/der.js.map +1 -1
- package/lib/cjs/identity/secp256k1/pem.d.ts +4 -3
- package/lib/cjs/identity/secp256k1/pem.d.ts.map +1 -1
- package/lib/cjs/identity/secp256k1/pem.js +2 -2
- package/lib/cjs/identity/secp256k1/pem.js.map +1 -1
- package/lib/cjs/identity/secp256k1/secp256k1.d.ts +5 -4
- package/lib/cjs/identity/secp256k1/secp256k1.d.ts.map +1 -1
- package/lib/cjs/identity/secp256k1/secp256k1.js +17 -20
- package/lib/cjs/identity/secp256k1/secp256k1.js.map +1 -1
- package/lib/cjs/principal/principal.js +5 -5
- package/lib/cjs/principal/principal.js.map +1 -1
- package/lib/esm/agent/actor.d.ts +7 -3
- package/lib/esm/agent/actor.d.ts.map +1 -1
- package/lib/esm/agent/actor.js +18 -5
- package/lib/esm/agent/actor.js.map +1 -1
- package/lib/esm/agent/agent/api.d.ts +35 -6
- package/lib/esm/agent/agent/api.d.ts.map +1 -1
- package/lib/esm/agent/agent/api.js.map +1 -1
- package/lib/esm/agent/agent/http/index.d.ts +11 -25
- package/lib/esm/agent/agent/http/index.d.ts.map +1 -1
- package/lib/esm/agent/agent/http/index.js +191 -166
- package/lib/esm/agent/agent/http/index.js.map +1 -1
- package/lib/esm/agent/auth.d.ts +6 -5
- package/lib/esm/agent/auth.d.ts.map +1 -1
- package/lib/esm/agent/auth.js +1 -1
- package/lib/esm/agent/auth.js.map +1 -1
- package/lib/esm/agent/canister-env/index.js +1 -1
- package/lib/esm/agent/canister-env/index.js.map +1 -1
- package/lib/esm/agent/canisterStatus/index.d.ts.map +1 -1
- package/lib/esm/agent/canisterStatus/index.js +2 -2
- package/lib/esm/agent/canisterStatus/index.js.map +1 -1
- package/lib/esm/agent/cbor.d.ts +2 -1
- package/lib/esm/agent/cbor.d.ts.map +1 -1
- package/lib/esm/agent/cbor.js +4 -1
- package/lib/esm/agent/cbor.js.map +1 -1
- package/lib/esm/agent/certificate.d.ts +5 -4
- package/lib/esm/agent/certificate.d.ts.map +1 -1
- package/lib/esm/agent/certificate.js +5 -9
- package/lib/esm/agent/certificate.js.map +1 -1
- package/lib/esm/agent/der.d.ts +3 -2
- package/lib/esm/agent/der.d.ts.map +1 -1
- package/lib/esm/agent/der.js.map +1 -1
- package/lib/esm/agent/errors.d.ts +7 -1
- package/lib/esm/agent/errors.d.ts.map +1 -1
- package/lib/esm/agent/errors.js +18 -1
- package/lib/esm/agent/errors.js.map +1 -1
- package/lib/esm/agent/polling/index.d.ts +3 -4
- package/lib/esm/agent/polling/index.d.ts.map +1 -1
- package/lib/esm/agent/polling/index.js +15 -8
- package/lib/esm/agent/polling/index.js.map +1 -1
- package/lib/esm/agent/polling/strategy.d.ts.map +1 -1
- package/lib/esm/agent/polling/strategy.js +6 -6
- package/lib/esm/agent/polling/strategy.js.map +1 -1
- package/lib/esm/agent/polling/types.d.ts +3 -4
- package/lib/esm/agent/polling/types.d.ts.map +1 -1
- package/lib/esm/agent/public_key.d.ts +3 -2
- package/lib/esm/agent/public_key.d.ts.map +1 -1
- package/lib/esm/agent/public_key.js +2 -2
- package/lib/esm/agent/public_key.js.map +1 -1
- package/lib/esm/agent/request_id.js +2 -2
- package/lib/esm/agent/request_id.js.map +1 -1
- package/lib/esm/agent/subnetStatus/index.js +1 -1
- package/lib/esm/agent/subnetStatus/index.js.map +1 -1
- package/lib/esm/agent/utils/bls.d.ts.map +1 -1
- package/lib/esm/agent/utils/bls.js +7 -6
- package/lib/esm/agent/utils/bls.js.map +1 -1
- package/lib/esm/agent/utils/readState.d.ts +2 -2
- package/lib/esm/agent/utils/readState.d.ts.map +1 -1
- package/lib/esm/agent/utils/readState.js +1 -1
- package/lib/esm/agent/utils/readState.js.map +1 -1
- package/lib/esm/agent/utils/target.d.ts +15 -0
- package/lib/esm/agent/utils/target.d.ts.map +1 -0
- package/lib/esm/agent/utils/target.js +23 -0
- package/lib/esm/agent/utils/target.js.map +1 -0
- package/lib/esm/candid/utils/buffer.d.ts +2 -1
- package/lib/esm/candid/utils/buffer.d.ts.map +1 -1
- package/lib/esm/candid/utils/buffer.js +5 -2
- package/lib/esm/candid/utils/buffer.js.map +1 -1
- package/lib/esm/identity/identity/attributes.d.ts +6 -2
- package/lib/esm/identity/identity/attributes.d.ts.map +1 -1
- package/lib/esm/identity/identity/attributes.js +9 -1
- package/lib/esm/identity/identity/attributes.js.map +1 -1
- package/lib/esm/identity/identity/delegation.d.ts +10 -3
- package/lib/esm/identity/identity/delegation.d.ts.map +1 -1
- package/lib/esm/identity/identity/delegation.js +22 -9
- package/lib/esm/identity/identity/delegation.js.map +1 -1
- package/lib/esm/identity/identity/ecdsa.d.ts +2 -2
- package/lib/esm/identity/identity/ecdsa.d.ts.map +1 -1
- package/lib/esm/identity/identity/ecdsa.js +3 -2
- package/lib/esm/identity/identity/ecdsa.js.map +1 -1
- package/lib/esm/identity/identity/ed25519.d.ts +3 -2
- package/lib/esm/identity/identity/ed25519.d.ts.map +1 -1
- package/lib/esm/identity/identity/ed25519.js +10 -7
- package/lib/esm/identity/identity/ed25519.js.map +1 -1
- package/lib/esm/identity/identity/webauthn.d.ts +4 -3
- package/lib/esm/identity/identity/webauthn.d.ts.map +1 -1
- package/lib/esm/identity/identity/webauthn.js +8 -7
- package/lib/esm/identity/identity/webauthn.js.map +1 -1
- package/lib/esm/identity/secp256k1/der.d.ts +2 -1
- package/lib/esm/identity/secp256k1/der.d.ts.map +1 -1
- package/lib/esm/identity/secp256k1/der.js.map +1 -1
- package/lib/esm/identity/secp256k1/pem.d.ts +4 -3
- package/lib/esm/identity/secp256k1/pem.d.ts.map +1 -1
- package/lib/esm/identity/secp256k1/pem.js +2 -2
- package/lib/esm/identity/secp256k1/pem.js.map +1 -1
- package/lib/esm/identity/secp256k1/secp256k1.d.ts +5 -4
- package/lib/esm/identity/secp256k1/secp256k1.d.ts.map +1 -1
- package/lib/esm/identity/secp256k1/secp256k1.js +10 -13
- package/lib/esm/identity/secp256k1/secp256k1.js.map +1 -1
- package/lib/esm/principal/principal.js +2 -2
- package/lib/esm/principal/principal.js.map +1 -1
- package/package.json +20 -10
- package/src/agent/actor.ts +0 -524
- package/src/agent/agent/api.ts +0 -292
- package/src/agent/agent/http/expiry.ts +0 -107
- package/src/agent/agent/http/index.ts +0 -1727
- package/src/agent/agent/http/transforms.ts +0 -54
- package/src/agent/agent/http/types.ts +0 -145
- package/src/agent/agent/index.ts +0 -4
- package/src/agent/auth.ts +0 -147
- package/src/agent/canister-env/index.ts +0 -192
- package/src/agent/canisterStatus/index.ts +0 -260
- package/src/agent/cbor.ts +0 -63
- package/src/agent/certificate.ts +0 -1058
- package/src/agent/constants.ts +0 -24
- package/src/agent/der.ts +0 -191
- package/src/agent/errors.ts +0 -962
- package/src/agent/fetch_candid.ts +0 -37
- package/src/agent/index.ts +0 -130
- package/src/agent/observable.ts +0 -49
- package/src/agent/polling/backoff.ts +0 -129
- package/src/agent/polling/index.ts +0 -238
- package/src/agent/polling/strategy.ts +0 -140
- package/src/agent/polling/types.ts +0 -28
- package/src/agent/public_key.ts +0 -65
- package/src/agent/request_id.ts +0 -99
- package/src/agent/subnetStatus/index.ts +0 -263
- package/src/agent/utils/bls.ts +0 -18
- package/src/agent/utils/buffer.ts +0 -64
- package/src/agent/utils/certificateReject.ts +0 -25
- package/src/agent/utils/createExpirableStore.ts +0 -31
- package/src/agent/utils/expirableStore.ts +0 -47
- package/src/agent/utils/inMemoryExpirableStore.ts +0 -49
- package/src/agent/utils/indexedDBExpirableStore.ts +0 -191
- package/src/agent/utils/leb.ts +0 -14
- package/src/agent/utils/random.ts +0 -30
- package/src/agent/utils/readState.ts +0 -180
- package/src/candid/candid-core.ts +0 -274
- package/src/candid/candid-ui.ts +0 -281
- package/src/candid/idl.ts +0 -2654
- package/src/candid/index.ts +0 -35
- package/src/candid/types.ts +0 -7
- package/src/candid/utils/bigint-math.ts +0 -26
- package/src/candid/utils/buffer.ts +0 -221
- package/src/candid/utils/hash.ts +0 -31
- package/src/candid/utils/leb128.ts +0 -233
- package/src/identity/identity/attributes.ts +0 -84
- package/src/identity/identity/delegation.ts +0 -411
- package/src/identity/identity/ecdsa.ts +0 -162
- package/src/identity/identity/ed25519.ts +0 -246
- package/src/identity/identity/partial.ts +0 -60
- package/src/identity/identity/webauthn.ts +0 -265
- package/src/identity/index.ts +0 -44
- package/src/identity/secp256k1/der.ts +0 -177
- package/src/identity/secp256k1/index.ts +0 -52
- package/src/identity/secp256k1/pem.ts +0 -39
- package/src/identity/secp256k1/secp256k1.ts +0 -276
- package/src/index.ts +0 -3
- package/src/principal/index.ts +0 -19
- package/src/principal/principal.ts +0 -181
- package/src/principal/utils/base32.ts +0 -101
- package/src/principal/utils/getCrc.ts +0 -52
|
@@ -1,181 +0,0 @@
|
|
|
1
|
-
import { base32Decode, base32Encode } from './utils/base32.ts';
|
|
2
|
-
import { getCrc32 } from './utils/getCrc.ts';
|
|
3
|
-
import { sha224 } from '@noble/hashes/sha2';
|
|
4
|
-
import { bytesToHex, hexToBytes } from '@noble/hashes/utils';
|
|
5
|
-
|
|
6
|
-
export const JSON_KEY_PRINCIPAL = '__principal__';
|
|
7
|
-
const SELF_AUTHENTICATING_SUFFIX = 2;
|
|
8
|
-
const ANONYMOUS_SUFFIX = 4;
|
|
9
|
-
|
|
10
|
-
const MANAGEMENT_CANISTER_PRINCIPAL_TEXT_STR = 'aaaaa-aa';
|
|
11
|
-
|
|
12
|
-
export interface JsonnablePrincipal {
|
|
13
|
-
[JSON_KEY_PRINCIPAL]: string;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export class Principal {
|
|
17
|
-
public static anonymous(): Principal {
|
|
18
|
-
return new this(new Uint8Array([ANONYMOUS_SUFFIX]));
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Utility method, returning the principal representing the management canister, decoded from the hex string `'aaaaa-aa'`
|
|
23
|
-
* @returns {Principal} principal of the management canister
|
|
24
|
-
*/
|
|
25
|
-
public static managementCanister(): Principal {
|
|
26
|
-
return this.fromText(MANAGEMENT_CANISTER_PRINCIPAL_TEXT_STR);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
public static selfAuthenticating(publicKey: Uint8Array): Principal {
|
|
30
|
-
const sha = sha224(publicKey);
|
|
31
|
-
return new this(new Uint8Array([...sha, SELF_AUTHENTICATING_SUFFIX]));
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
public static from(other: unknown): Principal {
|
|
35
|
-
if (typeof other === 'string') {
|
|
36
|
-
return Principal.fromText(other);
|
|
37
|
-
}
|
|
38
|
-
if (Object.getPrototypeOf(other) === Uint8Array.prototype) {
|
|
39
|
-
return new Principal(other as Uint8Array);
|
|
40
|
-
}
|
|
41
|
-
if (Principal.isPrincipal(other)) {
|
|
42
|
-
return new Principal(other._arr);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
throw new Error(`Impossible to convert ${JSON.stringify(other)} to Principal.`);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
public static fromHex(hex: string): Principal {
|
|
49
|
-
return new this(hexToBytes(hex));
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
public static fromText(text: string): Principal {
|
|
53
|
-
let maybePrincipal = text;
|
|
54
|
-
// If formatted as JSON string, parse it first
|
|
55
|
-
if (text.includes(JSON_KEY_PRINCIPAL)) {
|
|
56
|
-
const obj = JSON.parse(text);
|
|
57
|
-
if (JSON_KEY_PRINCIPAL in obj) {
|
|
58
|
-
maybePrincipal = obj[JSON_KEY_PRINCIPAL];
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
const canisterIdNoDash = maybePrincipal.toLowerCase().replace(/-/g, '');
|
|
63
|
-
|
|
64
|
-
let arr = base32Decode(canisterIdNoDash);
|
|
65
|
-
arr = arr.slice(4, arr.length);
|
|
66
|
-
|
|
67
|
-
const principal = new this(arr);
|
|
68
|
-
if (principal.toText() !== maybePrincipal) {
|
|
69
|
-
throw new Error(
|
|
70
|
-
`Principal "${principal.toText()}" does not have a valid checksum (original value "${maybePrincipal}" may not be a valid Principal ID).`,
|
|
71
|
-
);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
return principal;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
public static fromUint8Array(arr: Uint8Array): Principal {
|
|
78
|
-
return new this(arr);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
public static isPrincipal(other: unknown): other is Principal {
|
|
82
|
-
return (
|
|
83
|
-
other instanceof Principal ||
|
|
84
|
-
(typeof other === 'object' &&
|
|
85
|
-
other !== null &&
|
|
86
|
-
'_isPrincipal' in other &&
|
|
87
|
-
(other as { _isPrincipal: boolean })['_isPrincipal'] === true &&
|
|
88
|
-
'_arr' in other &&
|
|
89
|
-
(other as { _arr: Uint8Array })['_arr'] instanceof Uint8Array)
|
|
90
|
-
);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
public readonly _isPrincipal = true;
|
|
94
|
-
|
|
95
|
-
protected constructor(private _arr: Uint8Array) {}
|
|
96
|
-
|
|
97
|
-
public isAnonymous(): boolean {
|
|
98
|
-
return this._arr.byteLength === 1 && this._arr[0] === ANONYMOUS_SUFFIX;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
public toUint8Array(): Uint8Array {
|
|
102
|
-
return this._arr;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
public toHex(): string {
|
|
106
|
-
return bytesToHex(this._arr).toUpperCase();
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
public toText(): string {
|
|
110
|
-
const checksumArrayBuf = new ArrayBuffer(4);
|
|
111
|
-
const view = new DataView(checksumArrayBuf);
|
|
112
|
-
view.setUint32(0, getCrc32(this._arr));
|
|
113
|
-
const checksum = new Uint8Array(checksumArrayBuf);
|
|
114
|
-
|
|
115
|
-
const array = new Uint8Array([...checksum, ...this._arr]);
|
|
116
|
-
|
|
117
|
-
const result = base32Encode(array);
|
|
118
|
-
const matches = result.match(/.{1,5}/g);
|
|
119
|
-
if (!matches) {
|
|
120
|
-
// This should only happen if there's no character, which is unreachable.
|
|
121
|
-
throw new Error();
|
|
122
|
-
}
|
|
123
|
-
return matches.join('-');
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
public toString(): string {
|
|
127
|
-
return this.toText();
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Serializes to JSON
|
|
132
|
-
* @returns {JsonnablePrincipal} a JSON object with a single key, {@link JSON_KEY_PRINCIPAL}, whose value is the principal as a string
|
|
133
|
-
*/
|
|
134
|
-
public toJSON(): JsonnablePrincipal {
|
|
135
|
-
return { [JSON_KEY_PRINCIPAL]: this.toText() };
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* Utility method taking a Principal to compare against. Used for determining canister ranges in certificate verification
|
|
140
|
-
* @param {Principal} other - a {@link Principal} to compare
|
|
141
|
-
* @returns {'lt' | 'eq' | 'gt'} `'lt' | 'eq' | 'gt'` a string, representing less than, equal to, or greater than
|
|
142
|
-
*/
|
|
143
|
-
public compareTo(other: Principal): 'lt' | 'eq' | 'gt' {
|
|
144
|
-
for (let i = 0; i < Math.min(this._arr.length, other._arr.length); i++) {
|
|
145
|
-
if (this._arr[i] < other._arr[i]) {
|
|
146
|
-
return 'lt';
|
|
147
|
-
}
|
|
148
|
-
if (this._arr[i] > other._arr[i]) {
|
|
149
|
-
return 'gt';
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
// Here, at least one principal is a prefix of the other principal (they could be the same)
|
|
153
|
-
if (this._arr.length < other._arr.length) {
|
|
154
|
-
return 'lt';
|
|
155
|
-
}
|
|
156
|
-
if (this._arr.length > other._arr.length) {
|
|
157
|
-
return 'gt';
|
|
158
|
-
}
|
|
159
|
-
return 'eq';
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* Utility method checking whether a provided Principal is less than or equal to the current one using the {@link Principal.compareTo} method
|
|
164
|
-
* @param other a {@link Principal} to compare
|
|
165
|
-
* @returns {boolean} boolean
|
|
166
|
-
*/
|
|
167
|
-
public ltEq(other: Principal): boolean {
|
|
168
|
-
const cmp = this.compareTo(other);
|
|
169
|
-
return cmp == 'lt' || cmp == 'eq';
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* Utility method checking whether a provided Principal is greater than or equal to the current one using the {@link Principal.compareTo} method
|
|
174
|
-
* @param other a {@link Principal} to compare
|
|
175
|
-
* @returns {boolean} boolean
|
|
176
|
-
*/
|
|
177
|
-
public gtEq(other: Principal): boolean {
|
|
178
|
-
const cmp = this.compareTo(other);
|
|
179
|
-
return cmp == 'gt' || cmp == 'eq';
|
|
180
|
-
}
|
|
181
|
-
}
|
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
const alphabet = 'abcdefghijklmnopqrstuvwxyz234567';
|
|
2
|
-
|
|
3
|
-
// Build a lookup table for decoding.
|
|
4
|
-
const lookupTable: Record<string, number> = Object.create(null);
|
|
5
|
-
for (let i = 0; i < alphabet.length; i++) {
|
|
6
|
-
lookupTable[alphabet[i]] = i;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
// Add aliases for rfc4648.
|
|
10
|
-
lookupTable['0'] = lookupTable.o;
|
|
11
|
-
lookupTable['1'] = lookupTable.i;
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* @param input The Uint8Array to encode.
|
|
15
|
-
* @returns A Base32 string encoding the input.
|
|
16
|
-
*/
|
|
17
|
-
export function base32Encode(input: Uint8Array): string {
|
|
18
|
-
// How many bits will we skip from the first byte.
|
|
19
|
-
let skip = 0;
|
|
20
|
-
// 5 high bits, carry from one byte to the next.
|
|
21
|
-
let bits = 0;
|
|
22
|
-
|
|
23
|
-
// The output string in base32.
|
|
24
|
-
let output = '';
|
|
25
|
-
|
|
26
|
-
function encodeByte(byte: number) {
|
|
27
|
-
if (skip < 0) {
|
|
28
|
-
// we have a carry from the previous byte
|
|
29
|
-
bits |= byte >> -skip;
|
|
30
|
-
} else {
|
|
31
|
-
// no carry
|
|
32
|
-
bits = (byte << skip) & 248;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
if (skip > 3) {
|
|
36
|
-
// Not enough data to produce a character, get us another one
|
|
37
|
-
skip -= 8;
|
|
38
|
-
return 1;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
if (skip < 4) {
|
|
42
|
-
// produce a character
|
|
43
|
-
output += alphabet[bits >> 3];
|
|
44
|
-
skip += 5;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
return 0;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
for (let i = 0; i < input.length; ) {
|
|
51
|
-
i += encodeByte(input[i]);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
return output + (skip < 0 ? alphabet[bits >> 3] : '');
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* @param input The base32 encoded string to decode.
|
|
59
|
-
*/
|
|
60
|
-
export function base32Decode(input: string): Uint8Array {
|
|
61
|
-
// how many bits we have from the previous character.
|
|
62
|
-
let skip = 0;
|
|
63
|
-
// current byte we're producing.
|
|
64
|
-
let byte = 0;
|
|
65
|
-
|
|
66
|
-
const output = new Uint8Array(((input.length * 4) / 3) | 0);
|
|
67
|
-
let o = 0;
|
|
68
|
-
|
|
69
|
-
function decodeChar(char: string) {
|
|
70
|
-
// Consume a character from the stream, store
|
|
71
|
-
// the output in this.output. As before, better
|
|
72
|
-
// to use update().
|
|
73
|
-
let val = lookupTable[char.toLowerCase()];
|
|
74
|
-
if (val === undefined) {
|
|
75
|
-
throw new Error(`Invalid character: ${JSON.stringify(char)}`);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// move to the high bits
|
|
79
|
-
val <<= 3;
|
|
80
|
-
byte |= val >>> skip;
|
|
81
|
-
skip += 5;
|
|
82
|
-
|
|
83
|
-
if (skip >= 8) {
|
|
84
|
-
// We have enough bytes to produce an output
|
|
85
|
-
output[o++] = byte;
|
|
86
|
-
skip -= 8;
|
|
87
|
-
|
|
88
|
-
if (skip > 0) {
|
|
89
|
-
byte = (val << (5 - skip)) & 255;
|
|
90
|
-
} else {
|
|
91
|
-
byte = 0;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
for (const c of input) {
|
|
97
|
-
decodeChar(c);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
return output.slice(0, o);
|
|
101
|
-
}
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
// This file is translated to JavaScript from
|
|
2
|
-
// https://lxp32.github.io/docs/a-simple-example-crc32-calculation/
|
|
3
|
-
const lookUpTable: Uint32Array = new Uint32Array([
|
|
4
|
-
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
|
|
5
|
-
0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91,
|
|
6
|
-
0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
|
|
7
|
-
0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5,
|
|
8
|
-
0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
|
|
9
|
-
0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
|
|
10
|
-
0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f,
|
|
11
|
-
0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d,
|
|
12
|
-
0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
|
|
13
|
-
0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
|
|
14
|
-
0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457,
|
|
15
|
-
0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
|
|
16
|
-
0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb,
|
|
17
|
-
0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9,
|
|
18
|
-
0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
|
|
19
|
-
0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad,
|
|
20
|
-
0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683,
|
|
21
|
-
0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
|
|
22
|
-
0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7,
|
|
23
|
-
0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
|
|
24
|
-
0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
|
|
25
|
-
0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79,
|
|
26
|
-
0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f,
|
|
27
|
-
0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
|
|
28
|
-
0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
|
|
29
|
-
0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21,
|
|
30
|
-
0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
|
|
31
|
-
0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45,
|
|
32
|
-
0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db,
|
|
33
|
-
0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
|
|
34
|
-
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
|
|
35
|
-
0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d,
|
|
36
|
-
]);
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Calculate the CRC32 of a Uint8Array.
|
|
40
|
-
* @param buf The Uint8Array to calculate the CRC32 of.
|
|
41
|
-
*/
|
|
42
|
-
export function getCrc32(buf: Uint8Array): number {
|
|
43
|
-
let crc = -1;
|
|
44
|
-
|
|
45
|
-
for (let i = 0; i < buf.length; i++) {
|
|
46
|
-
const byte = buf[i];
|
|
47
|
-
const t = (byte ^ crc) & 0xff;
|
|
48
|
-
crc = lookUpTable[t] ^ (crc >>> 8);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
return (crc ^ -1) >>> 0;
|
|
52
|
-
}
|