@astrox/agent 0.0.24 → 0.0.30

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.
Files changed (133) hide show
  1. package/lib/cjs/actor.d.ts +126 -0
  2. package/lib/cjs/actor.js +207 -0
  3. package/lib/cjs/actor.js.map +1 -0
  4. package/lib/cjs/agent/api.d.ts +140 -0
  5. package/lib/cjs/agent/api.js +16 -0
  6. package/lib/cjs/agent/api.js.map +1 -0
  7. package/lib/cjs/agent/http/index.d.ts +49 -0
  8. package/lib/cjs/agent/http/index.js +355 -0
  9. package/lib/cjs/agent/http/index.js.map +1 -0
  10. package/lib/cjs/agent/http/transforms.d.ts +20 -0
  11. package/lib/cjs/agent/http/transforms.js +70 -0
  12. package/lib/cjs/agent/http/transforms.js.map +1 -0
  13. package/lib/cjs/agent/http/types.d.ts +77 -0
  14. package/lib/cjs/agent/http/types.js +24 -0
  15. package/lib/cjs/agent/http/types.js.map +1 -0
  16. package/lib/cjs/agent/index.d.ts +7 -0
  17. package/lib/cjs/agent/index.js +42 -0
  18. package/lib/cjs/agent/index.js.map +1 -0
  19. package/lib/cjs/agent/proxy.d.ts +85 -0
  20. package/lib/cjs/agent/proxy.js +159 -0
  21. package/lib/cjs/agent/proxy.js.map +1 -0
  22. package/lib/cjs/auth.d.ts +87 -0
  23. package/lib/cjs/auth.js +70 -0
  24. package/lib/cjs/auth.js.map +1 -0
  25. package/lib/cjs/canisters/asset.d.ts +10 -0
  26. package/lib/cjs/canisters/asset.js +18 -0
  27. package/lib/cjs/canisters/asset.js.map +1 -0
  28. package/lib/cjs/canisters/asset_idl.d.ts +7 -0
  29. package/lib/cjs/canisters/asset_idl.js +14 -0
  30. package/lib/cjs/canisters/asset_idl.js.map +1 -0
  31. package/lib/cjs/canisters/management.d.ts +33 -0
  32. package/lib/cjs/canisters/management.js +30 -0
  33. package/lib/cjs/canisters/management.js.map +1 -0
  34. package/lib/cjs/canisters/management_idl.d.ts +7 -0
  35. package/lib/cjs/canisters/management_idl.js +29 -0
  36. package/lib/cjs/canisters/management_idl.js.map +1 -0
  37. package/lib/cjs/cbor.d.ts +9 -0
  38. package/lib/cjs/cbor.js +138 -0
  39. package/lib/cjs/cbor.js.map +1 -0
  40. package/lib/cjs/certificate.d.ts +43 -0
  41. package/lib/cjs/certificate.js +227 -0
  42. package/lib/cjs/certificate.js.map +1 -0
  43. package/lib/cjs/errors.d.ts +8 -0
  44. package/lib/cjs/errors.js +13 -0
  45. package/lib/cjs/errors.js.map +1 -0
  46. package/lib/cjs/index.d.ts +52 -0
  47. package/lib/cjs/index.js +38 -0
  48. package/lib/cjs/index.js.map +1 -0
  49. package/lib/cjs/polling/index.d.ts +16 -0
  50. package/lib/cjs/polling/index.js +81 -0
  51. package/lib/cjs/polling/index.js.map +1 -0
  52. package/lib/cjs/polling/strategy.d.ts +47 -0
  53. package/lib/cjs/polling/strategy.js +106 -0
  54. package/lib/cjs/polling/strategy.js.map +1 -0
  55. package/lib/cjs/request_id.d.ts +15 -0
  56. package/lib/cjs/request_id.js +89 -0
  57. package/lib/cjs/request_id.js.map +1 -0
  58. package/lib/cjs/utils/bls.d.ts +9 -0
  59. package/lib/cjs/utils/bls.js +65 -0
  60. package/lib/cjs/utils/bls.js.map +1 -0
  61. package/lib/cjs/utils/buffer.d.ts +16 -0
  62. package/lib/cjs/utils/buffer.js +59 -0
  63. package/lib/cjs/utils/buffer.js.map +1 -0
  64. package/lib/cjs/vendor/bls/bls.d.ts +28 -0
  65. package/lib/cjs/vendor/bls/bls.js +894 -0
  66. package/lib/cjs/vendor/bls/bls.js.map +1 -0
  67. package/lib/esm/actor.d.ts +126 -0
  68. package/lib/esm/actor.js +200 -0
  69. package/lib/esm/actor.js.map +1 -0
  70. package/lib/esm/agent/api.d.ts +140 -0
  71. package/lib/esm/agent/api.js +13 -0
  72. package/lib/esm/agent/api.js.map +1 -0
  73. package/lib/esm/agent/http/index.d.ts +49 -0
  74. package/lib/esm/agent/http/index.js +328 -0
  75. package/lib/esm/agent/http/index.js.map +1 -0
  76. package/lib/esm/agent/http/transforms.d.ts +20 -0
  77. package/lib/esm/agent/http/transforms.js +45 -0
  78. package/lib/esm/agent/http/transforms.js.map +1 -0
  79. package/lib/esm/agent/http/types.d.ts +77 -0
  80. package/lib/esm/agent/http/types.js +20 -0
  81. package/lib/esm/agent/http/types.js.map +1 -0
  82. package/lib/esm/agent/index.d.ts +7 -0
  83. package/lib/esm/agent/index.js +27 -0
  84. package/lib/esm/agent/index.js.map +1 -0
  85. package/lib/esm/agent/proxy.d.ts +85 -0
  86. package/lib/esm/agent/proxy.js +154 -0
  87. package/lib/esm/agent/proxy.js.map +1 -0
  88. package/lib/esm/auth.d.ts +87 -0
  89. package/lib/esm/auth.js +64 -0
  90. package/lib/esm/auth.js.map +1 -0
  91. package/lib/esm/canisters/asset.d.ts +10 -0
  92. package/lib/esm/canisters/asset.js +11 -0
  93. package/lib/esm/canisters/asset.js.map +1 -0
  94. package/lib/esm/canisters/asset_idl.d.ts +7 -0
  95. package/lib/esm/canisters/asset_idl.js +12 -0
  96. package/lib/esm/canisters/asset_idl.js.map +1 -0
  97. package/lib/esm/canisters/management.d.ts +37 -0
  98. package/lib/esm/canisters/management.js +23 -0
  99. package/lib/esm/canisters/management.js.map +1 -0
  100. package/lib/esm/canisters/management_idl.d.ts +7 -0
  101. package/lib/esm/canisters/management_idl.js +27 -0
  102. package/lib/esm/canisters/management_idl.js.map +1 -0
  103. package/lib/esm/cbor.d.ts +9 -0
  104. package/lib/esm/cbor.js +111 -0
  105. package/lib/esm/cbor.js.map +1 -0
  106. package/lib/esm/certificate.d.ts +43 -0
  107. package/lib/esm/certificate.js +200 -0
  108. package/lib/esm/certificate.js.map +1 -0
  109. package/lib/esm/errors.d.ts +8 -0
  110. package/lib/esm/errors.js +9 -0
  111. package/lib/esm/errors.js.map +1 -0
  112. package/lib/esm/index.d.ts +52 -0
  113. package/lib/esm/index.js +14 -0
  114. package/lib/esm/index.js.map +1 -0
  115. package/lib/esm/polling/index.d.ts +16 -0
  116. package/lib/esm/polling/index.js +57 -0
  117. package/lib/esm/polling/index.js.map +1 -0
  118. package/lib/esm/polling/strategy.d.ts +47 -0
  119. package/lib/esm/polling/strategy.js +95 -0
  120. package/lib/esm/polling/strategy.js.map +1 -0
  121. package/lib/esm/request_id.d.ts +15 -0
  122. package/lib/esm/request_id.js +81 -0
  123. package/lib/esm/request_id.js.map +1 -0
  124. package/lib/esm/utils/bls.d.ts +9 -0
  125. package/lib/esm/utils/bls.js +43 -0
  126. package/lib/esm/utils/bls.js.map +1 -0
  127. package/lib/esm/utils/buffer.d.ts +16 -0
  128. package/lib/esm/utils/buffer.js +52 -0
  129. package/lib/esm/utils/buffer.js.map +1 -0
  130. package/lib/esm/vendor/bls/bls.d.ts +28 -0
  131. package/lib/esm/vendor/bls/bls.js +870 -0
  132. package/lib/esm/vendor/bls/bls.js.map +1 -0
  133. package/package.json +4 -4
@@ -0,0 +1,47 @@
1
+ import { Principal } from '@astrox/principal';
2
+ import { RequestId, RequestStatusResponseStatus } from '..';
3
+ import { PollStrategy } from './index';
4
+ export declare type Predicate<T> = (canisterId: Principal, requestId: RequestId, status: RequestStatusResponseStatus) => Promise<T>;
5
+ /**
6
+ * A best practices polling strategy: wait 2 seconds before the first poll, then 1 second
7
+ * with an exponential backoff factor of 1.2. Timeout after 5 minutes.
8
+ */
9
+ export declare function defaultStrategy(): PollStrategy;
10
+ /**
11
+ * Predicate that returns true once.
12
+ */
13
+ export declare function once(): Predicate<boolean>;
14
+ /**
15
+ * Delay the polling once.
16
+ * @param condition A predicate that indicates when to delay.
17
+ * @param timeInMsec The amount of time to delay.
18
+ */
19
+ export declare function conditionalDelay(condition: Predicate<boolean>, timeInMsec: number): PollStrategy;
20
+ /**
21
+ * Error out after a maximum number of polling has been done.
22
+ * @param count The maximum attempts to poll.
23
+ */
24
+ export declare function maxAttempts(count: number): PollStrategy;
25
+ /**
26
+ * Throttle polling.
27
+ * @param throttleInMsec Amount in millisecond to wait between each polling.
28
+ */
29
+ export declare function throttle(throttleInMsec: number): PollStrategy;
30
+ /**
31
+ * Reject a call after a certain amount of time.
32
+ * @param timeInMsec Time in milliseconds before the polling should be rejected.
33
+ */
34
+ export declare function timeout(timeInMsec: number): PollStrategy;
35
+ /**
36
+ * A strategy that throttle, but using an exponential backoff strategy.
37
+ * @param startingThrottleInMsec The throttle in milliseconds to start with.
38
+ * @param backoffFactor The factor to multiple the throttle time between every poll. For
39
+ * example if using 2, the throttle will double between every run.
40
+ */
41
+ export declare function backoff(startingThrottleInMsec: number, backoffFactor: number): PollStrategy;
42
+ /**
43
+ * Chain multiple polling strategy. This _chains_ the strategies, so if you pass in,
44
+ * say, two throttling strategy of 1 second, it will result in a throttle of 2 seconds.
45
+ * @param strategies A strategy list to chain.
46
+ */
47
+ export declare function chain(...strategies: PollStrategy[]): PollStrategy;
@@ -0,0 +1,106 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.chain = exports.backoff = exports.timeout = exports.throttle = exports.maxAttempts = exports.conditionalDelay = exports.once = exports.defaultStrategy = void 0;
4
+ const buffer_1 = require("../utils/buffer");
5
+ const FIVE_MINUTES_IN_MSEC = 5 * 60 * 1000;
6
+ /**
7
+ * A best practices polling strategy: wait 2 seconds before the first poll, then 1 second
8
+ * with an exponential backoff factor of 1.2. Timeout after 5 minutes.
9
+ */
10
+ function defaultStrategy() {
11
+ return chain(conditionalDelay(once(), 1000), backoff(1000, 1.2), timeout(FIVE_MINUTES_IN_MSEC));
12
+ }
13
+ exports.defaultStrategy = defaultStrategy;
14
+ /**
15
+ * Predicate that returns true once.
16
+ */
17
+ function once() {
18
+ let first = true;
19
+ return async () => {
20
+ if (first) {
21
+ first = false;
22
+ return true;
23
+ }
24
+ return false;
25
+ };
26
+ }
27
+ exports.once = once;
28
+ /**
29
+ * Delay the polling once.
30
+ * @param condition A predicate that indicates when to delay.
31
+ * @param timeInMsec The amount of time to delay.
32
+ */
33
+ function conditionalDelay(condition, timeInMsec) {
34
+ return async (canisterId, requestId, status) => {
35
+ if (await condition(canisterId, requestId, status)) {
36
+ return new Promise(resolve => setTimeout(resolve, timeInMsec));
37
+ }
38
+ };
39
+ }
40
+ exports.conditionalDelay = conditionalDelay;
41
+ /**
42
+ * Error out after a maximum number of polling has been done.
43
+ * @param count The maximum attempts to poll.
44
+ */
45
+ function maxAttempts(count) {
46
+ let attempts = count;
47
+ return async (canisterId, requestId, status) => {
48
+ if (--attempts <= 0) {
49
+ throw new Error(`Failed to retrieve a reply for request after ${count} attempts:\n` +
50
+ ` Request ID: ${buffer_1.toHex(requestId)}\n` +
51
+ ` Request status: ${status}\n`);
52
+ }
53
+ };
54
+ }
55
+ exports.maxAttempts = maxAttempts;
56
+ /**
57
+ * Throttle polling.
58
+ * @param throttleInMsec Amount in millisecond to wait between each polling.
59
+ */
60
+ function throttle(throttleInMsec) {
61
+ return () => new Promise(resolve => setTimeout(resolve, throttleInMsec));
62
+ }
63
+ exports.throttle = throttle;
64
+ /**
65
+ * Reject a call after a certain amount of time.
66
+ * @param timeInMsec Time in milliseconds before the polling should be rejected.
67
+ */
68
+ function timeout(timeInMsec) {
69
+ const end = Date.now() + timeInMsec;
70
+ return async (canisterId, requestId, status) => {
71
+ if (Date.now() > end) {
72
+ throw new Error(`Request timed out after ${timeInMsec} msec:\n` +
73
+ ` Request ID: ${buffer_1.toHex(requestId)}\n` +
74
+ ` Request status: ${status}\n`);
75
+ }
76
+ };
77
+ }
78
+ exports.timeout = timeout;
79
+ /**
80
+ * A strategy that throttle, but using an exponential backoff strategy.
81
+ * @param startingThrottleInMsec The throttle in milliseconds to start with.
82
+ * @param backoffFactor The factor to multiple the throttle time between every poll. For
83
+ * example if using 2, the throttle will double between every run.
84
+ */
85
+ function backoff(startingThrottleInMsec, backoffFactor) {
86
+ let currentThrottling = startingThrottleInMsec;
87
+ return () => new Promise(resolve => setTimeout(() => {
88
+ currentThrottling *= backoffFactor;
89
+ resolve();
90
+ }, currentThrottling));
91
+ }
92
+ exports.backoff = backoff;
93
+ /**
94
+ * Chain multiple polling strategy. This _chains_ the strategies, so if you pass in,
95
+ * say, two throttling strategy of 1 second, it will result in a throttle of 2 seconds.
96
+ * @param strategies A strategy list to chain.
97
+ */
98
+ function chain(...strategies) {
99
+ return async (canisterId, requestId, status) => {
100
+ for (const a of strategies) {
101
+ await a(canisterId, requestId, status);
102
+ }
103
+ };
104
+ }
105
+ exports.chain = chain;
106
+ //# sourceMappingURL=strategy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"strategy.js","sourceRoot":"","sources":["../../../src/polling/strategy.ts"],"names":[],"mappings":";;;AAEA,4CAAwC;AASxC,MAAM,oBAAoB,GAAG,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC;AAE3C;;;GAGG;AACH,SAAgB,eAAe;IAC7B,OAAO,KAAK,CAAC,gBAAgB,CAAC,IAAI,EAAE,EAAE,IAAI,CAAC,EAAE,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,EAAE,OAAO,CAAC,oBAAoB,CAAC,CAAC,CAAC;AAClG,CAAC;AAFD,0CAEC;AAED;;GAEG;AACH,SAAgB,IAAI;IAClB,IAAI,KAAK,GAAG,IAAI,CAAC;IACjB,OAAO,KAAK,IAAI,EAAE;QAChB,IAAI,KAAK,EAAE;YACT,KAAK,GAAG,KAAK,CAAC;YACd,OAAO,IAAI,CAAC;SACb;QACD,OAAO,KAAK,CAAC;IACf,CAAC,CAAC;AACJ,CAAC;AATD,oBASC;AAED;;;;GAIG;AACH,SAAgB,gBAAgB,CAAC,SAA6B,EAAE,UAAkB;IAChF,OAAO,KAAK,EACV,UAAqB,EACrB,SAAoB,EACpB,MAAmC,EACnC,EAAE;QACF,IAAI,MAAM,SAAS,CAAC,UAAU,EAAE,SAAS,EAAE,MAAM,CAAC,EAAE;YAClD,OAAO,IAAI,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,UAAU,CAAC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AAG,EAAE;QACd,iBAAiB,IAAI,aAAa,CAAC;QACnC,OAAO,EAAE,CAAC;IACZ,CAAC,EAAE,iBAAiB,CAAC,CACtB,CAAC;AACN,CAAC;AAVD,0BAUC;AAED;;;;GAIG;AACH,SAAgB,KAAK,CAAC,GAAG,UAA0B;IACjD,OAAO,KAAK,EACV,UAAqB,EACrB,SAAoB,EACpB,MAAmC,EACnC,EAAE;QACF,KAAK,MAAM,CAAC,IAAI,UAAU,EAAE;YAC1B,MAAM,CAAC,CAAC,UAAU,EAAE,SAAS,EAAE,MAAM,CAAC,CAAC;SACxC;IACH,CAAC,CAAC;AACJ,CAAC;AAVD,sBAUC"}
@@ -0,0 +1,15 @@
1
+ export declare type RequestId = ArrayBuffer & {
2
+ __requestId__: void;
3
+ };
4
+ /**
5
+ * sha256 hash the provided Buffer
6
+ * @param data - input to hash function
7
+ */
8
+ export declare function hash(data: ArrayBuffer): ArrayBuffer;
9
+ /**
10
+ * Get the RequestId of the provided ic-ref request.
11
+ * RequestId is the result of the representation-independent-hash function.
12
+ * https://sdk.dfinity.org/docs/interface-spec/index.html#hash-of-map
13
+ * @param request - ic-ref request to hash into RequestId
14
+ */
15
+ export declare function requestIdOf(request: Record<string, any>): RequestId;
@@ -0,0 +1,89 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.requestIdOf = exports.hash = void 0;
7
+ const candid_1 = require("@astrox/candid");
8
+ const principal_1 = require("@astrox/principal");
9
+ const borc_1 = __importDefault(require("borc"));
10
+ const js_sha256_1 = require("js-sha256");
11
+ const buffer_1 = require("./utils/buffer");
12
+ /**
13
+ * sha256 hash the provided Buffer
14
+ * @param data - input to hash function
15
+ */
16
+ function hash(data) {
17
+ return js_sha256_1.sha256.create().update(new Uint8Array(data)).arrayBuffer();
18
+ }
19
+ exports.hash = hash;
20
+ function hashValue(value) {
21
+ if (value instanceof borc_1.default.Tagged) {
22
+ return hashValue(value.value);
23
+ }
24
+ else if (typeof value === 'string') {
25
+ return hashString(value);
26
+ }
27
+ else if (typeof value === 'number') {
28
+ return hash(candid_1.lebEncode(value));
29
+ }
30
+ else if (value instanceof ArrayBuffer || ArrayBuffer.isView(value)) {
31
+ return hash(value);
32
+ }
33
+ else if (Array.isArray(value)) {
34
+ const vals = value.map(hashValue);
35
+ return hash(buffer_1.concat(...vals));
36
+ }
37
+ else if (value instanceof principal_1.Principal) {
38
+ return hash(value.toUint8Array());
39
+ }
40
+ else if (typeof value === 'object' &&
41
+ value !== null &&
42
+ typeof value.toHash === 'function') {
43
+ return hashValue(value.toHash());
44
+ // TODO This should be move to a specific async method as the webauthn flow required
45
+ // the flow to be synchronous to ensure Safari touch id works.
46
+ // } else if (value instanceof Promise) {
47
+ // return value.then(x => hashValue(x));
48
+ }
49
+ else if (typeof value === 'bigint') {
50
+ // Do this check much later than the other bigint check because this one is much less
51
+ // type-safe.
52
+ // So we want to try all the high-assurance type guards before this 'probable' one.
53
+ return hash(candid_1.lebEncode(value));
54
+ }
55
+ throw Object.assign(new Error(`Attempt to hash a value of unsupported type: ${value}`), {
56
+ // include so logs/callers can understand the confusing value.
57
+ // (when stringified in error message, prototype info is lost)
58
+ value,
59
+ });
60
+ }
61
+ const hashString = (value) => {
62
+ const encoded = new TextEncoder().encode(value);
63
+ return hash(encoded);
64
+ };
65
+ /**
66
+ * Get the RequestId of the provided ic-ref request.
67
+ * RequestId is the result of the representation-independent-hash function.
68
+ * https://sdk.dfinity.org/docs/interface-spec/index.html#hash-of-map
69
+ * @param request - ic-ref request to hash into RequestId
70
+ */
71
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
72
+ function requestIdOf(request) {
73
+ const hashed = Object.entries(request)
74
+ .filter(([, value]) => value !== undefined)
75
+ .map(([key, value]) => {
76
+ const hashedKey = hashString(key);
77
+ const hashedValue = hashValue(value);
78
+ return [hashedKey, hashedValue];
79
+ });
80
+ const traversed = hashed;
81
+ const sorted = traversed.sort(([k1], [k2]) => {
82
+ return buffer_1.compare(k1, k2);
83
+ });
84
+ const concatenated = buffer_1.concat(...sorted.map(x => buffer_1.concat(...x)));
85
+ const requestId = hash(concatenated);
86
+ return requestId;
87
+ }
88
+ exports.requestIdOf = requestIdOf;
89
+ //# sourceMappingURL=request_id.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"request_id.js","sourceRoot":"","sources":["../../src/request_id.ts"],"names":[],"mappings":";;;;;;AAAA,2CAA2C;AAC3C,iDAA8C;AAC9C,gDAAwB;AACxB,yCAA+C;AAC/C,2CAAiD;AAIjD;;;GAGG;AACH,SAAgB,IAAI,CAAC,IAAiB;IACpC,OAAO,kBAAQ,CAAC,MAAM,EAAE,CAAC,MAAM,CAAC,IAAI,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC;AACtE,CAAC;AAFD,oBAEC;AAMD,SAAS,SAAS,CAAC,KAAc;IAC/B,IAAI,KAAK,YAAY,cAAI,CAAC,MAAM,EAAE;QAChC,OAAO,SAAS,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;KAC/B;SAAM,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE;QACpC,OAAO,UAAU,CAAC,KAAK,CAAC,CAAC;KAC1B;SAAM,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE;QACpC,OAAO,IAAI,CAAC,kBAAS,CAAC,KAAK,CAAC,CAAC,CAAC;KAC/B;SAAM,IAAI,KAAK,YAAY,WAAW,IAAI,WAAW,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE;QACpE,OAAO,IAAI,CAAC,KAAoB,CAAC,CAAC;KACnC;SAAM,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE;QAC/B,MAAM,IAAI,GAAG,KAAK,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QAClC,OAAO,IAAI,CAAC,eAAM,CAAC,GAAG,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CAAC,KAAK,CAAC,CAAC;QAErC,OAAO,CAAC,SAAS,EAAE,WAAW,CAA+B,CAAC;IAChE,CAAC,CAAC,CAAC;IAEL,MAAM,SAAS,GAAsC,MAAM,CAAC;IAE5D,MAAM,MAAM,GAAsC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE;QAC9E,OAAO,gBAAO,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;IACzB,CAAC,CAAC,CAAC;IAEH,MAAM,YAAY,GAAgB,eAAM,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,eAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;IAC3E,MAAM,SAAS,GAAG,IAAI,CAAC,YAAY,CAAc,CAAC;IAClD,OAAO,SAAS,CAAC;AACnB,CAAC;AAnBD,kCAmBC"}
@@ -0,0 +1,9 @@
1
+ export declare let verify: (pk: Uint8Array, sig: Uint8Array, msg: Uint8Array) => Promise<boolean>;
2
+ /**
3
+ *
4
+ * @param pk primary key: Uint8Array
5
+ * @param sig signature: Uint8Array
6
+ * @param msg message: Uint8Array
7
+ * @returns Promise resolving a boolean
8
+ */
9
+ export declare function blsVerify(pk: Uint8Array, sig: Uint8Array, msg: Uint8Array): Promise<boolean>;
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ exports.blsVerify = exports.verify = void 0;
23
+ const index_1 = require("../index");
24
+ const bls_1 = __importStar(require("../vendor/bls/bls"));
25
+ /**
26
+ *
27
+ * @param pk primary key: Uint8Array
28
+ * @param sig signature: Uint8Array
29
+ * @param msg message: Uint8Array
30
+ * @returns Promise resolving a boolean
31
+ */
32
+ async function blsVerify(pk, sig, msg) {
33
+ if (!exports.verify) {
34
+ const kraken = index_1.getKraken();
35
+ if (!kraken) {
36
+ await bls_1.default();
37
+ if (bls_1.bls_init() !== 0) {
38
+ throw new Error('Cannot initialize BLS');
39
+ }
40
+ exports.verify = async (pk1, sig1, msg1) => {
41
+ // Reorder things from what the WASM expects (sig, m, w).
42
+ return bls_1.bls_verify(sig1, msg1, pk1) === 0;
43
+ };
44
+ }
45
+ else {
46
+ const isInit = await kraken.methodChannel.invokeMethod('agent/blsInit', []);
47
+ if (!isInit) {
48
+ throw new Error('Cannot initialize BLS');
49
+ }
50
+ exports.verify = async (pk1, sig1, msg1) => {
51
+ // Reorder things from what the WASM expects (sig, m, w).
52
+ const verifyResult = await kraken.methodChannel.invokeMethod('agent/blsVerify', [
53
+ Buffer.from(pk1).toString('hex'),
54
+ Buffer.from(sig1).toString('hex'),
55
+ new TextDecoder().decode(msg1),
56
+ ]);
57
+ return verifyResult === 0;
58
+ };
59
+ }
60
+ }
61
+ const result = await exports.verify(pk, sig, msg);
62
+ return result;
63
+ }
64
+ exports.blsVerify = blsVerify;
65
+ //# sourceMappingURL=bls.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bls.js","sourceRoot":"","sources":["../../../src/utils/bls.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;AACA,o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EAAE,GAAG,CAAC,CAAC;IAC1C,OAAO,MAAM,CAAC;AAChB,CAAC;AAnCD,8BAmCC"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Concatenate multiple array buffers.
3
+ * @param buffers The buffers to concatenate.
4
+ */
5
+ export declare function concat(...buffers: ArrayBuffer[]): ArrayBuffer;
6
+ /**
7
+ * Transforms a buffer to an hexadecimal string. This will use the buffer as an Uint8Array.
8
+ * @param buffer The buffer to return the hexadecimal string of.
9
+ */
10
+ export declare function toHex(buffer: ArrayBuffer): string;
11
+ /**
12
+ * Transforms a hexadecimal string into an array buffer.
13
+ * @param hex The hexadecimal string to use.
14
+ */
15
+ export declare function fromHex(hex: string): ArrayBuffer;
16
+ export declare function compare(b1: ArrayBuffer, b2: ArrayBuffer): number;
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.compare = exports.fromHex = exports.toHex = exports.concat = void 0;
4
+ /**
5
+ * Concatenate multiple array buffers.
6
+ * @param buffers The buffers to concatenate.
7
+ */
8
+ function concat(...buffers) {
9
+ const result = new Uint8Array(buffers.reduce((acc, curr) => acc + curr.byteLength, 0));
10
+ let index = 0;
11
+ for (const b of buffers) {
12
+ result.set(new Uint8Array(b), index);
13
+ index += b.byteLength;
14
+ }
15
+ return result.buffer;
16
+ }
17
+ exports.concat = concat;
18
+ /**
19
+ * Transforms a buffer to an hexadecimal string. This will use the buffer as an Uint8Array.
20
+ * @param buffer The buffer to return the hexadecimal string of.
21
+ */
22
+ function toHex(buffer) {
23
+ return [...new Uint8Array(buffer)].map(x => x.toString(16).padStart(2, '0')).join('');
24
+ }
25
+ exports.toHex = toHex;
26
+ const hexRe = /^([0-9A-F]{2})*$/i.compile();
27
+ /**
28
+ * Transforms a hexadecimal string into an array buffer.
29
+ * @param hex The hexadecimal string to use.
30
+ */
31
+ function fromHex(hex) {
32
+ if (!hexRe.test(hex)) {
33
+ throw new Error('Invalid hexadecimal string.');
34
+ }
35
+ const buffer = [...hex]
36
+ .reduce((acc, curr, i) => {
37
+ // tslint:disable-next-line:no-bitwise
38
+ acc[(i / 2) | 0] = (acc[(i / 2) | 0] || '') + curr;
39
+ return acc;
40
+ }, [])
41
+ .map(x => Number.parseInt(x, 16));
42
+ return new Uint8Array(buffer).buffer;
43
+ }
44
+ exports.fromHex = fromHex;
45
+ function compare(b1, b2) {
46
+ if (b1.byteLength !== b2.byteLength) {
47
+ return b1.byteLength - b2.byteLength;
48
+ }
49
+ const u1 = new Uint8Array(b1);
50
+ const u2 = new Uint8Array(b2);
51
+ for (let i = 0; i < u1.length; i++) {
52
+ if (u1[i] !== u2[i]) {
53
+ return u1[i] - u2[i];
54
+ }
55
+ }
56
+ return 0;
57
+ }
58
+ exports.compare = compare;
59
+ //# sourceMappingURL=buffer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"buffer.js","sourceRoot":"","sources":["../../../src/utils/buffer.ts"],"names":[],"mappings":";;;AAAA;;;GAGG;AACH,SAAgB,MAAM,CAAC,GAAG,OAAsB;IAC9C,MAAM,MAAM,GAAG,IAAI,UAAU,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE,CAAC,GAAG,GAAG,IAAI,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC,CAAC;IACvF,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,KAAK,MAAM,CAAC,IAAI,OAAO,EAAE;QACvB,MAAM,CAAC,GAAG,CAAC,IAAI,UAAU,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC;QACrC,KAAK,IAAI,CAAC,CAAC,UAAU,CAAC;KACvB;IACD,OAAO,MAAM,CAAC,MAAM,CAAC;AACvB,CAAC;AARD,wBAQC;AAED;;;GAGG;AACH,SAAgB,KAAK,CAAC,MAAmB;IACvC,OAAO,CAAC,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,C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AG,CAAC,EAAE,CAAC,GAAG,EAAE,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;QAClC,IAAI,EAAE,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE;YACnB,OAAO,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC;SACtB;KACF;IACD,OAAO,CAAC,CAAC;AACX,CAAC;AAbD,0BAaC"}
@@ -0,0 +1,28 @@
1
+ /**
2
+ * @returns {number}
3
+ */
4
+ export declare function bls_init(): number;
5
+ /**
6
+ * @param {Uint8Array} sig
7
+ * @param {Uint8Array} m
8
+ * @param {Uint8Array} w
9
+ * @returns {number}
10
+ */
11
+ export declare function bls_verify(sig: Uint8Array, m: Uint8Array, w: Uint8Array): number;
12
+ declare function init(): Promise<InitOutput>;
13
+ export declare type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
14
+ export interface InitOutput {
15
+ readonly memory: WebAssembly.Memory;
16
+ readonly bls_init: () => number;
17
+ readonly bls_verify: (a: number, b: number, c: number, d: number, e: number, f: number) => number;
18
+ readonly __wbindgen_malloc: (a: number) => number;
19
+ }
20
+ /**
21
+ * If `module_or_path` is {RequestInfo} or {URL}, makes a request and
22
+ * for everything else, calls `WebAssembly.instantiate` directly.
23
+ *
24
+ * @param {InitInput | Promise<InitInput>} module_or_path
25
+ *
26
+ * @returns {Promise<InitOutput>}
27
+ */
28
+ export default init;