UNPKG

@onflow/flow-js-testing

Version:

This package will expose a set of utility methods, to allow Cadence code testing with libraries like Jest

1,528 lines (1,384 loc) 105 kB
import * as fcl from '@onflow/fcl'; import { config as config$1, withPrefix, account, send, build, getBlock, decode } from '@onflow/fcl'; export { config } from '@onflow/fcl'; import path from 'path'; import fs from 'fs'; import { replaceImportAddresses, resolveArguments, getEnvironment, reportMissingImports, deployContract as deployContract$2, reportMissing, sendTransaction as sendTransaction$1, extractImports, extractContractParameters, generateSchema, splitArgs } from '@onflow/flow-cadut'; export { extractImports, replaceImportAddresses } from '@onflow/flow-cadut'; import * as rlp from 'rlp'; import { ec as ec$1 } from 'elliptic'; import { exec } from 'child_process'; import { createServer } from 'net'; import * as semver from 'semver'; import { satisfies } from 'semver'; import { sha3_256 } from 'js-sha3'; import { sha256 } from 'js-sha256'; /* * Flow JS Testing * * Copyright 2020-2021 Dapper Labs, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ const TARGET = "flow.json"; let configPath = null; let config = null; function isDir(dir) { return fs.lstatSync(dir).isDirectory(); } function listFiles(dir) { return new Set(fs.readdirSync(dir)); } function parentDir(dir) { return path.dirname(dir); } function findTarget(dir) { if (!isDir(dir)) throw new Error(`Not a directory: ${dir}`); return listFiles(dir).has(TARGET) ? path.resolve(dir, TARGET) : null; } function getConfigPath(dir) { if (configPath != null) return configPath; const filePath = findTarget(dir); if (filePath == null) { if (dir === parentDir(dir)) { throw new Error("No flow.json found"); } return getConfigPath(parentDir(dir)); } configPath = filePath; return configPath; } function flowConfig() { if (config != null) return config; const filePath = getConfigPath(process.cwd()); const content = fs.readFileSync(filePath, "utf8"); config = JSON.parse(content); return config; } /* * Flow JS Testing * * Copyright 2020-2021 Dapper Labs, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ const DEFAULT_COMPUTE_LIMIT = 9999; /** * Inits framework variables, storing private key of service account and base path * where Cadence files are stored. * @param {string} basePath - path to the folder with Cadence files to be tested. * @param {number} [props.port] - port to use for accessAPI * @param {number} [props.pkey] - private key to use for service account in case of collisions */ const init = async (basePath, props = {}) => { var _getServiceKey, _cfg$accounts$emulato, _cfg$accounts, _cfg$accounts$emulato2, _cfg$testing$paths, _cfg$testing; const { pkey = "48a1f554aeebf6bf9fe0d7b5b79d080700b073ee77909973ea0b2f6fbc902" } = props; const cfg = flowConfig(); config$1().put("PRIVATE_KEY", (_getServiceKey = getServiceKey(cfg)) != null ? _getServiceKey : pkey); config$1().put("SERVICE_ADDRESS", (_cfg$accounts$emulato = cfg == null ? void 0 : (_cfg$accounts = cfg.accounts) == null ? void 0 : (_cfg$accounts$emulato2 = _cfg$accounts["emulator-account"]) == null ? void 0 : _cfg$accounts$emulato2.address) != null ? _cfg$accounts$emulato : "f8d6e0586b0a20c7"); config$1().put("BASE_PATH", (_cfg$testing$paths = cfg == null ? void 0 : (_cfg$testing = cfg.testing) == null ? void 0 : _cfg$testing.paths) != null ? _cfg$testing$paths : basePath); config$1().put("fcl.limit", DEFAULT_COMPUTE_LIMIT); }; function getServiceKey(cfg) { var _cfg$accounts2, _cfg$accounts2$emulat; const value = cfg == null ? void 0 : (_cfg$accounts2 = cfg.accounts) == null ? void 0 : (_cfg$accounts2$emulat = _cfg$accounts2["emulator-account"]) == null ? void 0 : _cfg$accounts2$emulat.key; if (value) { if (typeof value === "object") { switch (value.type) { case "hex": return value.privateKey; case "file": { const configDir = path.dirname(getConfigPath()); const resovledPath = path.resolve(configDir, value.location); return fs.readFileSync(resovledPath, "utf8"); } default: return null; } } else if (typeof value === "string") { if (value.startsWith("$")) { return process.env[value.slice(1)]; } else { return value; } } else { return null; } } return null; } function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } /* * Flow JS Testing * * Copyright 2020-2021 Dapper Labs, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ const isObject = arg => typeof arg === "object" && arg !== null; const isString = obj => typeof obj === "string" || obj instanceof String; const isAddress = address => /^0x[0-9a-f]{0,16}$/.test(address); function getAvailablePorts(count = 1) { if (count === 0) return Promise.resolve([]); return new Promise((resolve, reject) => { const server = createServer(); server.listen(0, () => { const port = server.address().port; server.close(async err => { if (err) reject(err); resolve([...(await getAvailablePorts(count - 1)), port]); }); }); }); } /** * Get the Flow CLI version * @param {string} flowCommand - the Flow CLI command name * @returns {Promise<import("semver").SemVer>} */ async function getFlowVersion(flowCommand = "flow") { return new Promise((resolve, reject) => { exec(`${flowCommand} version --output=json`, (error, stdout) => { if (error) { reject("Could not determine Flow CLI version, please make sure it is installed and available in your PATH"); } else { let versionStr; try { versionStr = JSON.parse(stdout).version; } catch (error) { // fallback to regex for older versions of the CLI without JSON output const rxResult = /^Version: ([^\s]+)/m.exec(stdout); if (rxResult) { versionStr = rxResult[1]; } } const version = versionStr ? semver.parse(versionStr) : undefined; if (!version) { reject(`Invalid Flow CLI version string: ${versionStr}`); } resolve(version); } }); }); } const getServiceAddress = async () => { return withPrefix(await config$1().get("SERVICE_ADDRESS")); }; /* * Flow JS Testing * * Copyright 2020-2021 Dapper Labs, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ const invariant = (fact, msg, ...rest) => { if (!fact) { const error = new Error(`INVARIANT ${msg}`); error.stack = error.stack.split("\n").filter(d => !/at invariant/.test(d)).join("\n"); console.error("\n\n---\n\n", error, "\n\n", ...rest, "\n\n---\n\n"); throw error; } }; /** * Represents a signature for an arbitrary message generated using a particular key * @typedef {Object} SignatureObject * @property {string} addr address of account whose key was used to sign the message * @property {number} keyId key index on the account of they key used to sign the message * @property {string} signature signature corresponding to the signed message hash as hex-encoded string */ /** * Represents a private key object which may be used to generate a public key * @typedef {Object} KeyObject * @property {string | Buffer} privateKey private key for this key object * @property {SignatureAlgorithm} [signatureAlgorithm=SignatureAlgorithm.ECDSA_P256] signing algorithm used with this key * @property {HashAlgorithm} [hashAlgorithm=HashAlgorithm.SHA3_256] hash algorithm used with this key * @property {weight} [weight=1000] desired weight of this key (default full weight) */ /** * Represents a signer of a message or transaction * @typedef {Object} SignerInfoObject * @property {string} addr address of the signer * @property {HashAlgorithm} [hashAlgorithm=HashAlgorithm.SHA3_256] hash algorithm used to hash the message before signing * @property {SignatureAlgorithm} [signatureAlgorithm=SignatureAlgorithm.ECDSA_P256] signing algorithm used to generate the signature * @property {number} [keyId=0] index of the key on the signers account to use * @property {string | Buffer} [privateKey=SERVICE_KEY] private key of the signer (defaults to universal private key/service key from config) */ /** * Enum for signing algorithms * @readonly * @enum {number} */ const SignatureAlgorithm = { ECDSA_P256: 1, ECDSA_secp256k1: 2 }; /** * Enum for hasing algorithms * @readonly * @enum {number} */ const HashAlgorithm = { SHA2_256: 1, SHA3_256: 3 }; /** * Enum for mapping hash algorithm name to hashing function * @readonly * @enum {function} */ const HashFunction = { SHA2_256: sha256, SHA3_256: sha3_256 }; /** * Enum for mapping signature algorithm to elliptic instance * @readonly * @enum {EC} */ const ec = { ECDSA_P256: new ec$1("p256"), ECDSA_secp256k1: new ec$1("secp256k1") }; const resolveHashAlgoKey = hashAlgorithm => { const hashAlgorithmKey = Object.keys(HashAlgorithm).find(x => HashAlgorithm[x] === hashAlgorithm || isString(hashAlgorithm) && x.toLowerCase() === hashAlgorithm.toLowerCase()); if (!hashAlgorithmKey) throw new Error(`Provided hash algorithm "${hashAlgorithm}" is not currently supported`); return hashAlgorithmKey; }; const resolveSignAlgoKey = signatureAlgorithm => { const signatureAlgorithmKey = Object.keys(SignatureAlgorithm).find(x => SignatureAlgorithm[x] === signatureAlgorithm || isString(signatureAlgorithm) && x.toLowerCase() === signatureAlgorithm.toLowerCase()); if (!signatureAlgorithmKey) throw new Error(`Provided signature algorithm "${signatureAlgorithm}" is not currently supported`); return signatureAlgorithmKey; }; const hashMsgHex = (msgHex, hashAlgorithm = HashAlgorithm.SHA3_256) => { const hashAlgorithmKey = resolveHashAlgoKey(hashAlgorithm); const hashFn = HashFunction[hashAlgorithmKey]; const hash = hashFn.create(); hash.update(Buffer.from(msgHex, "hex")); return Buffer.from(hash.arrayBuffer()); }; const signWithKey = (privateKey, msgHex, hashAlgorithm = HashAlgorithm.SHA3_256, signatureAlgorithm = SignatureAlgorithm.ECDSA_P256) => { const signAlgo = resolveSignAlgoKey(signatureAlgorithm); const key = ec[signAlgo].keyFromPrivate(Buffer.from(privateKey, "hex")); const sig = key.sign(hashMsgHex(msgHex, hashAlgorithm)); const n = 32; // half of signature length? const r = sig.r.toArrayLike(Buffer, "be", n); const s = sig.s.toArrayLike(Buffer, "be", n); return Buffer.concat([r, s]).toString("hex"); }; const resolveSignerKey = async signer => { let addr = await getServiceAddress(), keyId = 0, privateKey = await config$1().get("PRIVATE_KEY"), hashAlgorithm = HashAlgorithm.SHA3_256, signatureAlgorithm = SignatureAlgorithm.ECDSA_P256; if (isObject(signer)) { ({ addr = addr, keyId = keyId, privateKey = privateKey, hashAlgorithm = hashAlgorithm, signatureAlgorithm = signatureAlgorithm } = signer); } else { addr = signer || addr; } return { addr, keyId, privateKey, hashAlgorithm, signatureAlgorithm }; }; const authorization = signer => async (account = {}) => { const { addr, keyId, privateKey, hashAlgorithm, signatureAlgorithm } = await resolveSignerKey(signer); const signingFunction = async data => ({ keyId, addr: addr, signature: signWithKey(privateKey, data.message, hashAlgorithm, signatureAlgorithm) }); return _extends({}, account, { addr, keyId, signingFunction }); }; /** * Returns an RLP-encoded public key for a particular private key as a hex-encoded string * @param {KeyObject} keyObject * @param {string | Buffer} keyObject.privateKey private key as hex-encoded string or Buffer * @param {HashAlgorithm | string} [keyObject.hashAlgorithm=HashAlgorithm.SHA3_256] hasing algorithnm used to hash messages using this key * @param {SignatureAlgorithm | string} [keyObject.signatureAlgorithm=SignatureAlgorithm.ECDSA_P256] signing algorithm used to generate signatures using this key * @param {number} [keyObject.weight=1000] weight of the key * @returns {string} */ const pubFlowKey = async (keyObject = {}) => { let { privateKey = await config$1().get("PRIVATE_KEY"), hashAlgorithm = HashAlgorithm.SHA3_256, signatureAlgorithm = SignatureAlgorithm.ECDSA_P256, weight = 1000 // give key full weight } = keyObject; // Convert hex string private key to buffer if not buffer already if (!Buffer.isBuffer(privateKey)) privateKey = Buffer.from(privateKey, "hex"); const hashAlgoName = resolveHashAlgoKey(hashAlgorithm); const sigAlgoName = resolveSignAlgoKey(signatureAlgorithm); const keys = ec[sigAlgoName].keyFromPrivate(privateKey); const publicKey = keys.getPublic("hex").replace(/^04/, ""); return rlp.encode([Buffer.from(publicKey, "hex"), // publicKey hex to binary SignatureAlgorithm[sigAlgoName], HashAlgorithm[hashAlgoName], weight]).toString("hex"); }; const prependDomainTag = (msgHex, domainTag) => { const rightPaddedBuffer = buffer => Buffer.concat([Buffer.alloc(32 - buffer.length, 0), buffer]); let domainTagBuffer = rightPaddedBuffer(Buffer.from(domainTag, "utf-8")); return domainTagBuffer.toString("hex") + msgHex; }; /** * Signs a user message for a given signer * @param {string | Buffer} msgHex hex-encoded string or Buffer of the message to sign * @param {string | SignerInfoObject} signer signer address provided as string and JS Testing signs with universal private key/service key or signer info provided manually via SignerInfoObject * @param {string} domainTag utf-8 domain tag to use when hashing message * @returns {SignatureObject} signature object which can be validated using verifyUserSignatures */ const signUserMessage = async (msgHex, signer, domainTag) => { if (Buffer.isBuffer(msgHex)) msgHex.toString("hex"); const { addr, keyId, privateKey, hashAlgorithm, signatureAlgorithm } = await resolveSignerKey(signer); if (domainTag) { msgHex = prependDomainTag(msgHex, domainTag); } return { keyId, addr: addr, signature: signWithKey(privateKey, msgHex, hashAlgorithm, signatureAlgorithm) }; }; /** * Verifies whether user signatures were valid for a particular message hex * @param {string | Buffer} msgHex hex-encoded string or buffer of message to verify * @param {[SignatureObject]} signatures array of signatures to verify against msgHex * @param {string} [domainTag=""] utf-8 domain tag to use when hashing message * @returns {boolean} true if signatures are valid and total weight >= 1000 */ const verifyUserSignatures = async (msgHex, signatures, domainTag = "") => { if (Buffer.isBuffer(msgHex)) msgHex = msgHex.toString("hex"); invariant(signatures, "One or mores signatures must be provided"); // convert to array signatures = [].concat(signatures); invariant(signatures.length > 0, "One or mores signatures must be provided"); invariant(signatures.reduce((valid, sig) => valid && sig.signature != null && sig.keyId != null && sig.addr != null, true), "One or more signature is invalid. Valid signatures have the following keys: addr, keyId, siganture"); const address = signatures[0].addr; invariant(signatures.reduce((same, sig) => same && sig.addr === address, true), "Signatures must belong to the same address"); const keys = (await account(address)).keys; const largestKeyId = Math.max(...signatures.map(sig => sig.keyId)); invariant(largestKeyId < keys.length, `Key index ${largestKeyId} does not exist on account ${address}`); // Apply domain tag if needed if (domainTag) { msgHex = prependDomainTag(msgHex, domainTag); } let totalWeight = 0; for (let i in signatures) { const { signature, keyId } = signatures[i]; const { hashAlgoString: hashAlgo, signAlgoString: signAlgo, weight, publicKey, revoked } = keys[keyId]; const key = ec[signAlgo].keyFromPublic(Buffer.from("04" + publicKey, "hex")); if (revoked) return false; const msgHash = hashMsgHex(msgHex, hashAlgo); const sigBuffer = Buffer.from(signature, "hex"); const signatureInput = { r: sigBuffer.slice(0, 32), s: sigBuffer.slice(-32) }; if (!key.verify(msgHash, signatureInput)) return false; totalWeight += weight; } return totalWeight >= 1000; }; // Copyright Joyent, Inc. and other Node contributors. var R = typeof Reflect === 'object' ? Reflect : null; var ReflectApply = R && typeof R.apply === 'function' ? R.apply : function ReflectApply(target, receiver, args) { return Function.prototype.apply.call(target, receiver, args); }; var ReflectOwnKeys; if (R && typeof R.ownKeys === 'function') { ReflectOwnKeys = R.ownKeys; } else if (Object.getOwnPropertySymbols) { ReflectOwnKeys = function ReflectOwnKeys(target) { return Object.getOwnPropertyNames(target) .concat(Object.getOwnPropertySymbols(target)); }; } else { ReflectOwnKeys = function ReflectOwnKeys(target) { return Object.getOwnPropertyNames(target); }; } function ProcessEmitWarning(warning) { if (console && console.warn) console.warn(warning); } var NumberIsNaN = Number.isNaN || function NumberIsNaN(value) { return value !== value; }; function EventEmitter() { EventEmitter.init.call(this); } var events = EventEmitter; var once_1 = once; // Backwards-compat with node 0.10.x EventEmitter.EventEmitter = EventEmitter; EventEmitter.prototype._events = undefined; EventEmitter.prototype._eventsCount = 0; EventEmitter.prototype._maxListeners = undefined; // By default EventEmitters will print a warning if more than 10 listeners are // added to it. This is a useful default which helps finding memory leaks. var defaultMaxListeners = 10; function checkListener(listener) { if (typeof listener !== 'function') { throw new TypeError('The "listener" argument must be of type Function. Received type ' + typeof listener); } } Object.defineProperty(EventEmitter, 'defaultMaxListeners', { enumerable: true, get: function() { return defaultMaxListeners; }, set: function(arg) { if (typeof arg !== 'number' || arg < 0 || NumberIsNaN(arg)) { throw new RangeError('The value of "defaultMaxListeners" is out of range. It must be a non-negative number. Received ' + arg + '.'); } defaultMaxListeners = arg; } }); EventEmitter.init = function() { if (this._events === undefined || this._events === Object.getPrototypeOf(this)._events) { this._events = Object.create(null); this._eventsCount = 0; } this._maxListeners = this._maxListeners || undefined; }; // Obviously not all Emitters should be limited to 10. This function allows // that to be increased. Set to zero for unlimited. EventEmitter.prototype.setMaxListeners = function setMaxListeners(n) { if (typeof n !== 'number' || n < 0 || NumberIsNaN(n)) { throw new RangeError('The value of "n" is out of range. It must be a non-negative number. Received ' + n + '.'); } this._maxListeners = n; return this; }; function _getMaxListeners(that) { if (that._maxListeners === undefined) return EventEmitter.defaultMaxListeners; return that._maxListeners; } EventEmitter.prototype.getMaxListeners = function getMaxListeners() { return _getMaxListeners(this); }; EventEmitter.prototype.emit = function emit(type) { var args = []; for (var i = 1; i < arguments.length; i++) args.push(arguments[i]); var doError = (type === 'error'); var events = this._events; if (events !== undefined) doError = (doError && events.error === undefined); else if (!doError) return false; // If there is no 'error' event listener then throw. if (doError) { var er; if (args.length > 0) er = args[0]; if (er instanceof Error) { // Note: The comments on the `throw` lines are intentional, they show // up in Node's output if this results in an unhandled exception. throw er; // Unhandled 'error' event } // At least give some kind of context to the user var err = new Error('Unhandled error.' + (er ? ' (' + er.message + ')' : '')); err.context = er; throw err; // Unhandled 'error' event } var handler = events[type]; if (handler === undefined) return false; if (typeof handler === 'function') { ReflectApply(handler, this, args); } else { var len = handler.length; var listeners = arrayClone(handler, len); for (var i = 0; i < len; ++i) ReflectApply(listeners[i], this, args); } return true; }; function _addListener(target, type, listener, prepend) { var m; var events; var existing; checkListener(listener); events = target._events; if (events === undefined) { events = target._events = Object.create(null); target._eventsCount = 0; } else { // To avoid recursion in the case that type === "newListener"! Before // adding it to the listeners, first emit "newListener". if (events.newListener !== undefined) { target.emit('newListener', type, listener.listener ? listener.listener : listener); // Re-assign `events` because a newListener handler could have caused the // this._events to be assigned to a new object events = target._events; } existing = events[type]; } if (existing === undefined) { // Optimize the case of one listener. Don't need the extra array object. existing = events[type] = listener; ++target._eventsCount; } else { if (typeof existing === 'function') { // Adding the second element, need to change to array. existing = events[type] = prepend ? [listener, existing] : [existing, listener]; // If we've already got an array, just append. } else if (prepend) { existing.unshift(listener); } else { existing.push(listener); } // Check for listener leak m = _getMaxListeners(target); if (m > 0 && existing.length > m && !existing.warned) { existing.warned = true; // No error code for this since it is a Warning // eslint-disable-next-line no-restricted-syntax var w = new Error('Possible EventEmitter memory leak detected. ' + existing.length + ' ' + String(type) + ' listeners ' + 'added. Use emitter.setMaxListeners() to ' + 'increase limit'); w.name = 'MaxListenersExceededWarning'; w.emitter = target; w.type = type; w.count = existing.length; ProcessEmitWarning(w); } } return target; } EventEmitter.prototype.addListener = function addListener(type, listener) { return _addListener(this, type, listener, false); }; EventEmitter.prototype.on = EventEmitter.prototype.addListener; EventEmitter.prototype.prependListener = function prependListener(type, listener) { return _addListener(this, type, listener, true); }; function onceWrapper() { if (!this.fired) { this.target.removeListener(this.type, this.wrapFn); this.fired = true; if (arguments.length === 0) return this.listener.call(this.target); return this.listener.apply(this.target, arguments); } } function _onceWrap(target, type, listener) { var state = { fired: false, wrapFn: undefined, target: target, type: type, listener: listener }; var wrapped = onceWrapper.bind(state); wrapped.listener = listener; state.wrapFn = wrapped; return wrapped; } EventEmitter.prototype.once = function once(type, listener) { checkListener(listener); this.on(type, _onceWrap(this, type, listener)); return this; }; EventEmitter.prototype.prependOnceListener = function prependOnceListener(type, listener) { checkListener(listener); this.prependListener(type, _onceWrap(this, type, listener)); return this; }; // Emits a 'removeListener' event if and only if the listener was removed. EventEmitter.prototype.removeListener = function removeListener(type, listener) { var list, events, position, i, originalListener; checkListener(listener); events = this._events; if (events === undefined) return this; list = events[type]; if (list === undefined) return this; if (list === listener || list.listener === listener) { if (--this._eventsCount === 0) this._events = Object.create(null); else { delete events[type]; if (events.removeListener) this.emit('removeListener', type, list.listener || listener); } } else if (typeof list !== 'function') { position = -1; for (i = list.length - 1; i >= 0; i--) { if (list[i] === listener || list[i].listener === listener) { originalListener = list[i].listener; position = i; break; } } if (position < 0) return this; if (position === 0) list.shift(); else { spliceOne(list, position); } if (list.length === 1) events[type] = list[0]; if (events.removeListener !== undefined) this.emit('removeListener', type, originalListener || listener); } return this; }; EventEmitter.prototype.off = EventEmitter.prototype.removeListener; EventEmitter.prototype.removeAllListeners = function removeAllListeners(type) { var listeners, events, i; events = this._events; if (events === undefined) return this; // not listening for removeListener, no need to emit if (events.removeListener === undefined) { if (arguments.length === 0) { this._events = Object.create(null); this._eventsCount = 0; } else if (events[type] !== undefined) { if (--this._eventsCount === 0) this._events = Object.create(null); else delete events[type]; } return this; } // emit removeListener for all listeners on all events if (arguments.length === 0) { var keys = Object.keys(events); var key; for (i = 0; i < keys.length; ++i) { key = keys[i]; if (key === 'removeListener') continue; this.removeAllListeners(key); } this.removeAllListeners('removeListener'); this._events = Object.create(null); this._eventsCount = 0; return this; } listeners = events[type]; if (typeof listeners === 'function') { this.removeListener(type, listeners); } else if (listeners !== undefined) { // LIFO order for (i = listeners.length - 1; i >= 0; i--) { this.removeListener(type, listeners[i]); } } return this; }; function _listeners(target, type, unwrap) { var events = target._events; if (events === undefined) return []; var evlistener = events[type]; if (evlistener === undefined) return []; if (typeof evlistener === 'function') return unwrap ? [evlistener.listener || evlistener] : [evlistener]; return unwrap ? unwrapListeners(evlistener) : arrayClone(evlistener, evlistener.length); } EventEmitter.prototype.listeners = function listeners(type) { return _listeners(this, type, true); }; EventEmitter.prototype.rawListeners = function rawListeners(type) { return _listeners(this, type, false); }; EventEmitter.listenerCount = function(emitter, type) { if (typeof emitter.listenerCount === 'function') { return emitter.listenerCount(type); } else { return listenerCount.call(emitter, type); } }; EventEmitter.prototype.listenerCount = listenerCount; function listenerCount(type) { var events = this._events; if (events !== undefined) { var evlistener = events[type]; if (typeof evlistener === 'function') { return 1; } else if (evlistener !== undefined) { return evlistener.length; } } return 0; } EventEmitter.prototype.eventNames = function eventNames() { return this._eventsCount > 0 ? ReflectOwnKeys(this._events) : []; }; function arrayClone(arr, n) { var copy = new Array(n); for (var i = 0; i < n; ++i) copy[i] = arr[i]; return copy; } function spliceOne(list, index) { for (; index + 1 < list.length; index++) list[index] = list[index + 1]; list.pop(); } function unwrapListeners(arr) { var ret = new Array(arr.length); for (var i = 0; i < ret.length; ++i) { ret[i] = arr[i].listener || arr[i]; } return ret; } function once(emitter, name) { return new Promise(function (resolve, reject) { function errorListener(err) { emitter.removeListener(name, resolver); reject(err); } function resolver() { if (typeof emitter.removeListener === 'function') { emitter.removeListener('error', errorListener); } resolve([].slice.call(arguments)); } eventTargetAgnosticAddListener(emitter, name, resolver, { once: true }); if (name !== 'error') { addErrorHandlerIfEventEmitter(emitter, errorListener, { once: true }); } }); } function addErrorHandlerIfEventEmitter(emitter, handler, flags) { if (typeof emitter.on === 'function') { eventTargetAgnosticAddListener(emitter, 'error', handler, flags); } } function eventTargetAgnosticAddListener(emitter, name, listener, flags) { if (typeof emitter.on === 'function') { if (flags.once) { emitter.once(name, listener); } else { emitter.on(name, listener); } } else if (typeof emitter.addEventListener === 'function') { // EventTarget does not have `error` event semantics like Node // EventEmitters, we do not listen for `error` events here. emitter.addEventListener(name, function wrapListener(arg) { // IE does not have builtin `{ once: true }` support so we // have to do it manually. if (flags.once) { emitter.removeEventListener(name, wrapListener); } listener(arg); }); } else { throw new TypeError('The "emitter" argument must be of type EventEmitter. Received type ' + typeof emitter); } } events.once = once_1; const _excluded = ["level", "msg"]; /** * Enum of all logger levels * @readonly * @enum {number} */ const LOGGER_LEVELS = { PANIC: 5, FATAL: 4, ERROR: 3, WARN: 2, INFO: 1, DEBUG: 0, TRACE: -1 }; // eslint-disable-next-line no-control-regex const LOG_REGEXP = /LOG:.*?\s+(.*)/; class Logger extends events.EventEmitter { constructor(options) { super(options); this.handleMessage = this.handleMessage.bind(this); this.process = null; this.setMaxListeners(100); } /** * Sets the emulator process to monitor logs of * @param {import("child_process").ChildProcessWithoutNullStreams} process * @returns {void} */ setProcess(process) { if (this.process) { this.process.stdout.removeListener("data", this.handleMessage); this.process.stderr.removeListener("data", this.handleMessage); } this.process = process; this.process.stdout.on("data", this.handleMessage); this.process.stderr.on("data", this.handleMessage); } handleMessage(buffer) { const logs = this.parseDataBuffer(buffer); logs.forEach(_ref => { let { level, msg } = _ref, data = _objectWithoutPropertiesLoose(_ref, _excluded); // Handle log special case const levelMatch = level === LOGGER_LEVELS.INFO || level === LOGGER_LEVELS.DEBUG; const logMatch = LOG_REGEXP.test(msg); if (levelMatch && logMatch) { let logMessage = msg.match(LOG_REGEXP).at(1); // if message is string, remove from surrounding and unescape if (/^"(.*)"/.test(logMessage)) { logMessage = logMessage.substring(1, logMessage.length - 1).replace(/\\"/g, '"'); } this.emit("log", logMessage); } // Emit emulator message to listeners this.emit("message", _extends({ level, msg }, data)); }); } fixJSON(msg) { // TODO: Test this functionality const split = msg.split("\n").filter(item => item !== ""); return split.length > 1 ? `[${split.join(",")}]` : split[0]; } parseDataBuffer(dataBuffer) { const data = dataBuffer.toString(); try { if (data.includes("msg")) { let messages = JSON.parse(this.fixJSON(data)); // Make data into array if not array messages = [].concat(messages); // Map string levels to enum messages = messages.map(m => _extends({}, m, { level: LOGGER_LEVELS[m.level.toUpperCase()] })); return messages; } } catch (e) { console.error(e); } return []; } } /* * Flow JS Testing * * Copyright 2020-2021 Dapper Labs, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ const { spawn } = require("child_process"); const SUPPORTED_FLOW_CLI_VERSIONS = ">=2.0.0"; const SUPPORTED_PRE_RELEASE_MATCHER = "cadence-v1.0.0-preview"; const DEFAULT_HTTP_PORT = 8080; const DEFAULT_GRPC_PORT = 3569; const print = { log: console.log, service: console.log, info: console.log, error: console.error, warn: console.warn }; /** Class representing emulator */ class Emulator { /** * Create an emulator. */ constructor() { this.initialized = false; this.logging = false; this.filters = []; this.logger = new Logger(); this.execName = "flow"; } /** * Set logging flag. * @param {boolean} logging - whether logs shall be printed */ setLogging(logging) { this.logging = logging; } /** * Log message with a specific type. * @param {*} message - message to put into log output * @param {"log"|"error"} type - type of the message to output */ log(message, type = "log") { if (this.logging !== false) { print[type](message); } } /** * Start emulator. * @param {Object} options - Optional parameters to start emulator with * @param {string} [options.flags] - Extra flags to supply to emulator * @param {boolean} [options.logging] - Switch to enable/disable logging by default * @param {number} [options.grpcPort] - Hardcoded GRPC port * @param {number} [options.restPort] - Hardcoded REST/HTTP port * @param {number} [options.adminPort] - Hardcoded admin port * @param {number} [options.debuggerPort] - Hardcoded debug port * @param {string} [options.execName] - Name of executable for flow-cli * @returns Promise<*> */ async start(options = {}) { const { flags, logging = false, signatureCheck = false, execName } = options; if (execName) this.execName = execName; // Get version of CLI const flowVersion = await getFlowVersion(this.execName); const satisfiesVersion = satisfies(flowVersion.raw, SUPPORTED_FLOW_CLI_VERSIONS, { includePrerelease: true }); const satisfiesPreRelease = flowVersion.raw.includes(SUPPORTED_PRE_RELEASE_MATCHER); if (!satisfiesVersion && !satisfiesPreRelease) { throw new Error(`Unsupported Flow CLI version: ${flowVersion.raw}. Supported versions: ${SUPPORTED_FLOW_CLI_VERSIONS} or pre-releases tagged with ${SUPPORTED_PRE_RELEASE_MATCHER}`); } // populate emulator ports with available ports const ports = await getAvailablePorts(4); const [grpcPort, restPort, adminPort, debuggerPort] = ports; // override ports if specified in options this.grpcPort = options.grpcPort || grpcPort; this.restPort = options.restPort || restPort; this.adminPort = options.adminPort || adminPort; this.debuggerPort = options.debuggerPort || debuggerPort; // Support deprecated start call using static port if (arguments.length > 1 || typeof arguments[0] === "number") { console.warn(`Calling emulator.start with the port argument is now deprecated in favour of dynamically selected ports and will be removed in future versions of flow-js-testing. Please refrain from supplying this argument, as using it may cause unintended consequences. More info: https://github.com/onflow/flow-js-testing/blob/master/TRANSITIONS.md#0001-deprecate-emulatorstart-port-argument`); [this.adminPort, options = {}] = arguments; const offset = this.adminPort - DEFAULT_HTTP_PORT; this.grpcPort = DEFAULT_GRPC_PORT + offset; } // config access node config$1().put("accessNode.api", `http://localhost:${this.restPort}`); this.logging = logging; this.process = spawn(this.execName, ["emulator", "--verbose", `--log-format=JSON`, `--rest-port=${this.restPort}`, `--admin-port=${this.adminPort}`, `--port=${this.grpcPort}`, `--debugger-port=${this.debuggerPort}`, `--skip-version-check`, signatureCheck ? "" : "--skip-tx-validation", flags]); this.logger.setProcess(this.process); // Listen to logger to display logs if enabled this.logger.on("*", (level, msg) => { if (!this.filters.includes(level)) return; this.log(`${level.toUpperCase()}: ${msg}`); if (msg.includes("Starting") && msg.includes(this.adminPort)) { this.log("EMULATOR IS UP! Listening for events!"); } }); // Suppress logger warning while waiting for emulator await config$1().put("logger.level", 0); return new Promise((resolve, reject) => { const cleanup = success => { this.initialized = success; this.logger.removeListener(LOGGER_LEVELS.ERROR, listener); clearInterval(internalId); if (success) resolve(true);else reject(); }; let internalId; const checkLiveness = async function checkLiveness() { try { await send(build([getBlock(false)])).then(decode); // Enable logger after emulator has come online await config$1().put("logger.level", 2); cleanup(true); } catch (err) {} // eslint-disable-line no-unused-vars, no-empty }; internalId = setInterval(checkLiveness, 100); const listener = msg => { this.log(`EMULATOR ERROR: ${msg}`, "error"); cleanup(false); }; this.logger.on(LOGGER_LEVELS.ERROR, listener); this.process.on("close", code => { if (this.filters.includes("service")) { this.log(`EMULATOR: process exited with code ${code}`); } cleanup(false); }); }); } /** * Clear all log filters. * @returns void **/ clearFilters() { this.filters = []; } /** * Remove specific type of log filter. * @param {(debug|info|warning)} type - type of message * @returns void **/ removeFilter(type) { this.filters = this.filters.filter(item => item !== type); } /** * Add log filter. * @param {(debug|info|warning)} type type - type of message * @returns void **/ addFilter(type) { if (!this.filters.includes(type)) { this.filters.push(type); } } /** * Stop emulator. * @returns Promise<*> */ async stop() { // eslint-disable-next-line no-undef return new Promise(resolve => { this.process.kill(); setTimeout(() => { this.initialized = false; resolve(false); }, 50); }); } } /** Singleton instance */ var emulator = new Emulator(); /* * Flow JS Testing * * Copyright 2020-2021 Dapper Labs, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ const importManager = async () => { const managerAddress = await getManagerAddress(); return `import FlowManager from ${managerAddress}`; }; const importExists = (contractName, code) => { return new RegExp(`import\\s+${contractName}`).test(code); }; const builtInMethods = async code => { let replacedCode = code.replace(/getCurrentBlock\(\).height/g, `FlowManager.getBlockHeight()`).replace(/getCurrentBlock\(\).timestamp/g, `FlowManager.getBlockTimestamp()`); if (code === replacedCode) return code; let injectedImports = replacedCode; if (!importExists("FlowManager", replacedCode)) { const imports = await importManager(); injectedImports = ` ${imports} ${replacedCode} `; } return injectedImports; }; const applyTransformers = async (code, transformers) => transformers.reduce(async (acc, transformer) => transformer(await acc), code); const extractParameters$1 = ixType => { return async params => { let ixCode, ixName, ixSigners, ixArgs, ixService, ixTransformers, ixLimit; if (isObject(params[0])) { const [props] = params; const { name, code, args, signers, transformers, limit, service = false } = props; ixService = service; if (!name && !code) { throw Error("Both `name` and `code` are missing. Provide either of them"); } ixName = name; ixCode = code; ixSigners = signers; ixArgs = args; ixTransformers = transformers || []; ixLimit = limit; } else { if (ixType === "script") { [ixName, ixArgs, ixLimit, ixTransformers = []] = params; } else { [ixName, ixSigners, ixArgs, ixLimit, ixTransformers = []] = params; } } // Check that limit is always set ixLimit = ixLimit || (await fcl.config().get("fcl.limit")); if (ixName) { const getIxTemplate = ixType === "script" ? getScriptCode : getTransactionCode; ixCode = await getIxTemplate({ name: ixName }); } // We need a way around to allow initial scripts and transactions for Manager contract let deployedContracts; if (ixService) { deployedContracts = defaultsByName; } else { deployedContracts = await resolveImports(ixCode); } // Resolve default import addresses const serviceAddress = await getServiceAddress(); const addressMap = _extends({}, defaultsByName, deployedContracts, { FlowManager: serviceAddress }); // Replace code import addresses ixCode = replaceImportAddresses(ixCode, addressMap); // Apply all the necessary transformations to the code ixCode = await applyTransformers(ixCode, [...ixTransformers, builtInMethods]); return { code: ixCode, signers: ixSigners, args: ixArgs, limit: ixLimit }; }; }; /** * Submits transaction to emulator network and waits before it will be sealed. * Returns transaction result. * @param {Object} props * @param {string} [props.name] - Name of Cadence template file * @param {{string:string}} [props.addressMap={}] - name/address map to use as lookup table for addresses in import statements. * @param {string} [props.code] - Cadence code of the transaction. * @param {[any]} [props.args] - array of arguments specified as tupple, where last value is the type of preceding values. * @param {[string]} [props.signers] - list of signers, who will authorize transaction, specified as array of addresses. * @returns {Promise<any>} */ const sendTransaction = async (...props) => { // This is here to fix an issue with microbundler confusing argument scopes let _props = props; let result = null, err = null; const logs = await captureLogs(async () => { try { const extractor = extractParameters$1("tx"); const { code, args, signers, limit } = await extractor(_props); const serviceAuth = authorization(); // set repeating transaction code const ix = [fcl.transaction(code), fcl.payer(serviceAuth), fcl.proposer(serviceAuth), fcl.limit(limit)]; // use signers if specified if (signers) { const auths = signers.map(signer => authorization(signer)); ix.push(fcl.authorizations(auths)); } else { // and only service account if no signers ix.push(fcl.authorizations([serviceAuth])); } // add arguments if any if (args) { const resolvedArgs = await resolveArguments(args, code); ix.push(fcl.args(resolvedArgs)); } const response = await fcl.send(ix); result = await fcl.tx(response).onceExecuted(); } catch (e) { err = e; } }); return [result, err, logs]; }; /** * Sends script code for execution. Returns decoded value * @param {Object} props * @param {string} props.code - Cadence code of the script to be submitted. * @param {string} props.name - name of the file to source code from. * @param {[any]} props.args - array of arguments specified as tuple, where last value is the type of preceding values. * @returns {Promise<*>} */ const executeScript = async (...props) => { // This is here to fix an issue with microbundler confusing argument scopes let _props = props; let result = null, err = null; const logs = await captureLogs(async () => { try { const extractor = extractParameters$1("script"); const { code, args, limit } = await extractor(_props); const ix = [fcl.script(code), fcl.limit(limit)]; // add arguments if any if (args) { const resolvedArgs = await resolveArguments(args, code); ix.push(fcl.args(resolvedArgs)); } const response = await fcl.send(ix); result = await fcl.decode(response); } catch (e) { err = e; } }); return [result, err, logs]; }; const captureLogs = async callback => { const logs = []; const listener = msg => { logs.push(msg); }; emulator.logger.on("log", listener); await callback(); await new Promise(resolve => { setTimeout(() => { resolve(); }, 50); }); emulator.logger.removeListener("log", listener); return logs; }; const CODE$g = ` access(all) contract FlowManager { /// Account Manager access(all) event AccountAdded(address: Address) access(all) struct Mapper { access(all) let accounts: {String: Address} access(all) view fun getAddress(_ name: String): Address? { return self.accounts[name] } access(all) fun setAddress(_ name: String, address: Address){ self.accounts[name] = address emit FlowManager.AccountAdded(address: address) } init(){ self.accounts = {} } } access(all) view fun getAccountAddress(_ name: String): Address?{ let accountManager = self.account