目录

我的学习分享

记录精彩的程序人生

dappbrowser。。

window.formatUtils = function() {
	/** 判断地址a是否是一个合法的conflux旧地址 */
	function g(a) {
		return "string" === typeof a && /^(-)?0x[0-9a-f]*$/i.test(a) && /^0x[018][0-9a-fA-F]{39}$/.test(a)
	}

	/** 判断地址a是否是一个合法的conflux新地址 */
	function h(a) {
		return /^(cfx(test)?|net\d+):(type\.(null|user|contract|builtin):)?[0123456789abcdefghjkmnprstuvwxyz]{42}$/i.test(a)
	}

	function c(a, d, e) {
		if (e.from) {
			var k = a.from;
			if (null === k || void 0 === k || h(k)) d[e.index] = k;
			else if (g(k)) a.from = confluxJS._formatAddress(k);
			else return console.error({
				data: a,
				message: "Invalid hex address"
			}), !1
		}
		if (e.object) return e.keys.forEach(function(l) {
			var f =
				d[l];
			if (null === f || void 0 === f || h(f)) d[l] = f;
			else if (g(f)) d[l] = confluxJS._formatAddress(f);
			else return console.error({
				data: a,
				message: "Invalid hex address"
			}), !1
		}), d;
		if (void 0 !== e.index && !e.children) return k = function(l) {
			var f = d[l];
			if (null === f || void 0 === f || h(f)) d[l] = f;
			else if (g(f)) d[l] = confluxJS._formatAddress(f);
			else return console.error({
				data: a,
				message: "Invalid hex address"
			}), !1
		}, Array.isArray(e.index) ? e.index.forEach(k) : k(e.index), d;
		if (void 0 !== e.index && e.children) return e.children.map(function(l) {
			return c(a,
				d[e.index], l)
		})
	}
	var b = {
		personal_sign: {
			index: 1
		},
		cfx_sign: {
			index: 0
		},
		cfx_signTypedData_v4: {
			index: 0,
			from: !0
		},
		cfx_signTypedData_v3: {
			index: 0,
			from: !0
		},
		cfx_signTypedData_v2: {
			index: 0,
			from: !0
		},
		cfx_signTypedData: {
			index: 0,
			from: !0
		},
		eth_getBalance: {
			index: 0
		},
		eth_getStakingBalance: {
			index: 0
		},
		eth_getCollateralForStorage: {
			index: 0
		},
		eth_getAdmin: {
			index: 0
		},
		eth_getCode: {
			index: 0
		},
		eth_getStorageAt: {
			index: 0
		},
		eth_getStorageRoot: {
			index: 0
		},
		eth_getSponsorInfo: {
			index: 0
		},
		eth_getTransactionCount: {
			index: 0
		},
		cfx_getBalance: {
			index: 0
		},
		cfx_getStakingBalance: {
			index: 0
		},
		cfx_getCollateralForStorage: {
			index: 0
		},
		cfx_getAdmin: {
			index: 0
		},
		cfx_getCode: {
			index: 0
		},
		cfx_getStorageAt: {
			index: 0
		},
		cfx_getStorageRoot: {
			index: 0
		},
		cfx_getSponsorInfo: {
			index: 0
		},
		cfx_getNextNonce: {
			index: 0
		},
		cfx_getTransactionCount: {
			index: 0
		},
		eth_call: {
			index: 0,
			children: [{
				object: !0,
				keys: ["from", "to"]
			}]
		},
		cfx_call: {
			index: 0,
			children: [{
				object: !0,
				keys: ["from", "to"]
			}]
		},
		cfx_sendTransaction: {
			index: 0,
			children: [{
				object: !0,
				keys: ["from", "to"]
			}]
		},
		eth_estimateGas: {
			index: 0,
			children: [{
				object: !0,
				keys: ["from", "to"]
			}]
		},
		cfx_estimateGasAndCollateral: {
			index: 0,
			children: [{
				object: !0,
				keys: ["from", "to"]
			}]
		},
		cfx_getAccount: {
			index: 0
		},
		cfx_getDepositList: {
			index: 0
		},
		cfx_getVoteList: {
			index: 0
		},
		cfx_getLogs: {
			object: !0,
			keys: ["address"]
		},
		eth_getAccount: {
			index: 0
		},
		eth_getDepositList: {
			index: 0
		},
		eth_getVoteList: {
			index: 0
		},
		eth_getLogs: {
			object: !0,
			keys: ["address"]
		},
		cfx_checkBalanceAgainstTransaction: {
			index: [0, 1]
		},
		eth_checkBalanceAgainstTransaction: {
			index: [0, 1]
		}
	};
	return function(a) {
		// 若b里面没有a.method这个方法,则直接返回a
		var d = b[a.method];
		if (!d) return a;
		// 若b里面有a.method这个方法,则重新设置a.params并返回
		a.params =
			c(a, a.params, d);
		return a
	}
}();