@hyperlane-xyz/utils 0.5.0-beta0

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.
@@ -0,0 +1,6 @@
1
+ export * as types from './src/types';
2
+ export * as utils from './src/utils';
3
+ export { Validator, BaseValidator } from './src/validator';
4
+ export * as logging from './src/logging';
5
+ export { error, warn, log, debug, trace } from './src/logging';
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,aAAa,CAAC;AACrC,OAAO,KAAK,KAAK,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAC3D,OAAO,KAAK,OAAO,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.trace = exports.debug = exports.log = exports.warn = exports.error = exports.logging = exports.BaseValidator = exports.Validator = exports.utils = exports.types = void 0;
27
+ exports.types = __importStar(require("./src/types"));
28
+ exports.utils = __importStar(require("./src/utils"));
29
+ var validator_1 = require("./src/validator");
30
+ Object.defineProperty(exports, "Validator", { enumerable: true, get: function () { return validator_1.Validator; } });
31
+ Object.defineProperty(exports, "BaseValidator", { enumerable: true, get: function () { return validator_1.BaseValidator; } });
32
+ exports.logging = __importStar(require("./src/logging"));
33
+ var logging_1 = require("./src/logging");
34
+ Object.defineProperty(exports, "error", { enumerable: true, get: function () { return logging_1.error; } });
35
+ Object.defineProperty(exports, "warn", { enumerable: true, get: function () { return logging_1.warn; } });
36
+ Object.defineProperty(exports, "log", { enumerable: true, get: function () { return logging_1.log; } });
37
+ Object.defineProperty(exports, "debug", { enumerable: true, get: function () { return logging_1.debug; } });
38
+ Object.defineProperty(exports, "trace", { enumerable: true, get: function () { return logging_1.trace; } });
39
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,qDAAqC;AACrC,qDAAqC;AACrC,6CAA2D;AAAlD,sGAAA,SAAS,OAAA;AAAE,0GAAA,aAAa,OAAA;AACjC,yDAAyC;AACzC,yCAA+D;AAAtD,gGAAA,KAAK,OAAA;AAAE,+FAAA,IAAI,OAAA;AAAE,8FAAA,GAAG,OAAA;AAAE,gGAAA,KAAK,OAAA;AAAE,gGAAA,KAAK,OAAA"}
@@ -0,0 +1,6 @@
1
+ export declare function trace(message: string, data?: any): void;
2
+ export declare function debug(message: string, data?: any): void;
3
+ export declare function log(message: string, data?: any): void;
4
+ export declare function warn(message: string, data?: any): void;
5
+ export declare function error(message: string, data?: any): void;
6
+ //# sourceMappingURL=logging.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logging.d.ts","sourceRoot":"","sources":["../../src/logging.ts"],"names":[],"mappings":"AAcA,wBAAgB,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,GAAG,QAEhD;AAED,wBAAgB,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,GAAG,QAEhD;AAED,wBAAgB,GAAG,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,GAAG,QAE9C;AAED,wBAAgB,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,GAAG,QAE/C;AAED,wBAAgB,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,GAAG,QAEhD"}
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ var _a;
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.error = exports.warn = exports.log = exports.debug = exports.trace = void 0;
5
+ const utils_1 = require("./utils");
6
+ const ENV_LOG_LEVEL = ((_a = (0, utils_1.safelyAccessEnvVar)('LOG_LEVEL')) !== null && _a !== void 0 ? _a : 'debug').toLowerCase();
7
+ const LOG_TRACE = ENV_LOG_LEVEL == 'trace';
8
+ const LOG_DEBUG = LOG_TRACE || ENV_LOG_LEVEL == 'debug';
9
+ const LOG_INFO = LOG_DEBUG || ENV_LOG_LEVEL == 'info';
10
+ const LOG_WARN = LOG_INFO || ENV_LOG_LEVEL == 'warn';
11
+ const LOG_ERROR = LOG_WARN || ENV_LOG_LEVEL == 'error';
12
+ function trace(message, data) {
13
+ if (LOG_TRACE)
14
+ logWithFunction(console.trace, 'trace', message, data);
15
+ }
16
+ exports.trace = trace;
17
+ function debug(message, data) {
18
+ if (LOG_DEBUG)
19
+ logWithFunction(console.debug, 'debug', message, data);
20
+ }
21
+ exports.debug = debug;
22
+ function log(message, data) {
23
+ if (LOG_INFO)
24
+ logWithFunction(console.log, 'info', message, data);
25
+ }
26
+ exports.log = log;
27
+ function warn(message, data) {
28
+ if (LOG_WARN)
29
+ logWithFunction(console.warn, 'warn', message, data);
30
+ }
31
+ exports.warn = warn;
32
+ function error(message, data) {
33
+ if (LOG_ERROR)
34
+ logWithFunction(console.error, 'error', message, data);
35
+ }
36
+ exports.error = error;
37
+ function logWithFunction(logFn, level, message, data) {
38
+ const fullLog = Object.assign(Object.assign({}, data), { level,
39
+ message });
40
+ logFn(JSON.stringify(fullLog));
41
+ }
42
+ //# sourceMappingURL=logging.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logging.js","sourceRoot":"","sources":["../../src/logging.ts"],"names":[],"mappings":";;;;AAAA,mCAA6C;AAK7C,MAAM,aAAa,GAAG,CACpB,MAAA,IAAA,0BAAkB,EAAC,WAAW,CAAC,mCAAI,OAAO,CAC3C,CAAC,WAAW,EAAe,CAAC;AAC7B,MAAM,SAAS,GAAG,aAAa,IAAI,OAAO,CAAC;AAC3C,MAAM,SAAS,GAAG,SAAS,IAAI,aAAa,IAAI,OAAO,CAAC;AACxD,MAAM,QAAQ,GAAG,SAAS,IAAI,aAAa,IAAI,MAAM,CAAC;AACtD,MAAM,QAAQ,GAAG,QAAQ,IAAI,aAAa,IAAI,MAAM,CAAC;AACrD,MAAM,SAAS,GAAG,QAAQ,IAAI,aAAa,IAAI,OAAO,CAAC;AAEvD,SAAgB,KAAK,CAAC,OAAe,EAAE,IAAU;IAC/C,IAAI,SAAS;QAAE,eAAe,CAAC,OAAO,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;AACxE,CAAC;AAFD,sBAEC;AAED,SAAgB,KAAK,CAAC,OAAe,EAAE,IAAU;IAC/C,IAAI,SAAS;QAAE,eAAe,CAAC,OAAO,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;AACxE,CAAC;AAFD,sBAEC;AAED,SAAgB,GAAG,CAAC,OAAe,EAAE,IAAU;IAC7C,IAAI,QAAQ;QAAE,eAAe,CAAC,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;AACpE,CAAC;AAFD,kBAEC;AAED,SAAgB,IAAI,CAAC,OAAe,EAAE,IAAU;IAC9C,IAAI,QAAQ;QAAE,eAAe,CAAC,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;AACrE,CAAC;AAFD,oBAEC;AAED,SAAgB,KAAK,CAAC,OAAe,EAAE,IAAU;IAC/C,IAAI,SAAS;QAAE,eAAe,CAAC,OAAO,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;AACxE,CAAC;AAFD,sBAEC;AAED,SAAS,eAAe,CACtB,KAAmC,EACnC,KAAgB,EAChB,OAAe,EACf,IAAU;IAEV,MAAM,OAAO,mCACR,IAAI,KACP,KAAK;QACL,OAAO,GACR,CAAC;IACF,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,CAAC;AACjC,CAAC"}
@@ -0,0 +1,40 @@
1
+ import { ethers } from 'ethers';
2
+ /********* BASIC TYPES *********/
3
+ export declare type Domain = number;
4
+ export declare type Address = string;
5
+ export declare type AddressBytes32 = string;
6
+ export declare type HexString = string;
7
+ export declare type SignatureLike = {
8
+ r: string;
9
+ s?: string;
10
+ _vs?: string;
11
+ recoveryParam?: number;
12
+ v?: number;
13
+ } | ethers.utils.BytesLike;
14
+ /********* ABACUS CORE *********/
15
+ export declare type Checkpoint = {
16
+ root: string;
17
+ index: number;
18
+ signature: SignatureLike;
19
+ };
20
+ export declare type CallData = {
21
+ to: Address;
22
+ data: string;
23
+ };
24
+ export declare enum AbacusState {
25
+ UNINITIALIZED = 0,
26
+ ACTIVE = 1,
27
+ FAILED = 2
28
+ }
29
+ export declare enum MessageStatus {
30
+ NONE = 0,
31
+ PROCESSED = 1
32
+ }
33
+ export declare type ParsedMessage = {
34
+ origin: number;
35
+ sender: string;
36
+ destination: number;
37
+ recipient: string;
38
+ body: string;
39
+ };
40
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC,iCAAiC;AACjC,oBAAY,MAAM,GAAG,MAAM,CAAC;AAC5B,oBAAY,OAAO,GAAG,MAAM,CAAC;AAC7B,oBAAY,cAAc,GAAG,MAAM,CAAC;AACpC,oBAAY,SAAS,GAAG,MAAM,CAAC;AAG/B,oBAAY,aAAa,GACrB;IACE,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,CAAC,EAAE,MAAM,CAAC;IACX,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,CAAC,CAAC,EAAE,MAAM,CAAC;CACZ,GACD,MAAM,CAAC,KAAK,CAAC,SAAS,CAAC;AAE3B,iCAAiC;AACjC,oBAAY,UAAU,GAAG;IACvB,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,SAAS,EAAE,aAAa,CAAC;CAC1B,CAAC;AAEF,oBAAY,QAAQ,GAAG;IACrB,EAAE,EAAE,OAAO,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,oBAAY,WAAW;IACrB,aAAa,IAAI;IACjB,MAAM,IAAA;IACN,MAAM,IAAA;CACP;AAED,oBAAY,aAAa;IACvB,IAAI,IAAI;IACR,SAAS,IAAA;CACV;AAED,oBAAY,aAAa,GAAG;IAC1B,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,IAAI,EAAE,MAAM,CAAC;CACd,CAAC"}
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MessageStatus = exports.AbacusState = void 0;
4
+ var AbacusState;
5
+ (function (AbacusState) {
6
+ AbacusState[AbacusState["UNINITIALIZED"] = 0] = "UNINITIALIZED";
7
+ AbacusState[AbacusState["ACTIVE"] = 1] = "ACTIVE";
8
+ AbacusState[AbacusState["FAILED"] = 2] = "FAILED";
9
+ })(AbacusState = exports.AbacusState || (exports.AbacusState = {}));
10
+ var MessageStatus;
11
+ (function (MessageStatus) {
12
+ MessageStatus[MessageStatus["NONE"] = 0] = "NONE";
13
+ MessageStatus[MessageStatus["PROCESSED"] = 1] = "PROCESSED";
14
+ })(MessageStatus = exports.MessageStatus || (exports.MessageStatus = {}));
15
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":";;;AA+BA,IAAY,WAIX;AAJD,WAAY,WAAW;IACrB,+DAAiB,CAAA;IACjB,iDAAM,CAAA;IACN,iDAAM,CAAA;AACR,CAAC,EAJW,WAAW,GAAX,mBAAW,KAAX,mBAAW,QAItB;AAED,IAAY,aAGX;AAHD,WAAY,aAAa;IACvB,iDAAQ,CAAA;IACR,2DAAS,CAAA;AACX,CAAC,EAHW,aAAa,GAAb,qBAAa,KAAb,qBAAa,QAGxB"}
@@ -0,0 +1,42 @@
1
+ /// <reference types="node" />
2
+ import { ethers } from 'ethers';
3
+ import { Checkpoint } from './types';
4
+ import { Address, Domain, HexString, ParsedMessage } from './types';
5
+ export declare function assert(predicate: any, errorMessage?: string): void;
6
+ export declare function deepEquals(v1: any, v2: any): boolean;
7
+ export declare const ensure0x: (hexstr: string) => string;
8
+ export declare const strip0x: (hexstr: string) => string;
9
+ export declare function addressToBytes32(address: Address): string;
10
+ export declare function bytes32ToAddress(bytes32: string): Address;
11
+ export declare function formatCallData<C extends ethers.Contract, I extends Parameters<C['interface']['encodeFunctionData']>>(destinationContract: C, functionName: I[0], functionArgs: I[1]): string;
12
+ export declare const formatMessage: (localDomain: Domain, senderAddr: Address, destinationDomain: Domain, recipientAddr: Address, body: HexString) => string;
13
+ /**
14
+ * Parse a serialized Abacus message from raw bytes.
15
+ *
16
+ * @param message
17
+ * @returns
18
+ */
19
+ export declare function parseMessage(message: string): ParsedMessage;
20
+ export declare function messageHash(message: HexString, leafIndex: number): string;
21
+ export declare function destinationAndNonce(destination: Domain, sequence: number): ethers.BigNumber;
22
+ export declare function domainHash(domain: number): string;
23
+ export declare function sleep(ms: number): Promise<void>;
24
+ export declare function retryAsync<T>(runner: () => T, attempts?: number, baseRetryMs?: number): Promise<T>;
25
+ export declare function median(a: number[]): number;
26
+ export declare function sum(a: number[]): number;
27
+ export declare function mean(a: number[]): number;
28
+ export declare function stdDev(a: number[]): number;
29
+ export declare function streamToString(stream: NodeJS.ReadableStream): Promise<string>;
30
+ export declare function isCheckpoint(obj: any): obj is Checkpoint;
31
+ /**
32
+ * Wait up to a given amount of time, and throw an error if the promise does not resolve in time.
33
+ * @param promise The promise to timeout on.
34
+ * @param timeoutMs How long to wait for the promise in milliseconds.
35
+ * @param message The error message if a timeout occurs.
36
+ */
37
+ export declare function timeout<T>(promise: Promise<T>, timeoutMs?: number, message?: string): Promise<T>;
38
+ export declare function safelyAccessEnvVar(name: string): string | undefined;
39
+ export declare function difference<T>(a: Set<T>, b: Set<T>): Set<T>;
40
+ export declare function symmetricDifference<T>(a: Set<T>, b: Set<T>): Set<T>;
41
+ export declare function setEquality<T>(a: Set<T>, b: Set<T>): boolean;
42
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,MAAM,EAAS,MAAM,QAAQ,CAAC;AAEvC,OAAO,EAAE,UAAU,EAAE,MAAM,S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ter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.setEquality = exports.symmetricDifference = exports.difference = exports.safelyAccessEnvVar = exports.timeout = exports.isCheckpoint = exports.streamToString = exports.stdDev = exports.mean = exports.sum = exports.median = exports.retryAsync = exports.sleep = exports.domainHash = exports.destinationAndNonce = exports.messageHash = exports.parseMessage = exports.formatMessage = exports.formatCallData = exports.bytes32ToAddress = exports.addressToBytes32 = exports.strip0x = exports.ensure0x = exports.deepEquals = exports.assert = void 0;
13
+ const ethers_1 = require("ethers");
14
+ function assert(predicate, errorMessage) {
15
+ if (!predicate) {
16
+ throw new Error(errorMessage !== null && errorMessage !== void 0 ? errorMessage : 'Error');
17
+ }
18
+ }
19
+ exports.assert = assert;
20
+ function deepEquals(v1, v2) {
21
+ return JSON.stringify(v1) === JSON.stringify(v2);
22
+ }
23
+ exports.deepEquals = deepEquals;
24
+ const ensure0x = (hexstr) => hexstr.startsWith('0x') ? hexstr : `0x${hexstr}`;
25
+ exports.ensure0x = ensure0x;
26
+ const strip0x = (hexstr) => hexstr.startsWith('0x') ? hexstr.slice(2) : hexstr;
27
+ exports.strip0x = strip0x;
28
+ function addressToBytes32(address) {
29
+ return ethers_1.ethers.utils
30
+ .hexZeroPad(ethers_1.ethers.utils.hexStripZeros(address), 32)
31
+ .toLowerCase();
32
+ }
33
+ exports.addressToBytes32 = addressToBytes32;
34
+ function bytes32ToAddress(bytes32) {
35
+ return ethers_1.ethers.utils.getAddress(bytes32.slice(-40));
36
+ }
37
+ exports.bytes32ToAddress = bytes32ToAddress;
38
+ function formatCallData(destinationContract, functionName, functionArgs) {
39
+ return destinationContract.interface.encodeFunctionData(functionName, functionArgs);
40
+ }
41
+ exports.formatCallData = formatCallData;
42
+ const formatMessage = (localDomain, senderAddr, destinationDomain, recipientAddr, body) => {
43
+ senderAddr = addressToBytes32(senderAddr);
44
+ recipientAddr = addressToBytes32(recipientAddr);
45
+ return ethers_1.ethers.utils.solidityPack(['uint32', 'bytes32', 'uint32', 'bytes32', 'bytes'], [localDomain, senderAddr, destinationDomain, recipientAddr, body]);
46
+ };
47
+ exports.formatMessage = formatMessage;
48
+ /**
49
+ * Parse a serialized Abacus message from raw bytes.
50
+ *
51
+ * @param message
52
+ * @returns
53
+ */
54
+ function parseMessage(message) {
55
+ const buf = Buffer.from(ethers_1.utils.arrayify(message));
56
+ const origin = buf.readUInt32BE(0);
57
+ const sender = ethers_1.utils.hexlify(buf.slice(4, 36));
58
+ const destination = buf.readUInt32BE(36);
59
+ const recipient = ethers_1.utils.hexlify(buf.slice(40, 72));
60
+ const body = ethers_1.utils.hexlify(buf.slice(72));
61
+ return { origin, sender, destination, recipient, body };
62
+ }
63
+ exports.parseMessage = parseMessage;
64
+ function messageHash(message, leafIndex) {
65
+ return ethers_1.ethers.utils.solidityKeccak256(['bytes', 'uint256'], [message, leafIndex]);
66
+ }
67
+ exports.messageHash = messageHash;
68
+ function destinationAndNonce(destination, sequence) {
69
+ assert(destination < Math.pow(2, 32) - 1);
70
+ assert(sequence < Math.pow(2, 32) - 1);
71
+ return ethers_1.ethers.BigNumber.from(destination)
72
+ .mul(ethers_1.ethers.BigNumber.from(2).pow(32))
73
+ .add(ethers_1.ethers.BigNumber.from(sequence));
74
+ }
75
+ exports.destinationAndNonce = destinationAndNonce;
76
+ function domainHash(domain) {
77
+ return ethers_1.ethers.utils.solidityKeccak256(['uint32', 'string'], [domain, 'ABACUS']);
78
+ }
79
+ exports.domainHash = domainHash;
80
+ function sleep(ms) {
81
+ return new Promise((resolve) => setTimeout(resolve, ms));
82
+ }
83
+ exports.sleep = sleep;
84
+ // Retries an async function if it raises an exception,
85
+ // with exponential backoff.
86
+ // If all the tries fail it raises the last thrown exception
87
+ function retryAsync(runner, attempts = 5, baseRetryMs = 50) {
88
+ return __awaiter(this, void 0, void 0, function* () {
89
+ let saveError;
90
+ for (let i = 0; i < attempts; i++) {
91
+ try {
92
+ return runner();
93
+ }
94
+ catch (error) {
95
+ saveError = error;
96
+ yield sleep(baseRetryMs * Math.pow(2, i));
97
+ }
98
+ }
99
+ throw saveError;
100
+ });
101
+ }
102
+ exports.retryAsync = retryAsync;
103
+ function median(a) {
104
+ const sorted = a.slice().sort();
105
+ const mid = Math.floor(sorted.length / 2);
106
+ const median = sorted.length % 2 == 0 ? (sorted[mid] + sorted[mid + 1]) / 2 : sorted[mid];
107
+ return median;
108
+ }
109
+ exports.median = median;
110
+ function sum(a) {
111
+ return a.reduce((acc, i) => acc + i);
112
+ }
113
+ exports.sum = sum;
114
+ function mean(a) {
115
+ return sum(a) / a.length;
116
+ }
117
+ exports.mean = mean;
118
+ function stdDev(a) {
119
+ const xbar = mean(a);
120
+ const squaredDifferences = a.map((x) => Math.pow(x - xbar, 2));
121
+ return Math.sqrt(mean(squaredDifferences));
122
+ }
123
+ exports.stdDev = stdDev;
124
+ function streamToString(stream) {
125
+ return new Promise((resolve, reject) => {
126
+ const chunks = [];
127
+ stream
128
+ .setEncoding('utf8')
129
+ .on('data', (chunk) => chunks.push(chunk))
130
+ .on('error', (err) => reject(err))
131
+ .on('end', () => resolve(String.prototype.concat(...chunks)));
132
+ });
133
+ }
134
+ exports.streamToString = streamToString;
135
+ function isCheckpoint(obj) {
136
+ const isValidSignature = typeof obj.signature === 'string'
137
+ ? ethers_1.ethers.utils.isHexString(obj.signature)
138
+ : ethers_1.ethers.utils.isHexString(obj.signature.r) &&
139
+ ethers_1.ethers.utils.isHexString(obj.signature.s) &&
140
+ Number.isSafeInteger(obj.signature.v);
141
+ const isValidRoot = ethers_1.ethers.utils.isHexString(obj.root);
142
+ const isValidIndex = Number.isSafeInteger(obj.index);
143
+ return isValidIndex && isValidRoot && isValidSignature;
144
+ }
145
+ exports.isCheckpoint = isCheckpoint;
146
+ /**
147
+ * Wait up to a given amount of time, and throw an error if the promise does not resolve in time.
148
+ * @param promise The promise to timeout on.
149
+ * @param timeoutMs How long to wait for the promise in milliseconds.
150
+ * @param message The error message if a timeout occurs.
151
+ */
152
+ function timeout(promise, timeoutMs, message = 'Timeout reached') {
153
+ if (!timeoutMs || timeoutMs <= 0)
154
+ return promise;
155
+ return new Promise((resolve, reject) => {
156
+ setTimeout(() => {
157
+ reject(new Error(message));
158
+ }, timeoutMs);
159
+ promise.then(resolve).catch(reject);
160
+ });
161
+ }
162
+ exports.timeout = timeout;
163
+ // Should be used instead of referencing process directly in case we don't
164
+ // run in node.js
165
+ function safelyAccessEnvVar(name) {
166
+ try {
167
+ return process.env[name];
168
+ }
169
+ catch (error) {
170
+ return undefined;
171
+ }
172
+ }
173
+ exports.safelyAccessEnvVar = safelyAccessEnvVar;
174
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set#implementing_basic_set_operations
175
+ function difference(a, b) {
176
+ const _difference = new Set(a);
177
+ for (const elem of b) {
178
+ _difference.delete(elem);
179
+ }
180
+ return _difference;
181
+ }
182
+ exports.difference = difference;
183
+ function symmetricDifference(a, b) {
184
+ const _difference = new Set(a);
185
+ for (const elem of b) {
186
+ if (_difference.has(elem)) {
187
+ _difference.delete(elem);
188
+ }
189
+ else {
190
+ _difference.add(elem);
191
+ }
192
+ }
193
+ return _difference;
194
+ }
195
+ exports.symmetricDifference = symmetricDifference;
196
+ function setEquality(a, b) {
197
+ return symmetricDifference(a, b).size === 0;
198
+ }
199
+ exports.setEquality = setEquality;
200
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"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K,MAAM,IAAI,IAAI,CAAC,EAAE;QACpB,IAAI,WAAW,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE;YACzB,WAAW,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;SAC1B;aAAM;YACL,WAAW,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;SACvB;KACF;IACD,OAAO,WAAW,CAAC;AACrB,CAAC;AAVD,kDAUC;AAED,SAAgB,WAAW,CAAI,CAAS,EAAE,CAAS;IACjD,OAAO,mBAAmB,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,CAAC;AAC9C,CAAC;AAFD,kCAEC"}
@@ -0,0 +1,26 @@
1
+ import { ethers } from 'ethers';
2
+ import { types } from '@hyperlane-xyz/utils';
3
+ import { Checkpoint } from './types';
4
+ /**
5
+ * Utilities for validators to construct and verify checkpoints.
6
+ */
7
+ export declare class BaseValidator {
8
+ localDomain: types.Domain;
9
+ address: types.Address;
10
+ constructor(address: types.Address, localDomain: types.Domain);
11
+ domainHash(): string;
12
+ message(root: types.HexString, index: number): string;
13
+ messageHash(root: types.HexString, index: number): Uint8Array;
14
+ recoverAddressFromCheckpoint(checkpoint: Checkpoint): types.Address;
15
+ matchesSigner(checkpoint: Checkpoint): boolean;
16
+ }
17
+ /**
18
+ * Extension of BaseValidator that includes ethers signing utilities.
19
+ */
20
+ export declare class Validator extends BaseValidator {
21
+ protected signer: ethers.Signer;
22
+ constructor(signer: ethers.Signer, address: types.Address, localDomain: types.Domain);
23
+ static fromSigner(signer: ethers.Signer, localDomain: types.Domain): Promise<Validator>;
24
+ signCheckpoint(root: types.HexString, index: number): Promise<Checkpoint>;
25
+ }
26
+ //# sourceMappingURL=validator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validator.d.ts","sourceRoot":"","sources":["../../src/validator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC,OAAO,EAAE,KAAK,EAAS,MAAM,sBAAsB,CAAC;AAEpD,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAErC;;GAEG;AACH,qBAAa,aAAa;IACxB,WAAW,EAAE,KAAK,CAAC,MAAM,CAAC;IAC1B,OAAO,EAAE,KAAK,CAAC,OAAO,CAAC;gBAEX,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,WAAW,EAAE,KAAK,CAAC,MAAM;IAK7D,UAAU;IAIV,OAAO,CAAC,IAAI,EAAE,KAAK,CAAC,SAAS,EAAE,KAAK,EAAE,MAAM;IAO5C,WAAW,CAAC,IAAI,EAAE,KAAK,CAAC,SAAS,EAAE,KAAK,EAAE,MAAM;IAKhD,4BAA4B,CAAC,UAAU,EAAE,UAAU,GAAG,KAAK,CAAC,OAAO;IAKnE,aAAa,CAAC,UAAU,EAAE,UAAU;CAMrC;AAED;;GAEG;AACH,qBAAa,SAAU,SAAQ,aAAa;IAExC,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM;gBAArB,MAAM,EAAE,MAAM,CAAC,MAAM,EAC/B,OAAO,EAAE,KAAK,CAAC,OAAO,EACtB,WAAW,EAAE,KAAK,CAAC,MAAM;WAKd,UAAU,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,WAAW,EAAE,KAAK,CAAC,MAAM;IAIlE,cAAc,CAClB,IAAI,EAAE,KAAK,CAAC,SAAS,EACrB,KAAK,EAAE,MAAM,GACZ,OAAO,CAAC,UAAU,CAAC;CASvB"}
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.Validator = exports.BaseValidator = void 0;
13
+ const ethers_1 = require("ethers");
14
+ const utils_1 = require("@hyperlane-xyz/utils");
15
+ /**
16
+ * Utilities for validators to construct and verify checkpoints.
17
+ */
18
+ class BaseValidator {
19
+ constructor(address, localDomain) {
20
+ this.localDomain = localDomain;
21
+ this.address = address;
22
+ }
23
+ domainHash() {
24
+ return utils_1.utils.domainHash(this.localDomain);
25
+ }
26
+ message(root, index) {
27
+ return ethers_1.ethers.utils.solidityPack(['bytes32', 'bytes32', 'uint256'], [this.domainHash(), root, index]);
28
+ }
29
+ messageHash(root, index) {
30
+ const message = this.message(root, index);
31
+ return ethers_1.ethers.utils.arrayify(ethers_1.ethers.utils.keccak256(message));
32
+ }
33
+ recoverAddressFromCheckpoint(checkpoint) {
34
+ const msgHash = this.messageHash(checkpoint.root, checkpoint.index);
35
+ return ethers_1.ethers.utils.verifyMessage(msgHash, checkpoint.signature);
36
+ }
37
+ matchesSigner(checkpoint) {
38
+ return (this.recoverAddressFromCheckpoint(checkpoint).toLowerCase() ===
39
+ this.address.toLowerCase());
40
+ }
41
+ }
42
+ exports.BaseValidator = BaseValidator;
43
+ /**
44
+ * Extension of BaseValidator that includes ethers signing utilities.
45
+ */
46
+ class Validator extends BaseValidator {
47
+ constructor(signer, address, localDomain) {
48
+ super(address, localDomain);
49
+ this.signer = signer;
50
+ }
51
+ static fromSigner(signer, localDomain) {
52
+ return __awaiter(this, void 0, void 0, function* () {
53
+ return new Validator(signer, yield signer.getAddress(), localDomain);
54
+ });
55
+ }
56
+ signCheckpoint(root, index) {
57
+ return __awaiter(this, void 0, void 0, function* () {
58
+ const msgHash = this.messageHash(root, index);
59
+ const signature = yield this.signer.signMessage(msgHash);
60
+ return {
61
+ root,
62
+ index,
63
+ signature,
64
+ };
65
+ });
66
+ }
67
+ }
68
+ exports.Validator = Validator;
69
+ //# sourceMappingURL=validator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validator.js","sourceRoot":"","sources":["../../src/validator.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,mCAAgC;AAEhC,gDAAoD;AAIpD;;GAEG;AACH,MAAa,aAAa;IAIxB,YAAY,OAAsB,EAAE,WAAyB;QAC3D,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;QAC/B,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACzB,CAAC;IAED,UAAU;QACR,OAAO,aAAK,CAAC,UAAU,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;IAC5C,CAAC;IAED,OAAO,CAAC,IAAqB,EAAE,KAAa;QAC1C,OAAO,eAAM,CAAC,KAAK,CAAC,YAAY,CAC9B,CAAC,SAAS,EAAE,SAAS,EAAE,SAAS,CAAC,EACjC,CAAC,IAAI,CAAC,UAAU,EAAE,EAAE,IAAI,EAAE,KAAK,CAAC,CACjC,CAAC;IACJ,CAAC;IAED,WAAW,CAAC,IAAqB,EAAE,KAAa;QAC9C,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;QAC1C,OAAO,eAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,eAAM,CAAC,KAAK,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,CAAC;IAChE,CAAC;IAED,4BAA4B,CAAC,UAAsB;QACjD,MAAM,OAAO,GAAG,IAAI,CAAC,WAAW,CAAC,UAAU,CAAC,IAAI,EAAE,UAAU,CAAC,KAAK,CAAC,CAAC;QACpE,OAAO,eAAM,CAAC,KAAK,CAAC,aAAa,CAAC,OAAO,EAAE,UAAU,CAAC,SAAS,CAAC,CAAC;IACnE,CAAC;IAED,aAAa,CAAC,UAAsB;QAClC,OAAO,CACL,IAAI,CAAC,4BAA4B,CAAC,UAAU,CAAC,CAAC,WAAW,EAAE;YAC3D,IAAI,CAAC,OAAO,CAAC,WAAW,EAAE,CAC3B,CAAC;IACJ,CAAC;CACF;AApCD,sCAoCC;AAED;;GAEG;AACH,MAAa,SAAU,SAAQ,aAAa;IAC1C,YACY,MAAqB,EAC/B,OAAsB,EACtB,WAAyB;QAEzB,KAAK,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC;QAJlB,WAAM,GAAN,MAAM,CAAe;IAKjC,CAAC;IAED,MAAM,CAAO,UAAU,CAAC,MAAqB,EAAE,WAAyB;;YACtE,OAAO,IAAI,SAAS,CAAC,MAAM,EAAE,MAAM,MAAM,CAAC,UAAU,EAAE,EAAE,WAAW,CAAC,CAAC;QACvE,CAAC;KAAA;IAEK,cAAc,CAClB,IAAqB,EACrB,KAAa;;YAEb,MAAM,OAAO,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;YAC9C,MAAM,SAAS,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;YACzD,OAAO;gBACL,IAAI;gBACJ,KAAK;gBACL,SAAS;aACV,CAAC;QACJ,CAAC;KAAA;CACF;AAzBD,8BAyBC"}
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@hyperlane-xyz/utils",
3
+ "description": "General utilities for the Hyperlane network",
4
+ "version": "0.5.0-beta0",
5
+ "dependencies": {
6
+ "ethers": "^5.6.8"
7
+ },
8
+ "devDependencies": {
9
+ "chai": "^4.3.0",
10
+ "prettier": "^2.4.1",
11
+ "typescript": "^4.7.2"
12
+ },
13
+ "homepage": "https://www.hyperlane.xyz",
14
+ "keywords": [
15
+ "Hyperlane",
16
+ "SDK",
17
+ "Typescript"
18
+ ],
19
+ "license": "Apache-2.0",
20
+ "main": "dist/index.js",
21
+ "prepublish": "yarn build",
22
+ "repository": "https://github.com/hyperlane-xyz/hyperlane-monorepo",
23
+ "scripts": {
24
+ "build": "tsc",
25
+ "clean": "rm -rf ./dist",
26
+ "check": "tsc --noEmit",
27
+ "prettier": "prettier --write ./src"
28
+ },
29
+ "types": "dist/index.d.ts",
30
+ "files": [
31
+ "/dist"
32
+ ]
33
+ }