gamegold 5.4.2 → 5.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/gamegold-worker.js +1 -1
- package/lib/gamegold.js +3 -3
- package/package.json +1 -1
package/lib/gamegold-worker.js
CHANGED
|
@@ -196,7 +196,7 @@ var r=i(126),a=r.Buffer;function s(e,t){for(var i in e)t[i]=e[i]}function n(e,t,
|
|
|
196
196
|
* pkg.js - package constants
|
|
197
197
|
* Copyright (c) 2019-2022, Bookman Software (MIT License).
|
|
198
198
|
* https://github.com/bookmansoft/gamegold
|
|
199
|
-
*/t.protocol="vallnet",t.version="
|
|
199
|
+
*/t.protocol="vallnet",t.version="5.4.3",t.url="https://github.com/bookmansoft/gamegold"},function(e,t,i){"use strict";
|
|
200
200
|
/*!
|
|
201
201
|
* common.js - p2p constants for vallnet
|
|
202
202
|
* Copyright (c) 2019-2022, Bookman Software (MIT License).
|
package/lib/gamegold.js
CHANGED
|
@@ -206,7 +206,7 @@ var r=i(126),s=r.Buffer;function n(e,t){for(var i in e)t[i]=e[i]}function a(e,t,
|
|
|
206
206
|
* pkg.js - package constants
|
|
207
207
|
* Copyright (c) 2019-2022, Bookman Software (MIT License).
|
|
208
208
|
* https://github.com/bookmansoft/gamegold
|
|
209
|
-
*/t.protocol="vallnet",t.version="
|
|
209
|
+
*/t.protocol="vallnet",t.version="5.4.3",t.url="https://github.com/bookmansoft/gamegold"},function(e,t,i){"use strict";
|
|
210
210
|
/*!
|
|
211
211
|
* common.js - p2p constants for vallnet
|
|
212
212
|
* Copyright (c) 2019-2022, Bookman Software (MIT License).
|
|
@@ -877,7 +877,7 @@ var r=i(776),s=i(778),n=i(779),a=i(780),o=i(781),c=i(782),h=i(783);i=i(784),s=[]
|
|
|
877
877
|
/*!
|
|
878
878
|
* chaindb.js - blockchain data management for vallnet
|
|
879
879
|
* Copyright (c) 2019-2022, Bookman Software (MIT License).
|
|
880
|
-
*/const r=i(0),s=i(2),n=i(1),a=i(4),o=i(33),c=i(3),h=i(15),u=i(60),l=i(246),p=i(167),d=i(328),f=i(64),m=i(75),g=i(45),y=i(37),v=i(13),b=i(329),w=i(330),k=i(146),x=c.U8,S=c.U32,{getVerifyMsg:_,BlockFinalType:E,ContractEnvType:R,ContractType:A}=i(5);var O=i(72);const C=i(50),I=i(20),j=i(69),P=i(113),T=i(28),B=i(6),N=i(144),M=i(7),L=i(159),F=i(118),z=F.scItem,H=i(339),D=i(312),U=D.cpItem,q=i(132),V=q.vpItem,K=i(340),W=K.vpItem,$=i(729),X=i(153),G=X.ErItem,Y=i(154),J=Y.ErAbolishItem,Z=i(227),Q=Z.EnchancementItem;i(54);const ee=i(341),te=i(117),{opcodes:ie,nestingCodes:re}=i(19),se=i(123),ne=i(26),ae=i(39),oe=i(32).VerifyError;class ce extends O{constructor(e){super(),this.chain=e,this.eid="chaindb",this.fromOptions(e.options),this.db=p(this.options),this.stateCache=new le(this.network),this.state=new ue,this.pending=null,this.current=null,this.coinCache=new f(this.options.coinCache),this.cacheHash=new f(this.options.entryCache),this.cacheHeight=new f(this.options.entryCache),this.autoTaskMgr=new L(this),this.propList=new q(this),this.htlcList=new K(this),this.$voteMgr=new $(this),this.transactionList=new H(this),this.cpList=new D(this),this.scList=new F(this),this.erList=new X(this),this.erAbList=new Y(this),this.enCpList=new Z(this,{type:"cp"}),this.middleHashChain=new Set}get hmacConnection(){return this.node.specialCp.content()}get voteMgr(){return this.$voteMgr}get mode(){return R.Block}fromOptions(e){this.options=e,this.node=e.node,this.network=e.network,this.logger=e.logger.context("chaindb")}get curHeight(){return this.chain.height}get finder(){return this.node.getTXFromAll?this.node.getTXFromAll.bind(this.node):()=>{}}get tidCreator(){return d.tx}rpcExecute(){return this.node.rpc.execute.apply(this.node.rpc,arguments)}createGenesisCoinbase(e,t){var i,r=new I,s=new P,n=((i=((i=(s.script.pushInt(0),s.script.pushData(c.ZERO_HASH160),Buffer.allocUnsafe(4))).writeUInt32LE(0,0,!0),s.script.pushData(i),s.script.pushData(c.ZERO_U64),s.script.compile(),s.witness.push(c.ZERO_HASH),s.witness.compile(),r.inputs.push(s),new T)).script.createScript(c.ZERO_HASH160),i.value=M.ORIGINAL,r.outputs.push(i),c.ZERO_HASH),a=[],[a,,]=(a.push(c.ZERO_HASH),N.createRoot(a));a=B.root256(a,n);return(n=new T).script.fromCommitment(a),r.outputs.push(n),s.script.setData(1,t),s.script.compile(),i.script.fromAddress(e.addresses[0]),r}dummyInput(){return P.fromOutpoint(new g(c.NULL_HASH,0))}createGenesisBlock(e){if(!e.addresses||e.addresses.length<11)throw new Error("addresses length expect 11.");let t=e.flags;t=t||Buffer.from("In the name of Athena: Of the user, By the user, and For the user.","ascii");var i=this.createGenesisCoinbase(e,t),r=this.network.genesisId,n=new j,[n,,]=(n.addInput(this.dummyInput()),n.cpRegister(e.addresses[0],M.COIN,{cid:r,oper:A.cpRegister,name:"ATHENA",ip:"*",pubAddress:"",pubKey:"",signMethod:""}),n.commit()),a=new j;a.addInput(this.dummyInput());for(let t=0;t<10;t++)for(let i=0;i<10;i++){var o={oper:A.propCreate,pid:""+this.network.tokenFix+s.sprintf("%04d",10*t+i+1),oid:this.network.tokenFix+"0000",cid:r,gold:M.COIN};a.propCreate(e.addresses[t+1],o)}var[h,,]=a.commit(),u=new m({version:e.version,prevBlock:c.NULL_HASH,merkleRoot:i.hash("hex"),time:e.time,bits:e.bits,nonce:e.nonce,height:0});return u.txs.push(i),u.txs.push(n),u.txs.push(h),u.refresh(!0),u.merkleRoot=u.createMerkleRoot("hex"),u}async open(e){this.logger.info("Opening ChainDB..."),await this.db.open();var t=await this.db.checkVersion(d,3);if(i=await this.getState()){var i;if(await this.verifyFlags(i),await this.verifyDeployments(),this.stateCache=await this.getStateCache(),this.state=i,!(i=await this.getEntry(0)))throw new Error("genesis block not found.");this.network.genesis.hash=i.hash,this.network.genesis.merkleRoot=i.merkleRoot,this.logger.info("ChainDB successfully loaded.")}else e&&(i=await this.getGenesisParams(e),this.node.miner&&(this.node.miner.addresses=[new v(i.coinbaseAddress)]),this.node.config)&&this.node.config.changeParams(i),await this.saveFlags(),await this.saveDeployments(),await this.saveGenesis(),this.logger.info("ChainDB successfully initialized.");return this.logger.info("Chain State: hash=%s tx=%d coin=%d value=%s.",this.state.rhash(),this.state.tx,this.state.coin,o.btc(this.state.value)),t}async getGenesisParams(e){if(e.isGenesisParams)return e;e=e||{};var t=new ee({name:"wallet-test",db:"memory",resolution:!1,verify:!1}),i=(t=(e.getMnemonicFromMemory=!0,await t.open(e),await t.get("primary")),[]);for(const s of await t.getPaths("default")){var r=s.toAddress();0==s.branch&&i.push(""+r.toString(e.network))}var s,n,a,o,h={};return h[e.network+"Addresses"]=i.reduce((e,t)=>e+(""==e?t:","+t),""),h.coinbaseAddress=i[1],h.notifyAddress=t.notifyAddress,this.node.config.notifyAddress=h.notifyAddress,await se.writeFile(h,e.network+"-genesis.params"),e.isGenesisEncrypt||(s=B.hash256(Buffer.from(t.master.mnemonic.toSeed().toString("hex"))),n=ne.publicKeyCreate(s,!0),a=v.fromWitnessPubkeyhash(B.hash160(n),this.network.type).toString(),o=B.hash256(Buffer.concat([Buffer.from("root"),t.master.key.privateKey])),t={body:{language:t.master.mnemonic.language,phrase:t.master.mnemonic.getPhrase(),passphrase:t.master.mnemonic.passphrase,awardAddress:h.coinbaseAddress,notifyAddress:t.notifyAddress,alliancePrivateKey:o.toString("hex"),allianceName:"root",allianceNodeId:0,allianceNodeToken:{prv:s.toString("hex"),pub:n.toString("hex"),address:a}},password:e.password||c.MNEMONIC_SALT,network:e.network,file:e.network+"-genesis.keystore"},await se.writeEncryptFile(t)),h}close(){return this.db.close()}start(){return r(!this.current),r(!this.pending),this.current=this.db.batch(),this.pending=this.state.clone(),this.coinCache.start(),this.cacheHash.start(),this.cacheHeight.start(),this.current}put(e,t){r(this.current),this.current.put(e,t)}del(e){r(this.current),this.current.del(e)}batch(){return r(this.current),this.current}drop(){var e=this.current;r(this.current),r(this.pending),this.current=null,this.pending=null,this.coinCache.drop(),this.cacheHash.drop(),this.cacheHeight.drop(),this.stateCache.drop(),e.clear()}async commit(){r(this.current),r(this.pending);try{await this.current.write()}catch(e){throw this.current=null,this.pending=null,this.coinCache.drop(),this.cacheHash.drop(),this.cacheHeight.drop(),e}this.pending.committed&&(this.state=this.pending),this.current=null,this.pending=null,this.coinCache.commit(),this.cacheHash.commit(),this.cacheHeight.commit(),this.stateCache.commit()}hasCache(e){return("number"==typeof e?this.cacheHeight:(r("string"==typeof e),this.cacheHash)).has(e)}getCache(e){return("number"==typeof e?this.cacheHeight:(r("string"==typeof e),this.cacheHash)).get(e)}async getHeight(e){var t;return"number"==typeof e?e:(r("string"==typeof e),e===c.NULL_HASH?-1:(t=this.cacheHash.get(e))?t.height:(t=await this.db.get(d.h(e)))?t.readUInt32LE(0,!0):-1)}async getHash(e){var t;return"string"==typeof e?e:(r("number"==typeof e),e<0?null:(t=this.cacheHeight.get(e))?t.hash:(t=await this.db.get(d.H(e)))?t.toString("hex"):null)}async getEntryByHeight(e){var t;return r("number"==typeof e),e<0?null:this.cacheHeight.get(e)||((e=await this.db.get(d.H(e)))&&(e=e.toString("hex"),t=this.state,e=await this.getEntryByHash(e))?(this.state===t&&this.cacheHeight.set(e.height,e),e):null)}async getEntryByHash(e){return r("string"==typeof e),e===c.NULL_HASH?null:this.cacheHash.get(e)||((e=await this.db.get(d.e(e)))?(e=b.fromRaw(e),this.cacheHash.set(e.hash,e),e):null)}getEntry(e){return"number"==typeof e?this.getEntryByHeight(e):this.getEntryByHash(e)}async hasEntry(e){return-1!==await this.getHeight(e)}async getAncestor(e,t){if(t<0)return null;if(r(0<=t),r(t<=e.height),await this.isMainChain(e))return this.getEntryByHeight(t);for(;e.height!==t;){e=this.getPrevCache(e)||await this.getPrevious(e),r(e)}return e}getPrevious(e){return this.getEntryByHash(e.prevBlock)}getPrevCache(e){return this.cacheHash.get(e.prevBlock)||null}async getNext(e){return(e=await this.getNextHash(e.hash))?this.getEntryByHash(e):null}async getNextEntry(e){var t=await this.getEntryByHeight(e.height+1);return t&&t.prevBlock===e.hash?t:null}getTip(){return this.getEntryByHash(this.state.tip)}async getState(){var e=await this.db.get(d.R);return e?ue.fromRaw(e):null}async saveGenesis(){let e=[];e=(this.node.config?this.node.config[this.network.type+"Addresses"]:this.network.genesis.addresses).split(",");var t=this.createGenesisBlock({version:this.network.genesis.version,time:this.network.genesis.time,bits:this.network.genesis.bits,nonce:this.network.genesis.nonce,addresses:e}),i=(this.network.genesis.hash=t.hash("hex"),this.network.genesis.merkleRoot=t.merkleRoot,b.fromBlock(t)),r=(this.logger.info("Writing genesis block to ChainDB."),new u);for(let e=0;e<t.txs.length;e++)r.addTX(t.txs[e],t.height,t.time);await this.save(i,t,r)}async getFlags(){var e=await this.db.get(d.O);return e?he.fromRaw(e):null}async verifyFlags(e){var t=this.options,i=await this.getFlags();let r=!1,s=!1;if(!i)throw new Error("No flags found.");if(t.network!==i.network)throw new Error("Network mismatch for chain.");if(t.spv&&!i.spv)throw new Error("Cannot retroactively enable SPV.");if(!t.spv&&i.spv)throw new Error("Cannot retroactively disable SPV.");if(!i.witness){if(!t.forceFlags)throw new Error("Cannot retroactively enable witness.");r=!0}if(t.prune&&!i.prune){if(!t.forceFlags)throw new Error("Cannot retroactively prune.");s=!0}if(!t.prune&&i.prune)throw new Error("Cannot retroactively unprune.");if(t.indexTX&&!i.indexTX)throw new Error("Cannot retroactively enable TX indexing.");if(!t.indexTX&&i.indexTX)throw new Error("Cannot retroactively disable TX indexing.");if(t.indexAddress&&!i.indexAddress)throw new Error("Cannot retroactively enable address indexing.");if(!t.indexAddress&&i.indexAddress)throw new Error("Cannot retroactively disable address indexing.");r&&(await this.logger.info("Rewriting chain flags."),await this.saveFlags()),s&&(await this.logger.info("Retroactively pruning chain."),await this.prune(e.tip))}async getStateCache(){var e=new le(this.network);for(const s of await this.db.range({gte:d.v(0,c.ZERO_HASH),lte:d.v(255,c.MAX_HASH),values:!0})){var[t,i]=d.vv(s.key),r=s.value[0];e.insert(t,i,r)}return e}saveDeployments(){var e=this.db.batch();return this.writeDeployments(e),e.write()}async batchPut(e,t){var i=this.db.batch();i.put(e,t),await i.write()}async batchDel(e){var t=this.db.batch();t.del(e),await t.write()}writeDeployments(e){var t=new a(1+17*this.network.deploys.length);t.writeU8(this.network.deploys.length);for(const e of this.network.deploys)t.writeU8(e.bit),t.writeU32(e.startTime),t.writeU32(e.timeout),t.writeI32(e.threshold),t.writeI32(e.window);e.put(d.V,t.render())}async checkDeployments(){var e=await this.db.get(d.V),t=(r(e,"No deployment table found."),new n(e)),i=t.readU8(),s=[];for(let e=0;e<i;e++){var a=t.readU8(),o=t.readU32(),c=t.readU32(),h=t.readI32(),u=t.readI32(),l=this.network.byBit(a);l&&o===l.startTime&&c===l.timeout&&h===l.threshold&&u===l.window||s.push(a)}return s}async verifyDeployments(){let e;try{e=await this.checkDeployments()}catch(t){if("EncodingError"!==t.type)throw t;e=[];for(let t=0;t<32;t++)e.push(t)}if(0===e.length)return!0;var t=this.db.batch();for(const i of e)this.logger.warning("Versionbit deployment params modified."),this.logger.warning("Invalidating cache for bit %d.",i),await this.invalidateCache(i,t);return this.writeDeployments(t),await t.write(),!1}async invalidateCache(e,t){for(const i of await this.db.keys({gte:d.v(e,c.ZERO_HASH),lte:d.v(e,c.MAX_HASH)}))t.del(i)}async prune(){var e=this.options,t=this.network.block.keepBlocks,i=this.network.block.pruneAfterHeight;if((await this.getFlags()).prune)throw new Error("Chain is already pruned.");var s=await this.getHeight(this.state.tip);if(s<=i+t)return!1;i+=1;var n=s-t,a=this.db.batch();for(let e=i;e<=n;e++){var o=await this.getHash(e);if(!o)throw new Error(`Cannot find hash for ${e}.`);a.del(d.b(o)),a.del(d.u(o))}try{e.prune=!0;const t=he.fromOptions(e);r(t.prune),a.put(d.O,t.toRaw()),await a.write()}catch(t){throw e.prune=!1,t}return await this.db.compactRange(),!0}async getNextHash(e){return(e=await this.db.get(d.n(e)))?e.toString("hex"):null}async isMainHash(e){if(r("string"==typeof e),e===c.NULL_HASH)return!1;if(e===this.network.genesis.hash)return!0;if(e===this.state.tip)return!0;var t=this.cacheHash.get(e);return t&&(t=this.cacheHeight.get(t.height))?t.hash===e:!!await this.getNextHash(e)}async isMainChain(e){var t;return!!e.isGenesis()||e.hash===this.state.tip||((t=this.getCache(e.height))?e.hash===t.hash:!!await this.getNextHash(e.hash))}getEntries(){return this.db.values({gte:d.e(c.ZERO_HASH),lte:d.e(c.MAX_HASH),parse:e=>b.fromRaw(e)})}getTimestampHashes(e,t){return this.db.keys({gte:d.th(t,c.ZERO_HASH),lte:d.th(e,c.MAX_HASH),parse:d.thh})}getContracts(){return this.db.values({gte:d.tx(c.NULL_HASH),lte:d.tx(c.HIGH_HASH),parse:e=>s.parseJson(e.toString())})}getCps(){return this.db.values({gte:d.CP(c.ZERO_CID,"hex"),lte:d.CP(c.MAX_CID,"hex"),parse:e=>U.fromRaw(e)})}async setCp(e){var t=this.db.batch();t.put(d.CP(e.cid),e.toRaw()),await t.write()}async delCp(e){var t=this.db.batch();t.del(d.CP(e)),await t.write()}getScs(){return this.db.values({gte:d.SC(c.ZERO_CID,"hex"),lte:d.SC(c.MAX_CID,"hex"),parse:e=>z.fromRaw(e)})}async setSc(e){var t=this.db.batch();t.put(d.SC(e.id),e.toRaw()),await t.write()}async delSc(e){var t=this.db.batch();t.del(d.SC(e)),await t.write()}async getSCList(e){return r(Array.isArray(e)),this.scList.query(e)}async delVp(e){var t=this.db.batch();t.del(d.VP(e.pid)),await t.write()}async setVp(e){var t=this.db.batch();t.put(d.VP(e.pid),e.toRaw()),await t.write()}async getVp(e){return(e=await this.db.get(d.VP(e)))?V.fromRaw(e):null}loadVpList(){return this.db.values({gte:d.VP(c.ZERO_CID,"hex"),lte:d.VP(c.MAX_CID,"hex"),parse:e=>V.fromRaw(e)})}async delHtlc(e){var t;e.id&&(this.htlcList.delete(e.id),this.htlcList.delAccount(e),(t=this.db.batch()).del(d.VH(e.shash,e.sidx)),await t.write())}async setSuggest(e){e=new W(e);var t=(this.htlcList.set(e.id,e),this.db.batch());t.put(d.VH(e.shash,e.sidx),e.toRaw()),await t.write()}async setAssent(e){e=new W(e);var t=(this.htlcList.set(e.id,e),this.db.batch());t.put(d.VH(e.shash,e.sidx),e.toRaw()),await t.write()}async loadHtlcList(){return this.db.values({gte:d.VH(c.ZERO_CID,0),lte:d.VH(c.MAX_CID,4294967295),parse:e=>W.fromRaw(e)})}async delErAbolish(e){var t=this.db.batch();t.del(d.ERA(e.erid)),await t.write()}async setErAbolish(e){var t=this.db.batch();t.put(d.ERA(e.erid),e.toRaw()),await t.write()}async getErAbolish(e){return(e=await this.db.get(d.ERA(e)))?J.fromRaw(e):null}loadErAbolishList(){return this.db.values({gte:d.ERA(c.ZERO_CID),lte:d.ERA(c.MAX_CID),parse:e=>J.fromRaw(e)})}async delCpEn(e){var t=this.db.batch();t.del(d.EN2(e.to,e.from)),await t.write()}async setCpEn(e){var t=this.db.batch();t.put(d.EN2(e.to,e.from),e.toRaw()),await t.write()}loadCpEnList(){return this.db.values({gte:d.EN2(c.ZERO_CID,c.ZERO_CID,"ascii"),lte:d.EN2(c.MAX_CID,c.MAX_CID,"ascii"),parse:e=>Q.fromRaw(e)})}queryCpEnListByCid(e){return this.db.values({gte:d.EN2(e,c.ZERO_CID,"ascii"),lte:d.EN2(e,c.MAX_CID,"ascii"),parse:e=>Q.fromRaw(e)})}async delEr(e){var t=this.db.batch();t.del(d.ER(e.erid)),t.del(d.ERR(e.witness,e.erid,e.txid)),await t.write()}async setEr(e,t){var i=this.db.batch(),r=d.ERR(e.witness,e.erid,e.txid);i.put(r,e.toRaw()),i.put(d.ER(e.erid),r),await i.write()}async getEr(e){var t;return(e=await this.db.get(d.ER(e)))&&([,,t]=d.ERRR(e),e=await this.db.get(e))?((e=G.fromRaw(e)).txid=t,e):null}async byPubkey(e,t,i=!1){if(!(e=await this.db.values({gte:d.ERR(e,c.NULL_HASH,c.NULL_HASH),lte:d.ERR(e,c.HIGH_HASH,c.HIGH_HASH),parse:e=>G.fromRaw(e)}))||0==e.length)return[];let r=null;if(i){var s,n=new Map;for(s of e){var a,o=await this.caVerifyObj(s);o&&((a=n.get(o.source.cluster))?o.startHeight>a.startHeight&&n.set(s.source.cluster,o):n.set(o.source.cluster,o))}r=n.values()}else r=e;if(!r||0==r.length)return[];var h,u=new y;for(h of r)u.set(h.erid,h);return u.query(t)}async caVerify(e){return this.caVerifyObj(await this.erList.getEr(e))}async caVerifyObj(e){var t;return e?(e.verify=!1,e.validHeight<=this.chain.height||(t=await this.erAbList.getErAb(e.erid))&&t.abolishHeight<this.chain.height||(t=ae.fromPublic(Buffer.from(e.witness,"hex"),this.network.type),e.verifying(t)&&(e.verify=!0,0<(t=this.cpList.query([["pubKey",e.witness]])).list.length)&&(e.cpid=t.list[0].cid)),e):null}async saveVote(e,t){var i=this.db.batch();i.put(d.VM(e),Buffer.from(JSON.stringify(t))),await i.write()}async saveOracle(e,t){var i=this.db.batch();i.put(d.VO(e),Buffer.from(JSON.stringify(t))),await i.write()}getTips(){return this.db.keys({gte:d.p(c.ZERO_HASH),lte:d.p(c.MAX_HASH),parse:d.pp})}async readCoin(e){var t,i,r,s;return this.options.spv?null:(({hash:t,index:i}=e),e=e.toKey(),r=this.state,(s=this.coinCache.get(e))?k.fromRaw(s):(s=await this.db.get(d.c(t,i)))?(r===this.state&&this.coinCache.set(e,s),k.fromRaw(s)):null)}async getCoin(e,t){return e=new g(e,t),(t=await this.readCoin(e))?t.toCoin(e):null}async hasCoins(e){for(let i=0;i<e.outputs.length;i++){var t=d.c(e.hash(),i);if(await this.db.has(t))return!0}return!1}async getCoinView(e){var t,i=new u;for({prevout:t}of e.inputs){var r=await this.readCoin(t);r&&i.addEntry(t,r)}return i}async getSpentView(e){var t,i=await this.getCoinView(e);for({prevout:t}of e.inputs)if(!i.hasEntry(t)){var r,{hash:r,index:s}=t;if(r=await this.getMeta(r)){const{tx:e,height:t,time:n}=r;s<e.outputs.length&&i.addIndex(e,s,t,n)}}return i}async getUndoCoins(e){return(e=await this.db.get(d.u(e)))?l.fromRaw(e):new l}async getBlock(e){return(e=await this.getRawBlock(e))?m.fromRaw(e):null}async getRawBlock(e){return!this.options.spv&&(e=await this.getHash(e))?this.db.get(d.b(e)):null}async getBlockView(e){var t=new u,i=await this.getUndoCoins(e.hash());if(!i.isEmpty()){for(let r=e.txs.length-1;0<r;r--){var s=e.txs[r];for(let e=s.inputs.length-1;0<=e;e--){var n=s.inputs[e];i.apply(t,n.prevout)}}r(i.isEmpty(),"Undo coins data inconsistency.")}return t}async getMeta(e){return this.options.indexTX&&(e=await this.db.get(d.t(e)))?w.fromRaw(e):null}async getTX(e){return(e=await this.getMeta(e))?e.tx:null}async getExtendTX(e){return(e=await this.getMeta(e))||null}async hasTX(e){return!!this.options.indexTX&&this.db.has(d.t(e))}async getCoinsByAddress(e){if(!this.options.indexAddress)return[];var t=[];for(const o of e=Array.isArray(e)?e:[e]){var i,s,n=v.getHash(o);for([i,s]of await this.db.keys({gte:d.C(n,c.ZERO_HASH,0),lte:d.C(n,c.MAX_HASH,4294967295),parse:d.Cc})){var a=await this.getCoin(i,s);r(a),t.push(a)}}return t}async getHashesByAddress(e,t="both"){if(Array.isArray(e)||(e=[e]),!this.options.indexTX||!this.options.indexAddress)return[];const i=Object.create(null);for(const s of e){var r=v.getHash(s);let e,n;n="input"==t?(e=d.TI(r,c.ZERO_HASH),d.TI(r,c.MAX_HASH)):"output"==t?(e=d.TO(r,c.ZERO_HASH),d.TO(r,c.MAX_HASH)):(e=d.T(r,c.ZERO_HASH),d.T(r,c.MAX_HASH)),await this.db.keys({gte:e,lte:n,parse:e=>{e=d.Tt(e),i[e]=!0}})}return Object.keys(i)}async getTXByAddress(e){var t=[];for(const i of await this.getMetaByAddress(e))t.push(i.tx);return t}async getVpByOid(e){return{list:[],count:0}}async getVpByAddress(e,t="both"){t=await this.getMetaByAddress(e,t);var i=new Map;for(const e of t)for(let t=0;t<e.tx.outputs.length;t++){var r=e.tx.outputs[t],s=r.getReturnData([ie.OP_PROPCREATE,ie.OP_PROPEXCHANGE]);s&&(s.current={height:e.height,hash:e.tx.rhash("hex"),index:t,address:r.getAddress().toString()},!(r=i.get(s.pid))||r.current.height<s.current.height)&&i.set(s.pid,s)}let n=[];return i.forEach(t=>{t.current.address==e&&n.push(t)}),{list:n,count:n.length}}async getMetaByAddress(e,t="both"){if(!this.options.indexTX||!this.options.indexAddress)return[];Array.isArray(e)||(e=[e]);var i=[];for(const n of await this.getHashesByAddress(e,t)){var s=await this.getMeta(n);r(s),i.push(s)}return i}async scan(e,t,i){"number"==typeof(e=null==e?this.network.genesis.hash:e)?this.logger.info("Scanning from height %d.",e):this.logger.info("Scanning from block %s.",s.revHex(e));let r=await this.getEntry(e);if(r){if(!await this.isMainChain(r))throw new Error("Cannot rescan an alternate chain.");let e=0;for(;r;){var n=await this.getBlock(r.hash),a=[];if(e++,n){this.logger.info("Scanning block %s (%d).",r.rhash(),r.height);for(let e=0;e<n.txs.length;e++){var o=n.txs[e];let i=!1;for(let e=0;e<o.outputs.length;e++){var c=o.outputs[e].getHash();c&&(0==r.height||t&&t.test(c))&&(c=g.fromTX(o,e),t&&t.add(c.toRaw()),i=!0)}if(!t||i)a.push(o);else if(0!==e)for(var{prevout:h}of o.inputs)if(!t||t.test(h.toRaw())){a.push(o);break}}}else if(!this.options.spv&&!this.options.prune)throw new Error("Block not found.");await i(r,a),r=await this.getNext(r)}this.logger.info("Finished scanning %d blocks.",e)}}async save(e,t,i){this.start();try{await this._save(e,t,i)}catch(e){throw this.drop(),e}await this.commit()}async _save(e,t,i){var r=t.hash();this.put(d.h(r),S(e.height)),this.put(d.th(t.time,r),null),this.put(d.e(r),e.toRaw()),this.cacheHash.push(e.hash,e),this.del(d.p(e.prevBlock)),this.put(d.p(r),null),this.saveUpdates(),i?(e.isGenesis()||this.put(d.n(e.prevBlock),r),this.put(d.H(e.height),r),this.cacheHeight.push(e.height,e),await this.saveBlock(e,t,i),this.put(d.R,this.pending.commit(r))):await this.saveBlock(e,t)}async reconnect(e,t,i){this.start();try{await this._reconnect(e,t,i)}catch(e){throw this.drop(),e}await this.commit()}async _reconnect(e,t,i){var s=t.hash();r(!e.isGenesis()),this.put(d.n(e.prevBlock),s),this.put(d.H(e.height),s),this.cacheHeight.push(e.height,e),this.cacheHash.push(e.hash,e),this.saveUpdates(),await this.connectBlock(e,t,i),this.put(d.R,this.pending.commit(s))}async disconnect(e,t){let i;this.start();try{i=await this._disconnect(e,t)}catch(e){throw this.drop(),e}return await this.commit(),i}async _disconnect(e,t){return this.del(d.n(e.prevBlock)),this.del(d.H(e.height)),this.cacheHeight.unpush(e.height),this.saveUpdates(),t=await this.disconnectBlock(e,t),this.put(d.R,this.pending.commit(e.prevBlock)),t}saveUpdates(){var e=this.stateCache.updates;if(0!==e.length){this.logger.info("Saving %d state cache updates.",e.length);for(const r of e){var{bit:t,hash:i}=r;this.put(d.v(t,i),r.toRaw())}}}async reset(e){var t=await this.getEntry(e);if(!t)throw new Error("Block not found.");if(!await this.isMainChain(t))throw new Error("Cannot reset on alternate chain.");if(this.options.prune)throw new Error("Cannot reset when pruned.");await this.removeChains();let i=await this.getTip();for(r(i),this.logger.debug("Resetting main chain to: %s",t.rhash());;){if(this.start(),i.hash===t.hash){this.put(d.R,this.pending.commit(i.hash)),await this.commit();break}r(!i.isGenesis()),this.del(d.p(i.hash)),this.put(d.p(i.prevBlock),null),this.del(d.H(i.height)),this.del(d.h(i.hash)),this.del(d.th(i.time,i.hash)),this.del(d.e(i.hash)),this.del(d.n(i.prevBlock));try{await this.removeBlock(i)}catch(e){throw this.drop(),e}this.cacheHeight.remove(i.height),this.cacheHash.remove(i.hash),i=await this.getPrevious(i),r(i)}return i}async removeChains(){var e=await this.getTips();this.start();try{for(const t of e)await this._removeChain(t)}catch(e){throw this.drop(),e}await this.commit()}async _removeChain(e){let t=await this.getEntryByHash(e);if(!t)throw new Error("Alternate chain tip not found.");for(this.logger.debug("Removing alternate chain: %s.",t.rhash());!await this.isMainChain(t);)r(!t.isGenesis()),this.del(d.p(t.hash)),this.del(d.h(t.hash)),this.del(d.th(t.time,t.hash)),this.del(d.e(t.hash)),this.del(d.b(t.hash)),this.cacheHash.unpush(t.hash),t=await this.getPrevious(t),r(t)}async saveBlock(e,t,i){var r=t.hash();this.options.spv||(this.put(d.b(r),t.toRaw()),i&&await this.connectBlock(e,t,i))}async removeBlock(e){if(this.options.spv)return new u;var t=await this.getBlock(e.hash);if(!t)throw new Error("Block not found.");this.del(d.b(t.hash()));var i=await this.disconnectBlock(e,t);return this.put(d.R,this.pending.commit(e.prevBlock)),await this.commit(),await this.chain.fire("block.disconnect",e,t,i),i}saveView(e){for(var[t,i]of e.map)for(var[r,s]of i.outputs){var n;s.spent?(this.del(d.c(t,r)),this.node.transmit("utxo.add",{hash:s.hash,index:s.index,address:s.getAddress().toString(),value:s.value,height:s.height}),this.coinCache.unpush(t+r)):(this.del(d.s(t,r)),n=s.toRaw(),this.put(d.c(t,r),n),this.node.transmit("utxo.remove",{hash:s.hash,index:s.index,address:s.getAddress().toString(),value:s.value,height:s.height}),this.coinCache.push(t+r,n))}}async getSpender(e,t){return(e=await this.db.get(d.s(e,t)))?I.DecodeSpenderValue(e):null}async connectBlock(e,t,i){if(!this.options.spv){var r=t.hash("hex");this.pending.connect(t);for(let u=0;u<t.txs.length;u++){var s=t.txs[u],n=s.hash("hex");if(!e.isGenesis()&&0<u)for(let e=0;e<s.inputs.length;e++){var a=s.inputs[e].prevout,o=i.getOutput(a);this.pending.spend(o);try{await this.emitCoinSpent(s.inputs[e],o,s,t)}catch(e){this.logger.error("connect block:",e.message)}this.put(d.s(a.hash,a.index),I.EncodeSpenderValue(n,e,t.height,r))}for(const e of s.outputs)e.script.isUnspendable()||this.pending.add(e);try{var c=await C.FactoryOfContract({env:this,block:t,tx:s,coins:i});if(!e.isGenesis()){var h=await c.verify();if(1!=h)throw new oe(s,_(h),"bad-contract-verify",100)}await c.isBlockFinal()==E.Final&&await c.confirm()}catch(e){this.logger.error("TX(%s) Smart Contract Execute Error: %s",s.txid(),e.message)}this.indexTX(s,i,e,u)}this.saveView(i),i.undo.isEmpty()||this.put(d.u(r),i.undo.commit()),await this.voteMgr.saveVote(),await this.propList.save(),this.scList.deletes([["indate","<",this.chain.height]]),await this.pruneBlock(e)}}async disconnectBlock(e,t){var i=new u;if(!this.options.spv){var s=t.hash(),n=await this.getUndoCoins(s);this.pending.disconnect(t);for(let r=t.txs.length-1;0<=r;r--){var a=t.txs[r];if(0<r)for(let e=a.inputs.length-1;0<=e;e--){var o=a.inputs[e].prevout;n.apply(i,o),this.pending.add(i.getOutput(o))}i.removeTX(a,e.height,e.time);for(let e=a.outputs.length-1;0<=e;e--){var c=a.outputs[e];c.script.isUnspendable()||this.pending.spend(c)}try{await(await C.FactoryOfContract({env:this,block:t,tx:a})).unconfirm()}catch(s){this.logger.error("TX(%s) Smart Contract Unconfirm Error: %s",a.txid(),s.message)}await this.unindexTX(a,i)}r(n.isEmpty(),"Undo coins data inconsistency."),this.saveView(i),this.del(d.u(s)),await this.voteMgr.saveVote(),await this.propList.save()}return i}async pruneBlock(e){this.options.spv||!this.options.prune||(e=e.height-this.network.block.keepBlocks)<=this.network.block.pruneAfterHeight||(e=await this.getHash(e))&&(this.del(d.b(e)),this.del(d.u(e)))}saveFlags(){var e=he.fromOptions(this.options),t=this.db.batch();return t.put(d.O,e.toRaw()),t.write()}indexTX(e,t,i,r){var s=e.hash();if(this.options.indexTX&&(r=w.fromTX(e,i,r),this.put(d.t(s),r.toRaw()),this.options.indexAddress)){for(const t of e.getHashes())this.put(d.T(t,s),null);for(const t of e.getOutputHashes())this.put(d.TO(t,s),null);for(const i of e.getInputHashes(t))this.put(d.TI(i,s),null)}if(this.options.indexAddress){if(!i.isGenesis()&&!e.isCoinbase())for(var{prevout:n}of e.inputs){var a=t.getOutput(n).getHash();a&&this.del(d.C(a,n.hash,n.index))}for(let t=0;t<e.outputs.length;t++){var o=e.outputs[t].getHash();o&&this.put(d.C(o,s,t),null)}}}async unindexTX(e,t){var i,r=e.hash();if(this.options.indexTX&&(this.del(d.t(r)),this.options.indexAddress))for(i of e.getHashes(t))this.del(d.T(i,r)),this.del(d.TI(i,r)),this.del(d.TO(i,r));if(this.options.indexAddress){if(!e.isCoinbase())for(var{prevout:s}of e.inputs){var n=t.getOutput(s).getHash();n&&this.put(d.C(n,s.hash,s.index),null)}for(let t=0;t<e.outputs.length;t++){var a=e.outputs[t].getHash();a&&this.del(d.C(a,r,t))}}}async readHostList(){return this.db.get(d.HL)}async writeHostList(e){var t=this.db.batch();return t.put(d.HL,e),t.write()}async emitCoinSpent(e,t,i,r){(i={env:this,block:r,tx:i,hash:e.prevout.hash,i:e.prevout.index,height:r.height,output:t,type:t.getReturnType(),data:t.getReturnData(re)}).data&&await te(i).spent(e)}}function he(e){if(!(this instanceof he))return new he(e);this.network=h.primary,this.spv=!1,this.witness=!0,this.prune=!1,this.indexTX=!1,this.indexAddress=!1,e&&this.fromOptions(e)}ce.layout=d,he.prototype.fromOptions=function(e){return this.network=h.get(e.network),null!=e.spv&&(r("boolean"==typeof e.spv),this.spv=e.spv),null!=e.prune&&(r("boolean"==typeof e.prune),this.prune=e.prune),null!=e.indexTX&&(r("boolean"==typeof e.indexTX),this.indexTX=e.indexTX),null!=e.indexAddress&&(r("boolean"==typeof e.indexAddress),this.indexAddress=e.indexAddress),this},he.fromOptions=function(e){return(new he).fromOptions(e)},he.prototype.toRaw=function(){var e=new a(12);let t=0;return this.spv&&(t|=1),this.witness&&(t|=2),this.prune&&(t|=4),this.indexTX&&(t|=8),this.indexAddress&&(t|=16),e.writeU32(this.network.magic),e.writeU32(t),e.writeU32(0),e.render()},he.prototype.fromRaw=function(e){return e=new n(e),this.network=h.fromMagic(e.readU32()),e=e.readU32(),this.spv=0!=(1&e),this.witness=0!=(2&e),this.prune=0!=(4&e),this.indexTX=0!=(8&e),this.indexAddress=0!=(16&e),this},he.fromRaw=function(e){return(new he).fromRaw(e)};class ue{constructor(){this.tip=c.NULL_HASH,this.tx=0,this.coin=0,this.value=0,this.committed=!1}rhash(){return s.revHex(this.tip)}clone(){var e=new ue;return e.tip=this.tip,e.tx=this.tx,e.coin=this.coin,e.value=this.value,e}connect(e){this.tx+=e.txs.length}disconnect(e){this.tx-=e.txs.length}add(e){this.coin++,this.value+=e.value}spend(e){this.coin--,this.value-=e.value}commit(e){return"string"!=typeof e&&(e=e.toString("hex")),this.tip=e,this.committed=!0,this.toRaw()}toRaw(){var e=new a(56);return e.writeHash(this.tip),e.writeU64(this.tx),e.writeU64(this.coin),e.writeU64(this.value),e.render()}static fromRaw(e){var t=new ue;e=new n(e);return t.tip=e.readHash("hex"),t.tx=e.readU64(),t.coin=e.readU64(),t.value=e.readU64(),t}}function le(e){this.network=e,this.bits=[],this.updates=[],this._init()}function pe(e,t,i){this.bit=e,this.hash=t,this.state=i}le.prototype._init=function(){for(let e=0;e<32;e++)this.bits.push(null);for(var{bit:e}of this.network.deploys)r(!this.bits[e]),this.bits[e]=new Map},le.prototype.set=function(e,t,i){var s=this.bits[e];r(s),s.get(t.hash)!==i&&(s.set(t.hash,i),this.updates.push(new pe(e,t.hash,i)))},le.prototype.get=function(e,t){return e=this.bits[e],r(e),null==(e=e.get(t.hash))?-1:e},le.prototype.commit=function(){this.updates.length=0},le.prototype.drop=function(){for(var{bit:e,hash:t}of this.updates)e=this.bits[e],r(e),e.delete(t);this.updates.length=0},le.prototype.insert=function(e,t,i){e=this.bits[e],r(e),e.set(t,i)},pe.prototype.toRaw=function(){return x(this.state)},e.exports=ce},function(e,t,i){"use strict";
|
|
880
|
+
*/const r=i(0),s=i(2),n=i(1),a=i(4),o=i(33),c=i(3),h=i(15),u=i(60),l=i(246),p=i(167),d=i(328),f=i(64),m=i(75),g=i(45),y=i(37),v=i(13),b=i(329),w=i(330),k=i(146),x=c.U8,S=c.U32,{getVerifyMsg:_,BlockFinalType:E,ContractEnvType:R,ContractType:A}=i(5);var O=i(72);const C=i(50),I=i(20),j=i(69),P=i(113),T=i(28),B=i(6),N=i(144),M=i(7),L=i(159),F=i(118),z=F.scItem,H=i(339),D=i(312),U=D.cpItem,q=i(132),V=q.vpItem,K=i(340),W=K.vpItem,$=i(729),X=i(153),G=X.ErItem,Y=i(154),J=Y.ErAbolishItem,Z=i(227),Q=Z.EnchancementItem;i(54);const ee=i(341),te=i(117),{opcodes:ie,nestingCodes:re}=i(19),se=i(123),ne=i(26),ae=i(39),oe=i(32).VerifyError;class ce extends O{constructor(e){super(),this.chain=e,this.eid="chaindb",this.fromOptions(e.options),this.db=p(this.options),this.stateCache=new le(this.network),this.state=new ue,this.pending=null,this.current=null,this.coinCache=new f(this.options.coinCache),this.cacheHash=new f(this.options.entryCache),this.cacheHeight=new f(this.options.entryCache),this.autoTaskMgr=new L(this),this.propList=new q(this),this.htlcList=new K(this),this.$voteMgr=new $(this),this.transactionList=new H(this),this.cpList=new D(this),this.scList=new F(this),this.erList=new X(this),this.erAbList=new Y(this),this.enCpList=new Z(this,{type:"cp"}),this.middleHashChain=new Set}get hmacConnection(){return this.node.specialCp.content()}get voteMgr(){return this.$voteMgr}get mode(){return R.Block}fromOptions(e){this.options=e,this.node=e.node,this.network=e.network,this.logger=e.logger.context("chaindb")}get curHeight(){return this.chain.height}get finder(){return this.node.getTXFromAll?this.node.getTXFromAll.bind(this.node):()=>{}}get tidCreator(){return d.tx}rpcExecute(){return this.node.rpc.execute.apply(this.node.rpc,arguments)}createGenesisCoinbase(e,t){var i,r=new I,s=new P,n=((i=((i=(s.script.pushInt(0),s.script.pushData(c.ZERO_HASH160),Buffer.allocUnsafe(4))).writeUInt32LE(0,0,!0),s.script.pushData(i),s.script.pushData(c.ZERO_U64),s.script.compile(),s.witness.push(c.ZERO_HASH),s.witness.compile(),r.inputs.push(s),new T)).script.createScript(c.ZERO_HASH160),i.value=M.ORIGINAL,r.outputs.push(i),c.ZERO_HASH),a=[],[a,,]=(a.push(c.ZERO_HASH),N.createRoot(a));a=B.root256(a,n);return(n=new T).script.fromCommitment(a),r.outputs.push(n),s.script.setData(1,t),s.script.compile(),i.script.fromAddress(e.addresses[0]),r}dummyInput(){return P.fromOutpoint(new g(c.NULL_HASH,0))}createGenesisBlock(e){if(!e.addresses||e.addresses.length<11)throw new Error("addresses length expect 11.");let t=e.flags;t=t||Buffer.from("In the name of Athena: Of the user, By the user, and For the user.","ascii");var i=this.createGenesisCoinbase(e,t),r=this.network.genesisId,n=new j,[n,,]=(n.addInput(this.dummyInput()),n.cpRegister(e.addresses[0],M.COIN,{cid:r,oper:A.cpRegister,name:"ATHENA",ip:"*",pubAddress:"",pubKey:"",signMethod:""}),n.commit()),a=new j;a.addInput(this.dummyInput());for(let t=0;t<10;t++)for(let i=0;i<10;i++){var o={oper:A.propCreate,pid:""+this.network.tokenFix+s.sprintf("%04d",10*t+i+1),oid:this.network.tokenFix+"0000",cid:r,gold:M.COIN};a.propCreate(e.addresses[t+1],o)}var[h,,]=a.commit(),u=new m({version:e.version,prevBlock:c.NULL_HASH,merkleRoot:i.hash("hex"),time:e.time,bits:e.bits,nonce:e.nonce,height:0});return u.txs.push(i),u.txs.push(n),u.txs.push(h),u.refresh(!0),u.merkleRoot=u.createMerkleRoot("hex"),u}async open(e){this.logger.info("Opening ChainDB..."),await this.db.open();var t=await this.db.checkVersion(d,3);if(i=await this.getState()){var i;if(await this.verifyFlags(i),await this.verifyDeployments(),this.stateCache=await this.getStateCache(),this.state=i,!(i=await this.getEntry(0)))throw new Error("genesis block not found.");this.network.genesis.hash=i.hash,this.network.genesis.merkleRoot=i.merkleRoot,this.logger.info("ChainDB successfully loaded.")}else e&&(i=await this.getGenesisParams(e),this.node.miner&&(this.node.miner.addresses=[new v(i.coinbaseAddress)]),this.node.config)&&this.node.config.changeParams(i),await this.saveFlags(),await this.saveDeployments(),await this.saveGenesis(),this.logger.info("ChainDB successfully initialized.");return this.logger.info("Chain State: hash=%s tx=%d coin=%d value=%s.",this.state.rhash(),this.state.tx,this.state.coin,o.btc(this.state.value)),t}async getGenesisParams(e){if(e.isGenesisParams)return e;e=e||{};var t=new ee({name:"wallet-test",db:"memory",resolution:!1,verify:!1}),i=(t=(e.getMnemonicFromMemory=!0,await t.open(e),await t.get("primary")),[]);for(const s of await t.getPaths("default")){var r=s.toAddress();0==s.branch&&i.push(""+r.toString(e.network))}var s,n,a,o,h={};return h[e.network+"Addresses"]=i.reduce((e,t)=>e+(""==e?t:","+t),""),h.coinbaseAddress=i[1],h.notifyAddress=t.notifyAddress,this.node.config.notifyAddress=h.notifyAddress,await se.writeFile(h,e.network+"-genesis.params"),e.isGenesisEncrypt||(s=B.hash256(Buffer.from(t.master.mnemonic.toSeed().toString("hex"))),n=ne.publicKeyCreate(s,!0),a=v.fromWitnessPubkeyhash(B.hash160(n),this.network.type).toString(),o=B.hash256(Buffer.concat([Buffer.from("root"),t.master.key.privateKey])),t={body:{language:t.master.mnemonic.language,phrase:t.master.mnemonic.getPhrase(),passphrase:t.master.mnemonic.passphrase,awardAddress:h.coinbaseAddress,notifyAddress:t.notifyAddress,alliancePrivateKey:o.toString("hex"),allianceName:"root",allianceNodeId:0,allianceNodeToken:{prv:s.toString("hex"),pub:n.toString("hex"),address:a}},password:e.password||c.MNEMONIC_SALT,network:e.network,file:e.network+"-genesis.keystore"},await se.writeEncryptFile(t)),h}close(){return this.db.close()}start(){return r(!this.current),r(!this.pending),this.current=this.db.batch(),this.pending=this.state.clone(),this.coinCache.start(),this.cacheHash.start(),this.cacheHeight.start(),this.current}put(e,t){r(this.current),this.current.put(e,t)}del(e){r(this.current),this.current.del(e)}batch(){return r(this.current),this.current}drop(){var e=this.current;r(this.current),r(this.pending),this.current=null,this.pending=null,this.coinCache.drop(),this.cacheHash.drop(),this.cacheHeight.drop(),this.stateCache.drop(),e.clear()}async commit(){r(this.current),r(this.pending);try{await this.current.write()}catch(e){throw this.current=null,this.pending=null,this.coinCache.drop(),this.cacheHash.drop(),this.cacheHeight.drop(),e}this.pending.committed&&(this.state=this.pending),this.current=null,this.pending=null,this.coinCache.commit(),this.cacheHash.commit(),this.cacheHeight.commit(),this.stateCache.commit()}hasCache(e){return("number"==typeof e?this.cacheHeight:(r("string"==typeof e),this.cacheHash)).has(e)}getCache(e){return("number"==typeof e?this.cacheHeight:(r("string"==typeof e),this.cacheHash)).get(e)}async getHeight(e){var t;return"number"==typeof e?e:(r("string"==typeof e),e===c.NULL_HASH?-1:(t=this.cacheHash.get(e))?t.height:(t=await this.db.get(d.h(e)))?t.readUInt32LE(0,!0):-1)}async getHash(e){var t;return"string"==typeof e?e:(r("number"==typeof e),e<0?null:(t=this.cacheHeight.get(e))?t.hash:(t=await this.db.get(d.H(e)))?t.toString("hex"):null)}async getEntryByHeight(e){var t;return r("number"==typeof e),e<0?null:this.cacheHeight.get(e)||((e=await this.db.get(d.H(e)))&&(e=e.toString("hex"),t=this.state,e=await this.getEntryByHash(e))?(this.state===t&&this.cacheHeight.set(e.height,e),e):null)}async getEntryByHash(e){return r("string"==typeof e),e===c.NULL_HASH?null:this.cacheHash.get(e)||((e=await this.db.get(d.e(e)))?(e=b.fromRaw(e),this.cacheHash.set(e.hash,e),e):null)}getEntry(e){return"number"==typeof e?this.getEntryByHeight(e):this.getEntryByHash(e)}async hasEntry(e){return-1!==await this.getHeight(e)}async getAncestor(e,t){if(t<0)return null;if(r(0<=t),r(t<=e.height),await this.isMainChain(e))return this.getEntryByHeight(t);for(;e.height!==t;){e=this.getPrevCache(e)||await this.getPrevious(e),r(e)}return e}getPrevious(e){return this.getEntryByHash(e.prevBlock)}getPrevCache(e){return this.cacheHash.get(e.prevBlock)||null}async getNext(e){return(e=await this.getNextHash(e.hash))?this.getEntryByHash(e):null}async getNextEntry(e){var t=await this.getEntryByHeight(e.height+1);return t&&t.prevBlock===e.hash?t:null}getTip(){return this.getEntryByHash(this.state.tip)}async getState(){var e=await this.db.get(d.R);return e?ue.fromRaw(e):null}async saveGenesis(){let e=[];e=(this.node.config?this.node.config[this.network.type+"Addresses"]:this.network.genesis.addresses).split(",");var t=this.createGenesisBlock({version:this.network.genesis.version,time:this.network.genesis.time,bits:this.network.genesis.bits,nonce:this.network.genesis.nonce,addresses:e}),i=(this.network.genesis.hash=t.hash("hex"),this.network.genesis.merkleRoot=t.merkleRoot,b.fromBlock(t)),r=(this.logger.info("Writing genesis block to ChainDB."),new u);for(let e=0;e<t.txs.length;e++)r.addTX(t.txs[e],t.height,t.time);await this.save(i,t,r)}async getFlags(){var e=await this.db.get(d.O);return e?he.fromRaw(e):null}async verifyFlags(e){var t=this.options,i=await this.getFlags();let r=!1,s=!1;if(!i)throw new Error("No flags found.");if(t.network!==i.network)throw new Error("Network mismatch for chain.");if(t.spv&&!i.spv)throw new Error("Cannot retroactively enable SPV.");if(!t.spv&&i.spv)throw new Error("Cannot retroactively disable SPV.");if(!i.witness){if(!t.forceFlags)throw new Error("Cannot retroactively enable witness.");r=!0}if(t.prune&&!i.prune){if(!t.forceFlags)throw new Error("Cannot retroactively prune.");s=!0}if(!t.prune&&i.prune)throw new Error("Cannot retroactively unprune.");if(t.indexTX&&!i.indexTX)throw new Error("Cannot retroactively enable TX indexing.");if(!t.indexTX&&i.indexTX)throw new Error("Cannot retroactively disable TX indexing.");if(t.indexAddress&&!i.indexAddress)throw new Error("Cannot retroactively enable address indexing.");if(!t.indexAddress&&i.indexAddress)throw new Error("Cannot retroactively disable address indexing.");r&&(await this.logger.info("Rewriting chain flags."),await this.saveFlags()),s&&(await this.logger.info("Retroactively pruning chain."),await this.prune(e.tip))}async getStateCache(){var e=new le(this.network);for(const s of await this.db.range({gte:d.v(0,c.ZERO_HASH),lte:d.v(255,c.MAX_HASH),values:!0})){var[t,i]=d.vv(s.key),r=s.value[0];e.insert(t,i,r)}return e}saveDeployments(){var e=this.db.batch();return this.writeDeployments(e),e.write()}async batchPut(e,t){var i=this.db.batch();i.put(e,t),await i.write()}async batchDel(e){var t=this.db.batch();t.del(e),await t.write()}writeDeployments(e){var t=new a(1+17*this.network.deploys.length);t.writeU8(this.network.deploys.length);for(const e of this.network.deploys)t.writeU8(e.bit),t.writeU32(e.startTime),t.writeU32(e.timeout),t.writeI32(e.threshold),t.writeI32(e.window);e.put(d.V,t.render())}async checkDeployments(){var e=await this.db.get(d.V),t=(r(e,"No deployment table found."),new n(e)),i=t.readU8(),s=[];for(let e=0;e<i;e++){var a=t.readU8(),o=t.readU32(),c=t.readU32(),h=t.readI32(),u=t.readI32(),l=this.network.byBit(a);l&&o===l.startTime&&c===l.timeout&&h===l.threshold&&u===l.window||s.push(a)}return s}async verifyDeployments(){let e;try{e=await this.checkDeployments()}catch(t){if("EncodingError"!==t.type)throw t;e=[];for(let t=0;t<32;t++)e.push(t)}if(0===e.length)return!0;var t=this.db.batch();for(const i of e)this.logger.warning("Versionbit deployment params modified."),this.logger.warning("Invalidating cache for bit %d.",i),await this.invalidateCache(i,t);return this.writeDeployments(t),await t.write(),!1}async invalidateCache(e,t){for(const i of await this.db.keys({gte:d.v(e,c.ZERO_HASH),lte:d.v(e,c.MAX_HASH)}))t.del(i)}async prune(){var e=this.options,t=this.network.block.keepBlocks,i=this.network.block.pruneAfterHeight;if((await this.getFlags()).prune)throw new Error("Chain is already pruned.");var s=await this.getHeight(this.state.tip);if(s<=i+t)return!1;i+=1;var n=s-t,a=this.db.batch();for(let e=i;e<=n;e++){var o=await this.getHash(e);if(!o)throw new Error(`Cannot find hash for ${e}.`);a.del(d.b(o)),a.del(d.u(o))}try{e.prune=!0;const t=he.fromOptions(e);r(t.prune),a.put(d.O,t.toRaw()),await a.write()}catch(t){throw e.prune=!1,t}return await this.db.compactRange(),!0}async getNextHash(e){return(e=await this.db.get(d.n(e)))?e.toString("hex"):null}async isMainHash(e){if(r("string"==typeof e),e===c.NULL_HASH)return!1;if(e===this.network.genesis.hash)return!0;if(e===this.state.tip)return!0;var t=this.cacheHash.get(e);return t&&(t=this.cacheHeight.get(t.height))?t.hash===e:!!await this.getNextHash(e)}async isMainChain(e){var t;return!!e.isGenesis()||e.hash===this.state.tip||((t=this.getCache(e.height))?e.hash===t.hash:!!await this.getNextHash(e.hash))}getEntries(){return this.db.values({gte:d.e(c.ZERO_HASH),lte:d.e(c.MAX_HASH),parse:e=>b.fromRaw(e)})}getTimestampHashes(e,t){return this.db.keys({gte:d.th(t,c.ZERO_HASH),lte:d.th(e,c.MAX_HASH),parse:d.thh})}getContracts(){return this.db.values({gte:d.tx(c.NULL_HASH),lte:d.tx(c.HIGH_HASH),parse:e=>s.parseJson(e.toString())})}getCps(){return this.db.values({gte:d.CP(c.ZERO_CID,"hex"),lte:d.CP(c.MAX_CID,"hex"),parse:e=>U.fromRaw(e)})}async setCp(e){var t=this.db.batch();t.put(d.CP(e.cid),e.toRaw()),await t.write()}async delCp(e){var t=this.db.batch();t.del(d.CP(e)),await t.write()}getScs(){return this.db.values({gte:d.SC(c.ZERO_CID,"hex"),lte:d.SC(c.MAX_CID,"hex"),parse:e=>z.fromRaw(e)})}async setSc(e){var t=this.db.batch();t.put(d.SC(e.id),e.toRaw()),await t.write()}async delSc(e){var t=this.db.batch();t.del(d.SC(e)),await t.write()}async getSCList(e){return r(Array.isArray(e)),this.scList.query(e)}async delVp(e){var t=this.db.batch();t.del(d.VP(e.pid)),await t.write()}async setVp(e){var t=this.db.batch();t.put(d.VP(e.pid),e.toRaw()),await t.write()}async getVp(e){return(e=await this.db.get(d.VP(e)))?V.fromRaw(e):null}loadVpList(){return this.db.values({gte:d.VP(c.ZERO_CID,"hex"),lte:d.VP(c.MAX_CID,"hex"),parse:e=>V.fromRaw(e)})}async delHtlc(e){var t;e.id&&(this.htlcList.delete(e.id),this.htlcList.delAccount(e),(t=this.db.batch()).del(d.VH(e.shash,e.sidx)),await t.write())}async setSuggest(e){e=new W(e);var t=(this.htlcList.set(e.id,e),this.db.batch());t.put(d.VH(e.shash,e.sidx),e.toRaw()),await t.write()}async setAssent(e){e=new W(e);var t=(this.htlcList.set(e.id,e),this.db.batch());t.put(d.VH(e.shash,e.sidx),e.toRaw()),await t.write()}async loadHtlcList(){return this.db.values({gte:d.VH(c.ZERO_CID,0),lte:d.VH(c.MAX_CID,4294967295),parse:e=>W.fromRaw(e)})}async delErAbolish(e){var t=this.db.batch();t.del(d.ERA(e.erid)),await t.write()}async setErAbolish(e){var t=this.db.batch();t.put(d.ERA(e.erid),e.toRaw()),await t.write()}async getErAbolish(e){return(e=await this.db.get(d.ERA(e)))?J.fromRaw(e):null}loadErAbolishList(){return this.db.values({gte:d.ERA(c.ZERO_CID),lte:d.ERA(c.MAX_CID),parse:e=>J.fromRaw(e)})}async delCpEn(e){var t=this.db.batch();t.del(d.EN2(e.to,e.from)),await t.write()}async setCpEn(e){var t=this.db.batch();t.put(d.EN2(e.to,e.from),e.toRaw()),await t.write()}loadCpEnList(){return this.db.values({gte:d.EN2(c.ZERO_CID,c.ZERO_CID,"ascii"),lte:d.EN2(c.MAX_CID,c.MAX_CID,"ascii"),parse:e=>Q.fromRaw(e)})}queryCpEnListByCid(e){return this.db.values({gte:d.EN2(e,c.ZERO_CID,"ascii"),lte:d.EN2(e,c.MAX_CID,"ascii"),parse:e=>Q.fromRaw(e)})}async delEr(e){var t=this.db.batch();t.del(d.ER(e.erid)),t.del(d.ERR(e.witness,e.erid,e.txid)),await t.write()}async setEr(e,t){var i=this.db.batch(),r=d.ERR(e.witness,e.erid,e.txid);i.put(r,e.toRaw()),i.put(d.ER(e.erid),r),await i.write()}async getEr(e){var t;return(e=await this.db.get(d.ER(e)))&&([,,t]=d.ERRR(e),e=await this.db.get(e))?((e=G.fromRaw(e)).txid=t,e):null}async byPubkey(e,t,i=!1){if(!(e=await this.db.values({gte:d.ERR(e,c.NULL_HASH,c.NULL_HASH),lte:d.ERR(e,c.HIGH_HASH,c.HIGH_HASH),parse:e=>G.fromRaw(e)}))||0==e.length)return[];let r=null;if(i){var s,n=new Map;for(s of e){var a,o=await this.caVerifyObj(s);o&&((a=n.get(o.source.cluster))?o.startHeight>a.startHeight&&n.set(s.source.cluster,o):n.set(o.source.cluster,o))}r=n.values()}else r=e;if(!r||0==r.length)return[];var h,u=new y;for(h of r)u.set(h.erid,h);return u.query(t)}async caVerify(e){return this.caVerifyObj(await this.erList.getEr(e))}async caVerifyObj(e){var t;return e?(e.verify=!1,e.validHeight<=this.chain.height||(t=await this.erAbList.getErAb(e.erid))&&t.abolishHeight<this.chain.height||(t=ae.fromPublic(Buffer.from(e.witness,"hex"),this.network.type),e.verifying(t)&&(e.verify=!0,0<(t=this.cpList.query([["pubKey",e.witness]])).list.length)&&(e.cpid=t.list[0].cid)),e):null}async saveVote(e,t){var i=this.db.batch();i.put(d.VM(e),Buffer.from(JSON.stringify(t))),await i.write()}async saveOracle(e,t){var i=this.db.batch();i.put(d.VO(e),Buffer.from(JSON.stringify(t))),await i.write()}getTips(){return this.db.keys({gte:d.p(c.ZERO_HASH),lte:d.p(c.MAX_HASH),parse:d.pp})}async readCoin(e){var t,i,r,s;return this.options.spv?null:(({hash:t,index:i}=e),e=e.toKey(),r=this.state,(s=this.coinCache.get(e))?k.fromRaw(s):(s=await this.db.get(d.c(t,i)))?(r===this.state&&this.coinCache.set(e,s),k.fromRaw(s)):null)}async getCoin(e,t){return e=new g(e,t),(t=await this.readCoin(e))?t.toCoin(e):null}async hasCoins(e){for(let i=0;i<e.outputs.length;i++){var t=d.c(e.hash(),i);if(await this.db.has(t))return!0}return!1}async getCoinView(e){var t,i=new u;for({prevout:t}of e.inputs){var r=await this.readCoin(t);r&&i.addEntry(t,r)}return i}async getSpentView(e){var t,i=await this.getCoinView(e);for({prevout:t}of e.inputs)if(!i.hasEntry(t)){var r,{hash:r,index:s}=t;if(r=await this.getMeta(r)){const{tx:e,height:t,time:n}=r;s<e.outputs.length&&i.addIndex(e,s,t,n)}}return i}async getUndoCoins(e){return(e=await this.db.get(d.u(e)))?l.fromRaw(e):new l}async getBlock(e){return(e=await this.getRawBlock(e))?m.fromRaw(e):null}async getRawBlock(e){return!this.options.spv&&(e=await this.getHash(e))?this.db.get(d.b(e)):null}async getBlockView(e){var t=new u,i=await this.getUndoCoins(e.hash());if(!i.isEmpty()){for(let r=e.txs.length-1;0<r;r--){var s=e.txs[r];for(let e=s.inputs.length-1;0<=e;e--){var n=s.inputs[e];i.apply(t,n.prevout)}}r(i.isEmpty(),"Undo coins data inconsistency.")}return t}async getMeta(e){return this.options.indexTX&&(e=await this.db.get(d.t(e)))?w.fromRaw(e):null}async getTX(e){return(e=await this.getMeta(e))?e.tx:null}async getExtendTX(e){return(e=await this.getMeta(e))||null}async hasTX(e){return!!this.options.indexTX&&this.db.has(d.t(e))}async getCoinsByAddress(e){if(!this.options.indexAddress)return[];var t=[];for(const o of e=Array.isArray(e)?e:[e]){var i,s,n=v.getHash(o);for([i,s]of await this.db.keys({gte:d.C(n,c.ZERO_HASH,0),lte:d.C(n,c.MAX_HASH,4294967295),parse:d.Cc})){var a=await this.getCoin(i,s);r(a),t.push(a)}}return t}async getHashesByAddress(e,t="both"){if(Array.isArray(e)||(e=[e]),!this.options.indexTX||!this.options.indexAddress)return[];const i=Object.create(null);for(const s of e){var r=v.getHash(s);let e,n;n="input"==t?(e=d.TI(r,c.ZERO_HASH),d.TI(r,c.MAX_HASH)):"output"==t?(e=d.TO(r,c.ZERO_HASH),d.TO(r,c.MAX_HASH)):(e=d.T(r,c.ZERO_HASH),d.T(r,c.MAX_HASH)),await this.db.keys({gte:e,lte:n,parse:e=>{e=d.Tt(e),i[e]=!0}})}return Object.keys(i)}async getTXByAddress(e){var t=[];for(const i of await this.getMetaByAddress(e))t.push(i.tx);return t}async getVpByOid(e){return{list:[],count:0}}async getVpByAddress(e,t="both"){t=await this.getMetaByAddress(e,t);var i=new Map;for(const e of t)for(let t=0;t<e.tx.outputs.length;t++){var r=e.tx.outputs[t],s=r.getReturnData([ie.OP_PROPCREATE,ie.OP_PROPEXCHANGE]);s&&(s.current={height:e.height,hash:e.tx.rhash("hex"),index:t,address:r.getAddress().toString()},!(r=i.get(s.pid))||r.current.height<s.current.height)&&i.set(s.pid,s)}let n=[];return i.forEach(t=>{t.current.address==e&&n.push(t)}),{list:n,count:n.length}}async getMetaByAddress(e,t="both"){if(!this.options.indexTX||!this.options.indexAddress)return[];Array.isArray(e)||(e=[e]);var i=[];for(const n of await this.getHashesByAddress(e,t)){var s=await this.getMeta(n);r(s),i.push(s)}return i}async scan(e,t,i){"number"==typeof(e=null==e?this.network.genesis.hash:e)?this.logger.info("Scanning from height %d.",e):this.logger.info("Scanning from block %s.",s.revHex(e));let r=await this.getEntry(e);if(r){if(!await this.isMainChain(r))throw new Error("Cannot rescan an alternate chain.");let e=0;for(;r;){var n=await this.getBlock(r.hash),a=[];if(e++,n){this.logger.info("Scanning block %s (%d).",r.rhash(),r.height);for(let e=0;e<n.txs.length;e++){var o=n.txs[e];let i=!1;for(let e=0;e<o.outputs.length;e++){var c=o.outputs[e].getHash();c&&(0==r.height||t&&t.test(c))&&(c=g.fromTX(o,e),t&&t.add(c.toRaw()),i=!0)}if(!t||i)a.push(o);else if(0!==e)for(var{prevout:h}of o.inputs)if(!t||t.test(h.toRaw())){a.push(o);break}}}else if(!this.options.spv&&!this.options.prune)throw new Error("Block not found.");await i(r,a),r=await this.getNext(r)}this.logger.info("Finished scanning %d blocks.",e)}}async save(e,t,i){this.start();try{await this._save(e,t,i)}catch(e){throw this.drop(),e}await this.commit()}async _save(e,t,i){var r=t.hash();this.put(d.h(r),S(e.height)),this.put(d.th(t.time,r),null),this.put(d.e(r),e.toRaw()),this.cacheHash.push(e.hash,e),this.del(d.p(e.prevBlock)),this.put(d.p(r),null),this.saveUpdates(),i?(e.isGenesis()||this.put(d.n(e.prevBlock),r),this.put(d.H(e.height),r),this.cacheHeight.push(e.height,e),await this.saveBlock(e,t,i),this.put(d.R,this.pending.commit(r))):await this.saveBlock(e,t)}async reconnect(e,t,i){this.start();try{await this._reconnect(e,t,i)}catch(e){throw this.drop(),e}await this.commit()}async _reconnect(e,t,i){var s=t.hash();r(!e.isGenesis()),this.put(d.n(e.prevBlock),s),this.put(d.H(e.height),s),this.cacheHeight.push(e.height,e),this.cacheHash.push(e.hash,e),this.saveUpdates(),await this.connectBlock(e,t,i),this.put(d.R,this.pending.commit(s))}async disconnect(e,t){let i;this.start();try{i=await this._disconnect(e,t)}catch(e){throw this.drop(),e}return await this.commit(),i}async _disconnect(e,t){return this.del(d.n(e.prevBlock)),this.del(d.H(e.height)),this.cacheHeight.unpush(e.height),this.saveUpdates(),t=await this.disconnectBlock(e,t),this.put(d.R,this.pending.commit(e.prevBlock)),t}saveUpdates(){var e=this.stateCache.updates;if(0!==e.length){this.logger.info("Saving %d state cache updates.",e.length);for(const r of e){var{bit:t,hash:i}=r;this.put(d.v(t,i),r.toRaw())}}}async reset(e){var t=await this.getEntry(e);if(!t)throw new Error("Block not found.");if(!await this.isMainChain(t))throw new Error("Cannot reset on alternate chain.");if(this.options.prune)throw new Error("Cannot reset when pruned.");await this.removeChains();let i=await this.getTip();for(r(i),this.logger.debug("Resetting main chain to: %s",t.rhash());;){if(this.start(),i.hash===t.hash){this.put(d.R,this.pending.commit(i.hash)),await this.commit();break}r(!i.isGenesis()),this.del(d.p(i.hash)),this.put(d.p(i.prevBlock),null),this.del(d.H(i.height)),this.del(d.h(i.hash)),this.del(d.th(i.time,i.hash)),this.del(d.e(i.hash)),this.del(d.n(i.prevBlock));try{await this.removeBlock(i)}catch(e){throw this.drop(),e}this.cacheHeight.remove(i.height),this.cacheHash.remove(i.hash),i=await this.getPrevious(i),r(i)}return i}async removeChains(){var e=await this.getTips();this.start();try{for(const t of e)await this._removeChain(t)}catch(e){throw this.drop(),e}await this.commit()}async _removeChain(e){let t=await this.getEntryByHash(e);if(!t)throw new Error("Alternate chain tip not found.");for(this.logger.debug("Removing alternate chain: %s.",t.rhash());!await this.isMainChain(t);)r(!t.isGenesis()),this.del(d.p(t.hash)),this.del(d.h(t.hash)),this.del(d.th(t.time,t.hash)),this.del(d.e(t.hash)),this.del(d.b(t.hash)),this.cacheHash.unpush(t.hash),t=await this.getPrevious(t),r(t)}async saveBlock(e,t,i){var r=t.hash();this.options.spv||(this.put(d.b(r),t.toRaw()),i&&await this.connectBlock(e,t,i))}async removeBlock(e){if(this.options.spv)return new u;var t=await this.getBlock(e.hash);if(!t)throw new Error("Block not found.");this.del(d.b(t.hash()));var i=await this.disconnectBlock(e,t);return this.put(d.R,this.pending.commit(e.prevBlock)),await this.commit(),await this.chain.fire("block.disconnect",e,t,i),i}saveView(e){for(var[t,i]of e.map)for(var[r,s]of i.outputs){var n;s.spent?(this.del(d.c(t,r)),this.node.transmit("utxo.remove",{oper:"utxo.remove",hash:t,index:r,address:s.output.getAddress().toString(),color:s.output.isColored,value:s.output.value,height:s.height}),this.coinCache.unpush(t+r)):(this.del(d.s(t,r)),n=s.toRaw(),this.put(d.c(t,r),n),this.node.transmit("utxo.add",{oper:"utxo.add",hash:t,index:r,color:s.output.isColored,address:s.output.getAddress().toString(),value:s.output.value,height:s.height}),this.coinCache.push(t+r,n))}}async getSpender(e,t){return(e=await this.db.get(d.s(e,t)))?I.DecodeSpenderValue(e):null}async connectBlock(e,t,i){if(!this.options.spv){var r=t.hash("hex");this.pending.connect(t);for(let u=0;u<t.txs.length;u++){var s=t.txs[u],n=s.hash("hex");if(!e.isGenesis()&&0<u)for(let e=0;e<s.inputs.length;e++){var a=s.inputs[e].prevout,o=i.getOutput(a);this.pending.spend(o);try{await this.emitCoinSpent(s.inputs[e],o,s,t)}catch(e){this.logger.error("connect block:",e.message)}this.put(d.s(a.hash,a.index),I.EncodeSpenderValue(n,e,t.height,r))}for(const e of s.outputs)e.script.isUnspendable()||this.pending.add(e);try{var c=await C.FactoryOfContract({env:this,block:t,tx:s,coins:i});if(!e.isGenesis()){var h=await c.verify();if(1!=h)throw new oe(s,_(h),"bad-contract-verify",100)}await c.isBlockFinal()==E.Final&&await c.confirm()}catch(e){this.logger.error("TX(%s) Smart Contract Execute Error: %s",s.txid(),e.message)}this.indexTX(s,i,e,u)}this.saveView(i),i.undo.isEmpty()||this.put(d.u(r),i.undo.commit()),await this.voteMgr.saveVote(),await this.propList.save(),this.scList.deletes([["indate","<",this.chain.height]]),await this.pruneBlock(e)}}async disconnectBlock(e,t){var i=new u;if(!this.options.spv){var s=t.hash(),n=await this.getUndoCoins(s);this.pending.disconnect(t);for(let r=t.txs.length-1;0<=r;r--){var a=t.txs[r];if(0<r)for(let e=a.inputs.length-1;0<=e;e--){var o=a.inputs[e].prevout;n.apply(i,o),this.pending.add(i.getOutput(o))}i.removeTX(a,e.height,e.time);for(let e=a.outputs.length-1;0<=e;e--){var c=a.outputs[e];c.script.isUnspendable()||this.pending.spend(c)}try{await(await C.FactoryOfContract({env:this,block:t,tx:a})).unconfirm()}catch(s){this.logger.error("TX(%s) Smart Contract Unconfirm Error: %s",a.txid(),s.message)}await this.unindexTX(a,i)}r(n.isEmpty(),"Undo coins data inconsistency."),this.saveView(i),this.del(d.u(s)),await this.voteMgr.saveVote(),await this.propList.save()}return i}async pruneBlock(e){this.options.spv||!this.options.prune||(e=e.height-this.network.block.keepBlocks)<=this.network.block.pruneAfterHeight||(e=await this.getHash(e))&&(this.del(d.b(e)),this.del(d.u(e)))}saveFlags(){var e=he.fromOptions(this.options),t=this.db.batch();return t.put(d.O,e.toRaw()),t.write()}indexTX(e,t,i,r){var s=e.hash();if(this.options.indexTX&&(r=w.fromTX(e,i,r),this.put(d.t(s),r.toRaw()),this.options.indexAddress)){for(const t of e.getHashes())this.put(d.T(t,s),null);for(const t of e.getOutputHashes())this.put(d.TO(t,s),null);for(const i of e.getInputHashes(t))this.put(d.TI(i,s),null)}if(this.options.indexAddress){if(!i.isGenesis()&&!e.isCoinbase())for(var{prevout:n}of e.inputs){var a=t.getOutput(n).getHash();a&&this.del(d.C(a,n.hash,n.index))}for(let t=0;t<e.outputs.length;t++){var o=e.outputs[t].getHash();o&&this.put(d.C(o,s,t),null)}}}async unindexTX(e,t){var i,r=e.hash();if(this.options.indexTX&&(this.del(d.t(r)),this.options.indexAddress))for(i of e.getHashes(t))this.del(d.T(i,r)),this.del(d.TI(i,r)),this.del(d.TO(i,r));if(this.options.indexAddress){if(!e.isCoinbase())for(var{prevout:s}of e.inputs){var n=t.getOutput(s).getHash();n&&this.put(d.C(n,s.hash,s.index),null)}for(let t=0;t<e.outputs.length;t++){var a=e.outputs[t].getHash();a&&this.del(d.C(a,r,t))}}}async readHostList(){return this.db.get(d.HL)}async writeHostList(e){var t=this.db.batch();return t.put(d.HL,e),t.write()}async emitCoinSpent(e,t,i,r){(i={env:this,block:r,tx:i,hash:e.prevout.hash,i:e.prevout.index,height:r.height,output:t,type:t.getReturnType(),data:t.getReturnData(re)}).data&&await te(i).spent(e)}}function he(e){if(!(this instanceof he))return new he(e);this.network=h.primary,this.spv=!1,this.witness=!0,this.prune=!1,this.indexTX=!1,this.indexAddress=!1,e&&this.fromOptions(e)}ce.layout=d,he.prototype.fromOptions=function(e){return this.network=h.get(e.network),null!=e.spv&&(r("boolean"==typeof e.spv),this.spv=e.spv),null!=e.prune&&(r("boolean"==typeof e.prune),this.prune=e.prune),null!=e.indexTX&&(r("boolean"==typeof e.indexTX),this.indexTX=e.indexTX),null!=e.indexAddress&&(r("boolean"==typeof e.indexAddress),this.indexAddress=e.indexAddress),this},he.fromOptions=function(e){return(new he).fromOptions(e)},he.prototype.toRaw=function(){var e=new a(12);let t=0;return this.spv&&(t|=1),this.witness&&(t|=2),this.prune&&(t|=4),this.indexTX&&(t|=8),this.indexAddress&&(t|=16),e.writeU32(this.network.magic),e.writeU32(t),e.writeU32(0),e.render()},he.prototype.fromRaw=function(e){return e=new n(e),this.network=h.fromMagic(e.readU32()),e=e.readU32(),this.spv=0!=(1&e),this.witness=0!=(2&e),this.prune=0!=(4&e),this.indexTX=0!=(8&e),this.indexAddress=0!=(16&e),this},he.fromRaw=function(e){return(new he).fromRaw(e)};class ue{constructor(){this.tip=c.NULL_HASH,this.tx=0,this.coin=0,this.value=0,this.committed=!1}rhash(){return s.revHex(this.tip)}clone(){var e=new ue;return e.tip=this.tip,e.tx=this.tx,e.coin=this.coin,e.value=this.value,e}connect(e){this.tx+=e.txs.length}disconnect(e){this.tx-=e.txs.length}add(e){this.coin++,this.value+=e.value}spend(e){this.coin--,this.value-=e.value}commit(e){return"string"!=typeof e&&(e=e.toString("hex")),this.tip=e,this.committed=!0,this.toRaw()}toRaw(){var e=new a(56);return e.writeHash(this.tip),e.writeU64(this.tx),e.writeU64(this.coin),e.writeU64(this.value),e.render()}static fromRaw(e){var t=new ue;e=new n(e);return t.tip=e.readHash("hex"),t.tx=e.readU64(),t.coin=e.readU64(),t.value=e.readU64(),t}}function le(e){this.network=e,this.bits=[],this.updates=[],this._init()}function pe(e,t,i){this.bit=e,this.hash=t,this.state=i}le.prototype._init=function(){for(let e=0;e<32;e++)this.bits.push(null);for(var{bit:e}of this.network.deploys)r(!this.bits[e]),this.bits[e]=new Map},le.prototype.set=function(e,t,i){var s=this.bits[e];r(s),s.get(t.hash)!==i&&(s.set(t.hash,i),this.updates.push(new pe(e,t.hash,i)))},le.prototype.get=function(e,t){return e=this.bits[e],r(e),null==(e=e.get(t.hash))?-1:e},le.prototype.commit=function(){this.updates.length=0},le.prototype.drop=function(){for(var{bit:e,hash:t}of this.updates)e=this.bits[e],r(e),e.delete(t);this.updates.length=0},le.prototype.insert=function(e,t,i){e=this.bits[e],r(e),e.set(t,i)},pe.prototype.toRaw=function(){return x(this.state)},e.exports=ce},function(e,t,i){"use strict";
|
|
881
881
|
/*!
|
|
882
882
|
* lowlevelup.js - LevelUP module for vallnet
|
|
883
883
|
* Copyright (c) 2019-2022, Bookman Software (MIT License).
|
|
@@ -940,7 +940,7 @@ var r=i(776),s=i(778),n=i(779),a=i(780),o=i(781),c=i(782),h=i(783);i=i(784),s=[]
|
|
|
940
940
|
* rpc.js - bitcoind-compatible json rpc for vallnet.
|
|
941
941
|
* Copyright (c) 2019-2022, Bookman Software (MIT License).
|
|
942
942
|
* https://github.com/bookmansoft/gamegold
|
|
943
|
-
*/const r=i(59),s=i(0),n=i(78),a=n.errors,o=n.MAGIC_STRING,c=i(77),h=i(82),u=i(7),l=i(6),p=i(158),d=i(26),f=i(124),m=i(121),g=i(33),y=i(17),v=i(107),b=i(36),w=i(2),k=i(3),x=i(84),S=i(68),_=i(233).openapi,E=i(152);i(132).vpItem,i(220);const R=i(13),A=i(75),O=i(89),C=i(113),I=i(131),j=i(69),P=i(45),T=i(28),B=i(20),N=i(19).opcodes,M=i(328);function L(e){for(let i=0;i<e.length;i+=4){var t=e.readUInt32LE(i,!0);e.writeUInt32BE(t,i,!0)}}e.exports=class extends n{constructor(e){super(e),s(e,"RPC requires a Node."),this.node=e,this.keys=e.keys,this.workers=e.workers,this.chain=e.chain,this.mempool=e.mempool,this.pool=e.pool,this.fees=e.fees,this.miner=e.miner,this.logger=e.logger.context("rpc"),this.procLimit=0,this.attempt=null,this.lastActivity=0,this.boundChain=!1,this.nonce1=0,this.nonce2=0,this.merkleMap=new Map,this.pollers=[],this.generating=!1,this.init()}init(){this.add("help",this.help),this.add("miner",this.routerOfMiner),this.add("miner.generate.admin",this.generate),this.add("miner.generateto.admin",this.generateToAddress),this.add("miner.setsync.admin",this.setSync),this.add("miner.setaddr.admin",this.setMinerAddr),this.add("miner.addaddr.admin",this.addMinerAddr),this.add("miner.getaddr.admin",this.getMinerAddr),this.add("miner.set.admin",this.setGenerate),this.add("miner.check",this.getGenerate),this.add("miner.difficulty",this.getDifficulty),this.add("vote",this.routerOfVote),this.add("vote.show",this.voteShow),this.add("vote.check",this.voteCheck),this.add("oracle.check",this.oracleCheck),this.add("block",this.routerOfBlock),this.add("block.best",this.getBestBlockHash),this.add("block.count",this.getBlockCount),this.add("block.info",this.getBlock),this.add("block.info.byheight",this.getBlockByHeight),this.add("block.hash",this.getBlockHash),this.add("block.header",this.getBlockHeader),this.add("block.verify",this.verifyBlock),this.add("block.tips",this.getChainTips),this.add("block.template.admin",this.getBlockTemplate),this.add("block.submit.admin",this.submitBlock),this.add("block.reset.admin",this.resetBlock),this.add("block.rollback.admin",this.rollbackBlock),this.add("block.invalidate.admin",this.invalidateBlock),this.add("block.reconsider.admin",this.reconsiderBlock),this.add("cp.byClass",this.cpByClass),this.add("cp.byName",this.cpByName),this.add("cp.byId",this.cpById),this.add("remoteNotify.set",this.remoteNotifySet),this.add("sc.query",this.querySCList),this.add("sc.balance",this.scBalance),this.add("sc.model",this.querySCModel),this.add("comm",this.routerOfComm),this.add("tx",this.routerOfTx),this.add("tx.raw.get",this.getRawTransaction),this.add("tx.raw.decode",this.decodeRawTransaction),this.add("tx.raw.create",this.createRawTransaction),this.add("tx.raw.send",this.sendRawTransaction),this.add("tx.raw.sign.admin",this.signRawTransaction),this.add("tx.get",this.getTXByHash),this.add("tx.list.address",this.getTXByAddress),this.add("tx.list.addresses",this.getTXByAddresses),this.add("tx.broadcast",this.broadcast),this.add("tx.prioritise",this.prioritiseTransaction),this.add("tx.utxo",this.getTXOut),this.add("tx.txo.proof",this.getTXOutProof),this.add("tx.txo.verifyproof",this.verifyTXOutProof),this.add("tx.coins",this.getCoins),this.add("tx.coincache",this.getCoinCache),this.add("tx.coins.byoutpoint",this.getCoinByOutpoint),this.add("tx.spender",this.getSpender),this.add("prop.byaddress",this.getVpByAddress),this.add("prop.byid",this.getVpById),this.add("prop.byHashIndex",this.getVpByHashIndex),this.add("sys",this.routerOfSys),this.add("sys.info",this.getInfo),this.add("sys.dump",this.heapDump),this.add("sys.blockinfo",this.getBlockchainInfo),this.add("sys.peerinfo",this.getPeerInfo),this.add("sys.networkinfo",this.getNetworkInfo),this.add("sys.memoryinfo",this.getMemoryInfo),this.add("sys.mempoolinfo",this.getMempoolInfo),this.add("sys.mininginfo",this.getMiningInfo),this.add("sys.addednodeinfo",this.getAddedNodeInfo),this.add("sys.txoinfo",this.getTXOutSetInfo),this.add("sys.connectioncount",this.getConnectionCount),this.add("sys.disconnectnode",this.disconnectNode),this.add("sys.nettotals",this.getNetTotals),this.add("sys.prune",this.pruneBlockchain),this.add("sys.ping",this.ping),this.add("sys.setloglevel",this.setLogLevel),this.add("sys.setban",this.setBan),this.add("sys.listbanned",this.listBanned),this.add("sys.clearbanned",this.clearBanned),this.add("sys.addnode",this.addNode),this.add("sys.hashrate",this.getNetworkHashPS),this.add("sys.work",this.getWork),this.add("sys.worklp",this.getWorkLongpoll),this.add("sys.setmocktime",this.setMockTime),this.add("sys.message.verify",this.verifyMessage),this.add("sys.message.signk",this.signMessageWithPrivkey),this.add("sys.stop.admin",this.stop),this.add("sys.decodescript",this.decodeScript),this.add("sys.validateaddress",this.validateAddress),this.add("sys.createmultisig",this.createMultisig),this.add("sys.createwitnessaddress",this.createWitnessAddress),this.add("sys.createAuthToken",this.authtoken),this.add("sys.groupPrefix",this.addGroupPrefix),this.add("sys.devmode",this.setDevMode),this.add("sys.groupSuffix",this.addGroupSuffix),this.add("sys.changeSpecialCp",this.changeSpecialCp),this.add("sys.rescan",this.rescan),this.add("sys.alliance",this.routerOfAlliance),this.add("sys.alliance.info",this.getAllianceInfo),this.add("sys.alliance.addpeer",this.addPeer),this.add("sys.alliance.list",this.listAlliance),this.add("sys.alliance.export",this.exportAlliance),this.add("sys.alliance.getpeer",this.getPeer),this.add("sys.alliance.delpeer",this.delPeer),this.add("estimate",this.routerOfEstimate),this.add("estimate.fee",this.estimateFee),this.add("estimate.smartfee",this.estimateSmartFee),this.add("estimate.priority",this.estimatePriority),this.add("estimate.smartpriority",this.estimateSmartPriority),this.add("mempool",this.routerOfMempool),this.add("mempool.info",this.getMempoolInfo),this.add("mempool.orphans",this.getMempoolOrphans),this.add("mempool.reset",this.resetMempool),this.add("mempool.ancestors",this.getMempoolAncestors),this.add("mempool.descendants",this.getMempoolDescendants),this.add("mempool.entry",this.getMempoolEntry),this.add("mempool.raw",this.getRawMempool),this.add("mempool.snap",this.getMempool),Object.values(_).map(e=>{this.add(e.name,async(t,i)=>this.node.conn_s2local.execute(e.name,t))})}get wallet(){return null}set wallet(e){}async routerOfMiner(e){throw new c(a.MISC_ERROR,"\n miner.generate.admin 生产一个区块\n miner.generateto.admin 使用指定的令牌地址生产一个区块\n miner.set.admin 设置矿机运行状态\n miner.setsync.admin 设置块链同步完成\n miner.setaddr.admin 动态设置奖励地址\n miner.addaddr.admin 动态添加奖励地址\n miner.getaddr.admin 查询实时奖励地址\n miner.check 检查矿机运行状态\n miner.difficulty 检查当前挖矿难度\n ")}async routerOfVote(e){throw new c(a.MISC_ERROR,"\n vote.check 查询指定地址的选举结果\n vote.show 查询当前或指定区间票仓信息\n vote.send 进行投票\n oracle.check 查询指定标记的数据上链情况\n oracle.send 进行数据上链操作\n ")}async routerOfBlock(e){throw new c(a.MISC_ERROR,"\n block.best 获取当前主链顶部区块哈希(小端)\n block.count 获取当前主链区块数量\n block.info 查询区块内容\n block.info.byheight 根据高度查询区块内容\n block.hash 根据高度查询区块哈希(小端)\n block.header 根据哈希(小端)查询区块头\n block.verify 校验区块数据\n block.tips 列表所有分叉的头部信息\n block.invalidate.admin 标记区块无效\n block.reconsider.admin 取消区块无效标记\n block.template.admin 生成或校验区块数据模板\n block.submit.admin 提交区块数据\n block.reset.admin 重置区块头至指定区块\n block.rollback.admin 回滚区块至指定区块\n ")}async routerOfMempool(e){throw new c(a.MISC_ERROR,"\n mempool.info 查看交易池概要信息\n mempool.ancestors 向前搠源交易池中指定交易\n mempool.descendants 向后搠源交易池中指定交易\n mempool.entry 查询交易记录\n mempool.raw 查询交易池中原始数据\n mempool.snap 查询交易池中原始数据\n mempool.reset 重置内存池\n ")}async routerOfEstimate(e){throw new c(a.MISC_ERROR,"\n estimate.fee 评估费用\n estimate.smartfee 评估智能费用\n estimate.priority 评估优先权\n estimate.smartpriority 评估智能优先权\n ")}async routerOfSys(e){throw new c(a.MISC_ERROR,"\n sys.info 查询系统概要信息\n sys.blockinfo 查询块链概要信息\n sys.memoryinfo 查询交易池概要信息\n sys.networkinfo 查询网络概要信息\n sys.nettotals 查询网速概要\n sys.peerinfo 查询对等节点概要信息\n sys.mininginfo 查询矿机概要信息\n sys.txoinfo 查询TXO概要信息\n sys.prune 修剪块链数据\n sys.ping PING所有管道\n sys.setloglevel 设置日志等级\n sys.setban 禁止管道\n sys.listbanned 列表已禁管道\n sys.clearbanned 清除已禁管道\n sys.addnode 添加管道\n sys.addednodeinfo 查询管道信息\n sys.connectioncount 查询连接数\n sys.disconnectnode 断开管道\n sys.hashrate 查询网络哈希速率\n sys.work 获取工作量\n sys.worklp 获取工作量\n sys.setmocktime 修改网络时间戳\n sys.message.sign 签署网络消息\n sys.message.signk 指定私钥签署网络消息\n sys.message.verify 验证网络消息\n sys.stop.admin 停止节点运行\n sys.decodescript 解码脚本\n sys.validateaddress 校验地址\n sys.zap 移除超龄挂起交易\n sys.createmultisig 生成多签脚本\n sys.walletdb.stop 停止钱包数据库的运行\n sys.walletdb.create 重新创立钱包数据库\n sys.walletdb.destroy 新增RPC指令:彻底删除钱包数据库\n sys.createwitnessaddress 生成见证地址\n sys.groupPrefix 添加/删除ACL前缀成员\n sys.groupSuffix 添加/删除ACL后缀成员\n sys.createAuthToken 制备令牌固定量\n sys.changeSpecialCp 添加特约商户\n sys.alliance 联盟管理\n sys.log 审计日志查询\n ")}async routerOfAlliance(e){throw new c(a.MISC_ERROR,"\n sys.alliance.info 查询联盟节点信息\n sys.alliance.gettoken 生成信道令牌\n sys.alliance.addpeer 添加认证信道\n sys.alliance.delpeer 删除认证信道\n sys.alliance.getpeer 查看信道列表\n sys.alliance.create 创建联盟节点证书\n sys.alliance.delete 吊销联盟节点证书\n sys.alliance.list 列表联盟节点证书\n sys.alliance.export 导出联盟节点证书\n ")}async routerOfComm(e){throw new c(a.MISC_ERROR,"\n comm.notify 通知(钱包)\n comm.listNotify 查询通知列表(钱包)\n comm.secret 安全通信(钱包)\n comm.listContact 列表安全通信联系人\n comm.scanContact 扫描指定交易,用关联地址建立通信联系人\n ")}async routerOfTx(e){throw new c(a.MISC_ERROR,"\n tx.raw.get 获取交易原始数据\n tx.raw.decode 解码交易原始数据\n tx.raw.create 创建交易,返回交易原始数据\n tx.raw.send 发送交易数据返回交易ID\n tx.get 查询交易记录\n tx.list.address 按地址查询交易列表\n tx.list.addresses 按地址列表查询交易列表\n tx.broadcast 广播交易\n tx.prioritise 优化交易池中的交易\n tx.utxo 查询UTXO信息\n tx.spender 查询硬币的消费者\n tx.mstrans 列表待处理多签交易\n tx.mstrans.sign 签署待处理多签交易\n tx.coins 查询硬币信息\n tx.coins.byoutpoint 查询硬币信息\n tx.txo.proof 查询TXO工作量信息\n tx.txo.verifyproof 校验TXO工作量信息\n tx.get.wallet 查询交易记录(钱包)\n tx.list 交易列表(钱包)\n tx.create 创建交易(钱包)\n tx.send 发送交易(钱包)\n tx.last 最近的交易\n tx.pending 查询挂起交易\n tx.range 范围查询\n tx.history 查询历史交易\n tx.history.since 查询指定日期后的历史交易\n tx.resend.admin 重发当前钱包的挂起交易\n tx.resendall.admin 重发所有钱包的挂起交易\n tx.importprunedfunds.admin 导入修剪资金\n tx.removeprunedfunds.admin 移除已导入修剪资金\n tx.fund.admin 填充资金\n tx.sign.admin 签署交易(钱包)\n tx.abandon.admin 丢弃指定交易\n tx.abandonall.admin 废弃所有挂起的交易\n tx.raw.sign.admin 签署交易\n ")}async exportAlliance(e,t,i){if(t)throw new c(a.MISC_ERROR,"sys.alliance.export: 批量导出联盟成员证书");for(var s of this.node.config.alliances)s=s.split("|"),r.unsupported||r.writeFile(`${this.network.type}-${s[0]}-${s[1]}.keystore`,s[2],"utf8")}async listAlliance(e,t,i){if(t)throw new c(a.MISC_ERROR,"sys.alliance.list: 列表现有联盟成员");var r,s=[];t={name:this.node.config.allianceName,id:this.node.config.allianceNodeId,address:this.node.config.allianceAwardAddress,host:this.node.config.host+":"+this.node.config.port};for(r of(t.voted=(await this.chain.db.voteMgr.checkVote(t.address)).result,s.push(t),this.node.config.alliances)){var n;(n={name:(n=r.split("|"))[0],id:n[1],address:n[4],host:n[5]}).voted=(await this.chain.db.voteMgr.checkVote(n.address)).result,s.push(n)}return s}async addPeer(e,t,i){if(t||e.length<2)throw new c(a.MISC_ERROR,"sys.alliance.addpeer host key: 添加授信信道 地址 信道公钥");return this._addPeer(e)}async _addPeer(e){var t=(e=new x([e])).str(0),i=e.str(1),r=e.str(2);e=e.uint(3);return t&&i?(i==d.publicKeyCreate(Buffer.from(this.node.config.identityKey,"hex"),!0).toString("hex")||this.node.pool.authdb.getKnown(t)||(await this.node.rpc.addNode([t,"add"]),this.node.pool.authdb.addKnown(t,Buffer.from(i,"hex"))),r==this.node.config.allianceName&&e==this.node.config.allianceNodeId&&(this.node.config.abandon=!1,this.node.config.saveAsync()),{host:t,key:i}):null}async getPeer(e,t,i){if(t)throw new c(a.MISC_ERROR,"sys.alliance.getpeer: 查看授信信道");return{peer:this.node.config.knownPeers}}async delPeer(e,t,i){if(t||e.length<1)throw new c(a.MISC_ERROR,"sys.alliance.delpeer hostname: 删除授信信道 信道地址");var r;t={host:new x([e]).str(0)};return(e=this.node.require("walletdb"))&&(e=await e.get("primary"))&&(r=v.signObj(t,e.notifyKey.privateKey),await e.commNotify({dst:e.notifyAddress,content:JSON.stringify({cmd:"peerdel",payload:t,sig:r,pub:e.notifyKey.publicKey.toString("hex")})})),t}async _delPeer(e){let t={host:(e=new x([e])).str(0),allianceName:e.str(1),allianceNodeId:e.uint(2)};var i=this.node.config.knownPeers.length;for(let e=0;e<i;e++)if(-1!=this.node.config.knownPeers[e].indexOf(t.host)){var r=this.node.config.knownPeers.splice(e,1);t.key=r[0].split(" ")[1].replace(" ",""),this.pool.authdb.delKnown(t.host),await this.node.rpc.addNode([t.host,"remove"]),setTimeout(()=>{var e,i;for(e of this.pool.peers.getbyhost(t.host))e.destroy();for(i of this.pool.peers.getInbound())i.destroy()},6e3);break}return t.allianceName==this.node.config.allianceName&&t.allianceNodeId==this.node.config.allianceNodeId&&(this.node.config.abandon=!0,this.node.config.saveAsync()),t}async getAllianceInfo(e,t,i){if(t)throw new c(a.MISC_ERROR,"sys.alliance.info: 获取联盟信息");t=this.node.config.identityKey;var r=(t=d.publicKeyCreate(Buffer.from(t,"hex"),!0)).toString("hex");t=R.fromWitnessPubkeyhash(l.hash160(t),this.network.type).toString();return{alliancePrivateKey:this.node.config.alliancePrivateKey,peerIdentity:r,awardAddress:t,allianceName:this.node.config.allianceName,allianceNodeId:this.node.config.allianceNodeId}}async voteShow(e,t){if(t)throw new c(a.MISC_ERROR,"vote.show [zoneId]: 查询选举结果 [指定区间]");return t=new x([e]).uint(0,0),this.chain.db.voteMgr.getVote(t)}async oracleCheck(e,t){if(t)throw new c(a.MISC_ERROR,"oracle.check key: 查询上链数据 数据标记");return{k:t=new x([e]).str(0,""),v:(e=await this.chain.db.voteMgr.checkOracle(t)).v,vote:e.vote}}async voteCheck(e,t){if(t)throw new c(a.MISC_ERROR,"vote.check address: 查询选举结果 指定地址");return t=new x([e]).str(0,""),this.chain.db.voteMgr.checkVote(t)}async changeSpecialCp(e,t){if(t||e.length<1)throw new c(a.MISC_ERROR,'sys.changeSpecialCp action[0-view 1-add 2-remove 3-clear] "[cid]": 添加/删除/清空特约商户');e=(t=new x([e])).uint(0,0);var i=t.array(1);if(e){switch(e){case 1:for(var r of i)this.node.specialCp.add(r);break;case 2:for(var s of i)this.node.specialCp.del(s);break;case 3:this.node.specialCp.clear()}await this.node.config.saveAsync()}return this.node.specialCp.content()}async setDevMode(e,t){if(t||e.length<1)throw new c(a.MISC_ERROR,"sys.devMode remove[true,false]: 设置/取消开发模式");return t=new x([e]).bool(0,!1),this.node.config.devMode=t,(e=this.node.require("walletdb"))&&(e.options.devMode=t),(e=this.node.require("contractdb"))&&(e.options.devMode=t),await this.node.config.saveAsync(),t}async addGroupPrefix(e,t){if(t||e.length<1)throw new c(a.MISC_ERROR,'sys.groupPrefix "[[prefix, cid]]" remove[true,false]: 添加/删除前缀成员');e=(t=new x([e])).array(0);var i,r=t.bool(1,!1);for(i of e)r?(this.node.acl.delGroupPrefix(i[0],i[1]),await this.logAudit({address:this.node.config.coinbaseAddress||"",oper:"usermanager",hash:"unforbid/"+i[1],index:0})):(this.node.acl.addGroupPrefix(i[0],i[1]),await this.logAudit({address:this.node.config.coinbaseAddress||"",oper:"usermanager",hash:"forbid/"+i[1],index:0}));return await this.node.config.saveAsync(),!0}async logAudit(e){var t=this.node.require("walletdb");t&&await t.logAudit(e)}async rescan(e,t){if(t)throw new c(a.MISC_ERROR,'sys.rescan ( "height" )');var i;t=new x([e]).u32(0,0);for(i of await this.chain.db.db.keys({gte:M.c(k.NULL_HASH,0),lte:M.c(k.HIGH_HASH,4294967295),parse:e=>M.cc(e)})){var r=await this.chain.getCoin(i[0],i[1]);r&&this.node.transmit("utxo.add",{hash:r.hash,index:r.index,address:r.getAddress().toString(),value:r.value,height:r.height})}return this.node.scan(t,null,async(e,t)=>{await this.node.fire("sys.rescan",e,t)}),!0}async addGroupSuffix(e,t){if(t||e.length<1)throw new c(a.MISC_ERROR,'sys.groupSuffix "[[suffix, cid]]" remove[true,false]: 添加/删除后缀成员');e=(t=new x([e])).array(0);var i,r=t.bool(1,!1);for(i of e)r?this.node.acl.delGroupSuffix(i[0],i[1]):this.node.acl.addGroupSuffix(i[0],i[1]);return await this.node.config.saveAsync(),!0}async heapDump(e,t){return this.node.config.heapDump(),!0}async getInfo(e,t){if(t||0!==e.length)throw new c(a.MISC_ERROR,"sys.info");t=this.mempool?this.mempool.map.size:0,e=this.mempool?this.mempool.getSize():0;let i=this.pool.hosts.getLocal();return i=i||this.pool.hosts.address,{version:h.version,testnet:"main"!=this.network.type,paytxfee:g.btc(this.network.feeRate,!0),relayfee:g.btc(this.network.minRelay,!0),network:this.network.type,chain:{height:this.chain.height,tip:this.chain.tip.rhash(),progress:this.chain.getProgress(),difficulty:m.bits2Diff(this.chain.tip.bits)},mempool:{tx:t,size:e},time:{uptime:this.node.uptime(),system:w.now(),adjusted:this.network.now(),offset:this.network.time.offset},memory:w.memoryUsage(),pool:{host:i.host,port:i.port,agent:this.pool.options.agent,protocolversion:this.pool.options.version,services:this.pool.options.services.toString(2),outbound:this.pool.peers.outbound,inbound:this.pool.peers.inbound,connections:this.pool.peers.size()},walletversion:0,balance:0,proxy:"",keypoololdest:0,keypoolsize:0,unlocked_until:0,errors:""}}async help(e){if(0===e.length)throw new c(a.MISC_ERROR,"\n miner 记账相关\n block 区块相关\n cp 实体管理\n prop 道具相关\n contract 交易对相关\n tx 交易相关\n sys 系统管理和状态监控\n estimate 评估\n mempool 交易池\n wallet 钱包管理\n key 密钥管理\n account 账户管理\n address 地址管理\n coin 硬币管理\n vote 投票管理\n htlc 跨链操作\n ");return e={method:e[0],params:[]},this.execute(e,!0)}async broadcast(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,"tx.broadcast tx-hex: 广播交易 交易HEX字符串");return t=new x([e]).buf(0),s(t,"tx.broadcast: tx-hex is required."),e=B.fromRaw(t),await this.node.sendTX(e),!0}async authtoken(e,t,i){if(t||1!==e.length)throw new c(a.TYPE_ERROR,"sys.createAuthToken tids: 制备令牌固定量 终端编码列表(逗分字符串)");if(t=new x([e]).array(0),!Array.isArray(t))throw new c(a.TYPE_ERROR,"sys.createAuthToken tids: 制备令牌固定量 终端编码列表(逗分字符串)");var r,{aeskey:s,aesiv:n}=S.fromKey(k.ONE_HASH,this.node.keys.hmacSalt,this.network.type).getAes(i.options.cid),o=[];for(r of t){if(!("string"==typeof r&&r.length<=40))throw new c(a.TYPE_ERROR,"各终端编码必须是最大长度40字节的字符串");var h=S.getHmac(r.toString(),this.node.keys.hmacSalt).token,u=this.node.require("walletdb");u&&await u.logAudit({address:this.node.config.coinbaseAddress||"",oper:"usermanager",hash:"create/"+r,index:0}),this.node.config.devMode?o.push({cid:r,encry:w.encrypt(s,n,h),token:h}):o.push({cid:r,encry:w.encrypt(s,n,h)})}return o}async querySCModel(e,t){return Object.keys(E.list).map(e=>E.list[e])}async querySCList(e,t){if(t)throw new c(a.MISC_ERROR,"sc.query (conditions): 查询合约 (查询条件)");t=new x([e]).array(0,[]),e=await this.chain.db.getSCList(t);var i,r=await this.node.cdb.get("primary");for(i of e.list){var s=await r.getBalance(i.options.addr);i.options.confirmed=s.toJSON().confirmed}return e}async scBalance(e,t){if(t)throw new c(a.MISC_ERROR,"sc.balance address: 查询合约库地址余额 指定地址");return t=new x([e]).str(0,""),(await(await this.node.cdb.get("primary")).getBalance(t)).toJSON()}async cpByClass(e,t){if(t||e.length<1||3<e.length)throw new c(a.MISC_ERROR,"cp.byClass cls: 查询CP编码集合 游戏类别 页码 页宽");e=(t=new x([e])).str(0,"");var i=t.uint(1,1),r=(t=t.uint(2,10),[]);return r.push(["size",t]),r.push(["page",i]),r.push(["cls",e]),this.chain.db.cpList.query(r)}async cpById(e,t){if(t||e.length<1)throw new c(a.MISC_ERROR,'cp.byId "clientNo": 根据编码查询厂商记录');return(t=new x([e]).str(0))?this.chain.db.cpList.getRecord(t):null}async cpByName(e,t){if(t||e.length<1)throw new c(a.MISC_ERROR,'cp.byName "name": 根据名称查询厂商记录');return(t=new x([e]).str(0))?this.chain.db.cpList.getItemByName(decodeURIComponent(t)):null}async resetBlock(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,"block.reset.admin [hash | height]: 重置区块头 [区块哈希 | 区块高度]");let i=new x([e]).str(0);if(s(i,"Height is required."),1==this.node.config.mining)throw new c(a.MISC_ERROR,'Use "miner.set.admin false" stop miner first!');if(this.generating||this.node.miner&&this.node.miner.cpu.running)throw new c(a.MISC_ERROR,"Miner is running, stop it first!");return 64!==i.length&&(i=parseInt(i,10)),await this.chain.reset(i),this.logger.info("Chain has been reset: %s.",i),!0}async rollbackBlock(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,"block.rollback.admin [hash | height]: 回滚区块 [区块哈希| 区块高度]");if(t=new x([e]).str(0),s(t,"Height is required."),1==this.node.config.mining)throw new c(a.MISC_ERROR,'Use "miner.set.admin false" stop miner first!');if(this.generating||this.node.miner&&this.node.miner.cpu.running)throw new c(a.MISC_ERROR,"Miner is running, stop it first!");let i=null;if(i=64!==t.length?await this.chain.getEntry(parseInt(t,10)):await this.chain.getEntry(w.revHex(t)))return await this.chain.reorganize(i),this.logger.info("Chain has been reorganize: %s.",t),!0;throw new c(a.MISC_ERROR,"Block not found.")}async stop(e,t){if(t||0!==e.length)throw new c(a.MISC_ERROR,"sys.stop.admin");return this.node.close().catch(e=>{setImmediate(()=>{throw e})}).finally(async()=>{await new Promise(e=>{setTimeout(e,1e4)}),process.exit(0)}),"Stopping."}async getNetworkInfo(e,t){if(t||0!==e.length)throw new c(a.MISC_ERROR,"sys.networkinfo");var i=[];for(const e of this.pool.hosts.local.values())i.push({address:e.addr.host,port:e.addr.port,score:e.score});return{version:h.version,subversion:this.pool.options.agent,protocolversion:this.pool.options.version,localservices:w.hex32(this.pool.options.services),localrelay:!this.pool.options.noRelay,timeoffset:this.network.time.offset,networkactive:this.pool.connected,connections:this.pool.peers.size(),networks:[],relayfee:g.btc(this.network.minRelay,!0),incrementalfee:0,localaddresses:i,warnings:""}}async addNode(e,t){if(t||2!==e.length)throw new c(a.MISC_ERROR,'sys.addnode "node" "add|remove|onetry"');var i=(e=new x([e])).str(0,"");switch(e.str(1,"")){case"add":if(-1!=this.node.config.nodes.indexOf(i))break;this.node.config.nodes.push(i),await this.node.config.saveAsync(),this.pool.hosts.addNode(i);case"onetry":var r=n.parseNetAddress(i,this.network);this.pool.peers.get(r.hostname)||(r=this.pool.createOutbound(r),this.pool.peers.add(r));break;case"remove":-1!=(r=this.node.config.nodes.indexOf(i))&&(this.node.config.nodes.splice(r,1),await this.node.config.saveAsync()),this.pool.hosts.removeNode(i)}return this.getAddedNodeInfo([],t)}async disconnectNode(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,'sys.disconnectnode "node"');return t=new x([e]).str(0,""),e=n.parseIP(t,this.network),(t=this.pool.peers.get(e.hostname))&&t.destroy(),null}async getAddedNodeInfo(e,t){if(t||1<e.length)throw new c(a.MISC_ERROR,'sys.addednodeinfo ( "node" )');t=this.pool.hosts;var i=new x([e]).str(0,"");let r;1===e.length&&(r=n.parseIP(i,this.network));var s=[];for(const e of t.nodes){if(r){if(e.host!==r.host)continue;if(e.port!==r.port)continue}var o=this.pool.peers.get(e.hostname);o&&o.connected?s.push({addednode:e.hostname,connected:o.connected,addresses:[{address:o.hostname(),connected:o.outbound?"outbound":"inbound"}]}):s.push({addednode:e.hostname,connected:!1,addresses:[]})}if(r&&0===s.length)throw new c(a.CLIENT_NODE_NOT_ADDED,"Node has not been added.");return s}async getConnectionCount(e,t){if(t||0!==e.length)throw new c(a.MISC_ERROR,"sys.connectioncount");return this.pool.peers.size()}async getNetTotals(e,t){if(t||0<e.length)throw new c(a.MISC_ERROR,"sys.nettotals");let i=0,r=0;for(let e=this.pool.peers.head();e;e=e.next)i+=e.socket.bytesWritten,r+=e.socket.bytesRead;return{totalbytesrecv:r,totalbytessent:i,timemillis:w.ms()}}async getPeerInfo(e,t){if(t)throw new c(a.MISC_ERROR,"sys.peerinfo onlyOutbound");var i=new x([e]).bool(0,!1),r=[];for(let e=this.pool.peers.head();e;e=e.next)if(!i||e.outbound){let t=this.network.time.known.get(e.hostname());var s=[];null==t&&(t=0);for(const t in e.blockMap.keys()){var n=w.revHex(t);s.push(n)}r.push({id:e.id,addr:e.hostname(),addrlocal:e.local.isNull()?void 0:e.local.hostname,services:w.hex32(e.services),relaytxes:!e.noRelay,lastsend:e.lastSend/1e3|0,lastrecv:e.lastRecv/1e3|0,bytessent:e.socket.bytesWritten,bytesrecv:e.socket.bytesRead,conntime:0!==e.time?(w.ms()-e.time)/1e3|0:0,timeoffset:t,pingtime:-1!==e.lastPong?(e.lastPong-e.lastPing)/1e3:-1,minping:-1!==e.minPing?e.minPing/1e3:-1,version:e.version,subver:e.agent,inbound:!e.outbound,startingheight:e.height,besthash:null,bestheight:0,banscore:e.banScore,inflight:s,whitelisted:!1})}return r}async ping(e,t){if(t||0!==e.length)throw new c(a.MISC_ERROR,"sys.ping");for(let e=this.pool.peers.head();e;e=e.next)e.sendPing();return null}async setBan(e,t){var i=(r=new x([e])).str(0,""),r=r.str(1,"");if(t||e.length<2||"add"!==r&&"remove"!==r)throw new c(a.MISC_ERROR,'sys.setban "ip(/netmask)" "add|remove" (bantime) (absolute)');var s=n.parseNetAddress(i,this.network);switch(r){case"add":this.pool.ban(s);break;case"remove":this.pool.unban(s)}return null}async listBanned(e,t){if(t||0!==e.length)throw new c(a.MISC_ERROR,"sys.listbanned");var i,r,s=[];for([i,r]of this.pool.hosts.banned)s.push({address:i,banned_until:r+this.pool.options.banTime,ban_created:r,ban_reason:""});return s}async clearBanned(e,t){if(t||0!==e.length)throw new c(a.MISC_ERROR,"sys.clearbanned");return this.pool.hosts.clearBanned(),null}async getBlockchainInfo(e,t){if(t||0!==e.length)throw new c(a.MISC_ERROR,"sys.blockinfo");return{chain:"testnet"!==this.network.type?this.network.type:"test",blocks:this.chain.height,headers:this.chain.height,orphan:this.chain.orphanMap.size,bestblockhash:this.chain.tip.rhash(),difficulty:m.bits2Diff(this.chain.tip.bits),mediantime:await this.chain.getMedianTime(this.chain.tip),verificationprogress:this.chain.getProgress(),chainwork:this.chain.tip.chainwork.toString("hex",64),pruned:this.chain.options.prune,softforks:this.getSoftforks(),bip9_softforks:await this.getBIP9Softforks(),pruneheight:this.chain.options.prune?Math.max(0,this.chain.height-this.network.block.keepBlocks):null}}async getBestBlockHash(e,t){if(t||0!==e.length)throw new c(a.MISC_ERROR,"block.best: 获取当前主链顶部区块哈希(小端)");return this.chain.tip.rhash()}async getBlockCount(e,t){if(t||0!==e.length)throw new c(a.MISC_ERROR,"block.count: 获取当前主链区块数量");return this.chain.tip.height}async getBlock(e,t){if(t||e.length<1||3<e.length)throw new c(a.MISC_ERROR,"block.info hash ( bool-verbose bool-details ): 查询区块内容 小端哈希 (展开为对象 更多细节)");e=(t=new x([e])).hash(0);var i=t.bool(1,!0);t=t.bool(2,!1);if(!e)throw new c(a.TYPE_ERROR,"Invalid block hash.");if(!(e=await this.chain.getEntry(e)))throw new c(a.MISC_ERROR,"Block not found");var r=await this.chain.getBlock(e.hash);if(r)return i?this.blockToJSON(e,r,t):r.toRaw().toString("hex");if(this.chain.options.spv)throw new c(a.MISC_ERROR,"Block not available (spv mode)");if(this.chain.options.prune)throw new c(a.MISC_ERROR,"Block not available (pruned data)");throw new c(a.MISC_ERROR,"Can't read block from disk")}async getBlockByHeight(e,t){if(t||e.length<1||3<e.length)throw new c(a.MISC_ERROR,'block.info.byheight "height" ( bool-verbose bool-details ): 根据高度查询区块 高度 (展开为对象 更多细节)');e=(t=new x([e])).u32(0,-1);var i=t.bool(1,!0);t=t.bool(2,!1);if(-1===e)throw new c(a.TYPE_ERROR,"Invalid block height.");if(!(e=await this.chain.getEntry(e)))throw new c(a.MISC_ERROR,"Block not found");var r=await this.chain.getBlock(e.hash);if(r)return i?this.blockToJSON(e,r,t):r.toRaw().toString("hex");if(this.chain.options.spv)throw new c(a.MISC_ERROR,"Block not available (spv mode)");if(this.chain.options.prune)throw new c(a.MISC_ERROR,"Block not available (pruned data)");throw new c(a.DATABASE_ERROR,"Can't read block from disk")}async getBlockHash(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,"block.hash height: 根据高度查询区块哈希(小端)");if(null==(t=new x([e]).u32(0))||t>this.chain.height)throw new c(a.INVALID_PARAMETER,"Block height out of range.");if(e=await this.chain.getHash(t))return w.revHex(e);throw new c(a.MISC_ERROR,"Not found.")}async getCoinByOutpoint(e,t){if(t||2!=e.length)throw new c(a.MISC_ERROR,"tx.coins.byoutpoint txid index: 查询硬币列表 交易ID 交易内索引");return e=(t=new x([e])).hash(0),t=t.u32(1),s(e,"Hash is required."),s(null!=t,"Index is required."),s(!this.chain.options.spv,"Cannot get coins in SPV mode."),(e=await this.node.getCoin(e,t))?e.getJSON(this.network):null}async getSpender(e,t,i){var r=(e=new x([e])).hash(0);e=e.u32(1);return(r=(s(r,"Hash is required."),s(null!=e,"Index is required."),await this.chain.db.getSpender(r,e)))&&(r.txid=w.revHex(r.txid)),r}async getCoins(e,t){if(t||1!=e.length)throw new c(a.MISC_ERROR,"tx.coins addr: 查询硬币列表 地址");t=new x([e]).str(0,""),s(t,"Address is required."),s(!this.chain.options.spv,"Cannot get coins in SPV mode."),e=await this.node.getCoinsByAddress(t.split(","));var i=[];for(const t of e)i.push(t.getJSON(this.network));return i}async getCoinCache(e,t){return this.chain.db.coinCache.size}async getBlockHeader(e,t){if(t||e.length<1||2<e.length)throw new c(a.MISC_ERROR,'block.header "hash" ( verbose ): 查询区块头 哈希(小端) (展开为对象)');if(e=(t=new x([e])).hash(0),t=t.bool(1,!0),!e)throw new c(a.MISC_ERROR,"Invalid block hash.");if(e=await this.chain.getEntry(e))return t?this.headerToJSON(e):e.toRaw().toString("hex",0,80);throw new c(a.MISC_ERROR,"Block not found")}async getChainTips(e,t){if(t||0!==e.length)throw new c(a.MISC_ERROR,"block.tips: 列表所有分叉的头部信息");var i=[];for(const e of await this.chain.getTips()){var r=await this.chain.getEntry(e),n=(s(r),await this.findFork(r)),o=await this.chain.isMainChain(r);i.push({height:r.height,hash:r.rhash(),branchlen:r.height-n.height,status:o?"active":"valid-headers"})}return i}async getDifficulty(e,t){if(t||0!==e.length)throw new c(a.MISC_ERROR,"miner.difficulty: 查看当前挖矿难度");return m.bits2Diff(this.chain.tip.bits)}async resetMempool(e,t){if(t||0!==e.length)throw new c(a.MISC_ERROR,"mempool.reset");if(this.mempool)return this.mempool.reset();throw new c(a.MISC_ERROR,"No mempool available.")}async getMempoolInfo(e,t){if(t||0!==e.length)throw new c(a.MISC_ERROR,"sys.mempoolinfo");if(this.mempool)return{waiting:this.mempool.waiting.keys().length,orphans:this.mempool.orphans.size,size:this.mempool.map.size,bytes:this.mempool.getSize(),usage:this.mempool.getSize(),maxmempool:this.mempool.options.maxSize,mempoolminfee:g.btc(this.mempool.options.minRelay,!0)};throw new c(a.MISC_ERROR,"No mempool available.")}async getMempoolOrphans(e,t){if(!this.mempool)throw new c(a.MISC_ERROR,"No mempool available.");let i=[];return this.mempool.orphans.forEach((e,t)=>{i.push(w.revHex(t))}),i}async getMempoolAncestors(e,t){if(t||e.length<1||2<e.length)throw new c(a.MISC_ERROR,"mempool.ancestors txid (bool-verbose): 向前搠源 交易哈希小端 [展开为对象]");if(e=(t=new x([e])).hash(0),t=t.bool(1,!1),!this.mempool)throw new c(a.MISC_ERROR,"No mempool available.");if(!e)throw new c(a.TYPE_ERROR,"Invalid TXID.");const i=this.mempool.getEntry(e);if(!i)throw new c(a.MISC_ERROR,"Transaction not in mempool.");e=this.mempool.getAncestors(i);var r=[];if(t)for(const t of e)r.push(this.entryToJSON(t));else for(const t of e)r.push(t.txid());return r}async getMempoolDescendants(e,t){if(t||e.length<1||2<e.length)throw new c(a.MISC_ERROR,"mempool.descendants txid (bool-verbose): 向后搠源 交易哈希小端 [展开为对象]");if(e=(t=new x([e])).hash(0),t=t.bool(1,!1),!this.mempool)throw new c(a.MISC_ERROR,"No mempool available.");if(!e)throw new c(a.TYPE_ERROR,"Invalid TXID.");const i=this.mempool.getEntry(e);if(!i)throw new c(a.MISC_ERROR,"Transaction not in mempool.");e=this.mempool.getDescendants(i);var r=[];if(t)for(const t of e)r.push(this.entryToJSON(t));else for(const t of e)r.push(t.txid());return r}async getMempoolEntry(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,"mempool.entry txid: 查询交易记录");if(t=new x([e]).hash(0),!this.mempool)throw new c(a.MISC_ERROR,"No mempool available.");if(!t)throw new c(a.TYPE_ERROR,"Invalid TXID.");if(e=this.mempool.getEntry(t))return this.entryToJSON(e);throw new c(a.MISC_ERROR,"Transaction not in mempool.")}async getMempool(e,t){if(t||0<e.length)throw new c(a.MISC_ERROR,"mempool.snap: 查询交易池原始记录");s(this.mempool,"No mempool available.");var i=[];for(const e of this.mempool.getSnapshot())i.push(w.revHex(e));return i}async getRawMempool(e,t){if(t||1<e.length)throw new c(a.MISC_ERROR,"mempool.raw ( bool-verbose ): 查询交易池原始记录 [展开为对象]");if(t=new x([e]).bool(0,!1),!this.mempool)throw new c(a.MISC_ERROR,"No mempool available.");if(t){var i={};for(const e of this.mempool.map.values())i[e.txid()]=this.entryToJSON(e);return i}return this.mempool.getSnapshot().map(w.revHex)}async getTXOut(e,t){if(t||e.length<2||3<e.length)throw new c(a.MISC_ERROR,'tx.utxo "txid" n ( includemempool )');e=(t=new x([e])).hash(0);var i=t.u32(1);t=t.bool(2,!0);if(this.chain.options.spv)throw new c(a.MISC_ERROR,"Cannot get coins in SPV mode.");if(this.chain.options.prune)throw new c(a.MISC_ERROR,"Cannot get coins when pruned.");if(!e||null==i)throw new c(a.TYPE_ERROR,"Invalid outpoint.");let r;if(t){if(!this.mempool)throw new c(a.MISC_ERROR,"No mempool available.");r=this.mempool.getCoin(e,i)}return(r=r||await this.chain.getCoin(e,i))?{bestblock:this.chain.tip.rhash(),confirmations:r.getDepth(this.chain.height),value:g.btc(r.value,!0),scriptPubKey:this.scriptToJSON(r.script,!0),version:r.version,coinbase:r.coinbase}:null}async getTXOutProof(e,t){if(t||1!==e.length&&2!==e.length)throw new c(a.MISC_ERROR,'tx.txo.proof ["txid",...] ( blockhash )');var i=(t=new x([e])).array(0);const r=t.hash(1);if(this.chain.options.spv)throw new c(a.MISC_ERROR,"Cannot get coins in SPV mode.");if(this.chain.options.prune)throw new c(a.MISC_ERROR,"Cannot get coins when pruned.");if(!i||0===i.length)throw new c(a.INVALID_PARAMETER,"Invalid TXIDs.");var s=new x([i]),n=new Set,o=[];let h=null;for(let e=0;e<i.length;e++){const t=s.hash(e);if(!t)throw new c(a.TYPE_ERROR,"Invalid TXID.");if(n.has(t))throw new c(a.INVALID_PARAMETER,"Duplicate txid.");n.add(t),o.push(t),h=t}let u=null;if(r?u=await this.chain.getBlock(r):this.chain.options.indexTX?(e=await this.chain.getMeta(h))&&(u=await this.chain.getBlock(e.block)):(t=await this.chain.getCoin(h,0))&&(u=await this.chain.getBlock(t.height)),!u)throw new c(a.MISC_ERROR,"Block not found.");for(const e of o)if(!u.hasTX(e))throw new c(a.VERIFY_ERROR,"Block does not contain all txids.");return(u=I.fromHashes(u,o)).toRaw().toString("hex")}async verifyTXOutProof(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,'tx.txo.verifyproof "proof"');if(!(t=new x([e]).buf(0)))throw new c(a.TYPE_ERROR,"Invalid hex string.");if(!(e=I.fromRaw(t)).verify())return[];if(!await this.chain.getEntry(e.hash("hex")))throw new c(a.MISC_ERROR,"Block not found in chain.");var i=[];for(const t of e.getTree().matches)i.push(w.revHex(t.toString("hex")));return i}async getTXOutSetInfo(e,t){if(t||0!==e.length)throw new c(a.MISC_ERROR,"sys.txoinfo");if(this.chain.options.spv)throw new c(a.MISC_ERROR,"Chainstate not available (SPV mode).");return{height:this.chain.height,bestblock:this.chain.tip.rhash(),transactions:this.chain.db.state.tx,txouts:this.chain.db.state.coin,bytes_serialized:0,hash_serialized:0,total_amount:g.btc(this.chain.db.state.value,!0)}}async pruneBlockchain(e,t){if(t||0!==e.length)throw new c(a.MISC_ERROR,"sys.prune: 修剪块链数据");if(this.chain.options.spv)throw new c(a.MISC_ERROR,"Cannot prune chain in SPV mode.");if(this.chain.options.prune)throw new c(a.MISC_ERROR,"Chain is already pruned.");if(this.chain.height<this.network.block.pruneAfterHeight)throw new c(a.MISC_ERROR,"Chain is too short for pruning.");try{await this.chain.prune()}catch(e){throw new c(a.DATABASE_ERROR,e.message)}}async submitWork(e){var t=await this.locker.lock();try{return await this._submitWork(e)}finally{t()}}async _submitWork(e){var t=this.attempt;if(!t)return!1;if(128!==e.length)throw new c(a.INVALID_PARAMETER,"Invalid work size.");if(L(e=e.slice(0,80)),(e=O.fromHead(e)).prevBlock!==t.prevBlock||e.bits!==t.bits)return!1;if(!e.verify())return!1;if(!(i=this.merkleMap.get(e.merkleRoot)))return!1;var i,[i,r]=i,s=e.nonce;e=e.time;if(!(i=t.getProof(i,r,e,s)).verify(t.target))return!1;let n;r=t.commit(i);try{n=await this.chain.add(r)}catch(e){if("VerifyError"===e.type)return this.logger.warning("RPC block rejected: %s (%s).",r.rhash(),e.reason),!1;throw e}return!!n||(this.logger.warning("RPC block rejected: %s (bad-prevblk).",r.rhash()),!1)}async createWork(e){var t=await this.locker.lock();try{return await this._createWork(e)}finally{t()}}async _createWork(){var e=await this.updateWork(),t=this.nonce1,i=this.nonce2,r=e.time,s=Buffer.allocUnsafe(128);s.fill(0),t=e.getRoot(t,i);return e.getHeader(t,r,0).copy(s,0),s[80]=128,s.writeUInt32BE(640,s.length-4,!0),L(s),{data:s.toString("hex"),target:e.target.toString("hex"),height:e.height}}async getWorkLongpoll(e,t){if(t||0!=e.length)throw new c(a.MISC_ERROR,"sys.worklp");return await this.longpoll(),this.createWork()}async getWork(e,t){if(t||1<e.length)throw new c(a.MISC_ERROR,'sys.work ( "data" )');if(1!==e.length)return this.createWork();if(t=new x([e]).buf(0))return this.submitWork(t);throw new c(a.TYPE_ERROR,"Invalid work data.")}async submitBlock(e,t){if(t||e.length<1||2<e.length)throw new c(a.MISC_ERROR,'block.submit.admin "hexdata" ( "jsonparametersobject" ): 提交区块数据 区块数据HEX串');return t=new x([e]).buf(0),e=A.fromRaw(t),this.addBlock(e)}async getBlockTemplate(e,t){if(t||1<e.length)throw new c(a.MISC_ERROR,"block.template.admin ( \"{'mode':'template | proposal', 'data': '{hex}', 'rules':['segwit']}\" ): 生成或校验区块数据模板");if(t=new x([e]).obj(0,{}),"template"!==(t=(e=new x([t])).str("mode","template"))&&"proposal"!==t)throw new c(a.INVALID_PARAMETER,"Invalid mode.");if("proposal"===t){if(!(t=e.buf("data")))throw new c(a.TYPE_ERROR,"Missing data parameter.");if((t=A.fromRaw(t)).prevBlock!==this.chain.tip.hash)return"inconclusive-not-best-prevblk";try{await this.chain.verifyBlock(t)}catch(e){if("VerifyError"===e.type)return e.reason;throw e}return null}let i=e.u32("maxversion",-1),r=e.array("rules");r&&(i=-1);let s=!1;if(t=e.array("capabilities")){let e=!1,i=!1;for(const r of t){if("string"!=typeof r)throw new c(a.TYPE_ERROR,"Invalid capability.");switch(r){case"coinbasetxn":e=!0;break;case"coinbasevalue":i=!0}}if(i=!0,e&&!i){if(0===this.miner.addresses.length)throw new c(a.MISC_ERROR,"No addresses available for coinbase.");s=!0}}return(t=e.str("longpollid"))&&await this.handleLongpoll(t),r=r||[],this.createTemplate(i,s,r)}async createTemplate(e,t,i){var r=await this.locker.lock();try{return await this._createTemplate(e,t,i)}finally{r()}}async _createTemplate(e,t,i){var r=await this.getTemplate(),n=r.witness?1:u.WITNESS_SCALE_FACTOR,o=["time","transactions","prevblock"],h=(2<=e&&o.push("version/force"),t&&(o.push("coinbase"),o.push("coinbase/append"),o.push("generation")),new Map);for(let e=0;e<r.items.length;e++){var l=r.items[e];h.set(l.hash,e+1)}var p=[];for(let e=0;e<r.items.length;e++){var d=r.items[e],m=d.tx,g=[];for(let t=0;t<m.inputs.length;t++){var y=m.inputs[t];null!=(y=h.get(y.prevout.hash))&&-1===g.indexOf(y)&&(s(y<e+1),g.push(y))}p.push({data:m.toRaw().toString("hex"),txid:m.txid(),hash:m.wtxid(),depends:g,fee:d.fee,sigops:d.sigops/n|0,weight:m.getWeight()})}let v=r.version;var b={},k=[];for(const e of this.network.deploys){var x=await this.chain.getState(this.chain.tip,e);let t=e.name;switch(x){case f.thresholdStates.DEFINED:case f.thresholdStates.FAILED:break;case f.thresholdStates.LOCKED_IN:v|=1<<e.bit;case f.thresholdStates.STARTED:e.force||(-1===i.indexOf(t)&&(v&=~(1<<e.bit)),e.required&&(t="!"+t)),b[t]=e.bit;break;case f.thresholdStates.ACTIVE:if(!e.force&&e.required){if(-1===i.indexOf(t))throw new c(a.INVALID_PARAMETER,`Client must support ${t}.`);t="!"+t}k.push(t);break;default:s(!1,"Bad state.")}}var S;e={capabilities:["proposal"],mutable:o,version:v>>>=0,rules:k,vbavailable:b,vbrequired:0,height:r.height,previousblockhash:w.revHex(r.prevBlock),target:w.revHex(r.target.toString("hex")),bits:w.hex32(r.bits),noncerange:"00000000ffffffff",curtime:r.time,mintime:r.mtp+1,maxtime:r.time+7200,expires:r.time+7200,sigoplimit:u.MAX_BLOCK_SIGOPS_COST/n|0,sizelimit:u.MAX_BLOCK_SIZE,weightlimit:void 0,longpollid:this.chain.tip.rhash()+w.pad32(this.totalTX()),submitold:!1,coinbaseaux:{flags:r.coinbaseFlags.toString("hex")},coinbasevalue:void 0,coinbasetxn:void 0,default_witness_commitment:void 0,transactions:p};return r.witness&&(e.sizelimit=u.MAX_RAW_BLOCK_SIZE,e.weightlimit=u.MAX_BLOCK_WEIGHT),t?((t=(o=r.toCoinbase()).inputs[0]).script.pop(),t.script.compile(),r.witness&&(S=o.outputs.pop(),s(S.script.isCommitment()),t.witness.clear()),o.refresh(),e.coinbasetxn={data:o.toRaw().toString("hex"),txid:o.txid(),hash:o.wtxid(),depends:[],fee:0,sigops:o.getSigopsCost()/n|0,weight:o.getWeight()}):e.coinbasevalue=r.getReward(),-1!==i.indexOf("segwit")&&(e.default_witness_commitment=r.getWitnessScript().toJSON()),e}async getMiningInfo(e,t){if(t||0!==e.length)throw new c(a.MISC_ERROR,"sys.mininginfo");let i=0,r=0,s=0,n=0;if(t=this.attempt){r=t.weight,s=t.items.length+1,n=t.getDifficulty(),i=1e3;for(const e of t.items)i+=e.tx.getBaseSize()}return{blocks:this.chain.height,currentblocksize:i,currentblockweight:r,currentblocktx:s,difficulty:n,errors:"",genproclimit:this.procLimit,networkhashps:await this.getHashRate(120),pooledtx:this.totalTX(),testnet:"main"!=this.network.type,chain:"testnet"!==this.network.type?this.network.type:"test",generate:this.node.config.mining}}async getNetworkHashPS(e,t){if(t||2<e.length)throw new c(a.MISC_ERROR,"getnetworkhashps ( blocks height )");return e=(t=new x([e])).u32(0,120),t=t.u32(1),this.getHashRate(e,t)}async prioritiseTransaction(e,t){if(t||3!==e.length)throw new c(a.MISC_ERROR,"tx.prioritise <txid> <priority delta> <fee delta>");e=(t=new x([e])).hash(0);var i=t.i64(1);t=t.i64(2);if(!this.mempool)throw new c(a.MISC_ERROR,"No mempool available.");if(!e)throw new c(a.TYPE_ERROR,"Invalid TXID");if(null==i||null==t)throw new c(a.TYPE_ERROR,"Invalid fee or priority.");if(e=this.mempool.getEntry(e))return this.mempool.prioritise(e,i,t),!0;throw new c(a.MISC_ERROR,"Transaction not in mempool.")}async verifyBlock(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,'block.verify "hexdata": 校验区块数据 区块数据HEX串');if(!(t=new x([e]).buf(0)))throw new c(a.TYPE_ERROR,"Invalid block hex.");if(this.chain.options.spv)throw new c(a.MISC_ERROR,"Cannot verify block in SPV mode.");e=A.fromRaw(t);try{await this.chain.verifyBlock(e)}catch(e){if("VerifyError"===e.type)return e.reason;throw e}return null}async getGenerate(e,t){if(t||0!==e.length)throw new c(a.MISC_ERROR,"miner.check: 检查矿机运行状态");return{mode:this.node.config.mining}}async setSync(e,t){this.chain.synced=!0,this.chain.emit("chain.full")}async getMinerAddr(e,t){if(t)throw new c(a.MISC_ERROR,"miner.getaddr.admin: 查询矿机奖励地址");return{address:this.node.config.coinbaseAddress}}async addMinerAddr(e,t){if(t)throw new c(a.MISC_ERROR,"miner.addaddr.admin address: 添加矿机奖励地址");(t=new x([e]).str(0,""))&&(this.miner.addresses.unshift(t),this.node.config.coinbaseAddress=this.miner.addresses.reduce((e,t)=>e+(e?","+t:t),""),await this.node.config.saveAsync())}async setMinerAddr(e,t){if(t)throw new c(a.MISC_ERROR,"miner.setaddr.admin addresses: 设置矿机奖励地址");let i=new x([e]).array(0,"");if(!i||0==i.length){if((t=await this.execute({method:"address.index",params:[1]})).error)throw new c(a.MISC_ERROR,"获取默认矿机奖励地址出错");i=[t.result]}this.miner.addresses=i.reduce((e,t)=>(e.push(new R(t)),e),[]),this.node.config.coinbaseAddress=i.reduce((e,t)=>e+(e?","+t:t),""),await this.node.config.saveAsync()}async remoteNotifySet(e,t){if(t)throw new c(a.MISC_ERROR,"remoteNotify.set url secret: 设置远程消息推送 远程地址 签名密钥");return e.length<1||(e=(t=new x([e])).str(0,""),t=t.str(1,""),this.node.config.remotenotify=e,this.node.config.remotesecret=t,this.node.config.saveAsync()),{url:this.node.config.remotenotify,secret:this.node.config.remotesecret}}async setGenerate(e,t){if(t||e.length<1||2<e.length)throw new c(a.MISC_ERROR,"miner.set.admin mine ( proclimit ): 设置矿机运行状态 [true|false] (进程数限制)");if(e=(t=new x([e])).bool(0,!1),t=t.u32(1,0),e&&0===this.miner.addresses.length)throw new c(a.MISC_ERROR,"No addresses available for coinbase.");return this.node.config.mining!=e&&(this.node.config.mining=e,this.node.config.saveAsync(),this.procLimit=t,e?this.miner.cpu.start():this.miner.cpu.stop()),{mode:this.node.config.mining}}async generate(e,t){if(this.node.spv)throw new c(a.INVALID_REQUEST,"spv node can not generate new block");if(this.node.config.mining)throw new c(a.INVALID_REQUEST,"Miner is auto-running.");if(t||e.length<1||2<e.length)throw new c(a.MISC_ERROR,"miner.generate.admin numblocks ( passphrase tip ): 生产区块 生产数量 ( 解锁密码 区块头 )");e=(t=new x([e])).u32(0,1);var i=t.str(1);t=t.hash(2);if(0===this.miner.addresses.length)throw new c(a.MISC_ERROR,"No addresses available for coinbase.");return this.mineBlocks(e,null,i,t)}async generateToAddress(e,t){if(this.node.spv)throw new c(a.INVALID_REQUEST,"spv node can not generate new block");if(this.node.config.mining)throw new c(a.INVALID_REQUEST,"Miner is auto-running.");if(t||2!==e.length)throw new c(a.MISC_ERROR,"miner.generateto.admin numblocks address: 生产区块并发奖至 生产数量 奖励地址 ( 解锁密码 区块头 )");e=(t=new x([e])).u32(0,1);var i=t.str(1,""),r=t.str(2);t=t.str(3),i=n.parseAddress(i,this.network);return this.mineBlocks(e,i,r,t)}async createRawTransaction(e,t){if(t||e.length<2||3<e.length)throw new c(a.MISC_ERROR,'tx.raw.create [{"txid":"id","vout":n},...] {"address":amount,"data":"hex",...} ( locktime ): 创建交易,返回交易原始数据');const i=new x([e]);t=i.array(0),e=i.obj(1);var r=i.u32(2);if(!t||!e)throw new c(a.TYPE_ERROR,"Invalid parameters (inputs and sendTo).");var s=new j;null!=r&&(s.locktime=r);for(const e of t){const t=new x([e]);var o=t.hash("txid"),h=t.u32("vout");let i=t.u32("sequence",4294967295);if(s.locktime&&i--,!o||null==h)throw new c(a.TYPE_ERROR,"Invalid outpoint.");var u=new C;u.prevout.hash=o,u.prevout.index=h,u.sequence=i,s.inputs.push(u)}var l=new x([e]),p=new Set;for(const t of Object.keys(e))if("data"===t){var d=l.buf(t);if(!d)throw new c(a.TYPE_ERROR,"Invalid nulldata..");var f=new T;f.value=0,f.script.createScript(d,y.types.NULLDATA),s.outputs.push(f)}else{if(f=(d=n.parseAddress(t,this.network)).toString(this.network),p.has(f))throw new c(a.INVALID_PARAMETER,"Duplicate address");p.add(f);var m=l.ufixed(t,8);if(null==m)throw new c(a.TYPE_ERROR,"Invalid output value.");var g=new T;g.value=m,g.script.fromAddress(d),s.outputs.push(g)}return s.toRaw().toString("hex")}async decodeRawTransaction(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,'tx.raw.decode "hexstring": 解码交易原始数据 交易原始数据');if(s(!this.chain.options.spv,"Cannot get TX in SPV mode."),!(t=new x([e]).buf(0)))throw new c(a.TYPE_ERROR,"Invalid hex string.");e=B.fromRaw(t);if((t=await this.node.getMeta(e.hash("hex")))&&t.block)return t=await this.chain.getEntry(t.block),this.txToJSON(e,t);throw new c(a.TYPE_ERROR,"TX Not Confirmed.")}async decodeScript(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,'sys.decodescript "hex"');t=new x([e]).buf(0);let i=new y;return t&&(i=y.fromRaw(t)),e=R.fromScripthash(i.hash160()),(t=this.scriptToJSON(i)).p2sh=e.toString(this.network),t}async getRawTransaction(e,t){if(t||e.length<1||2<e.length)throw new c(a.MISC_ERROR,'tx.raw.get "txid" ( verbose ): 获取交易原始数据 交易哈希小端 [展开为对象]');if(s(!this.chain.options.spv,"Cannot get TX in SPV mode."),e=(t=new x([e])).hash(0),t=t.bool(1,!1),!e)throw new c(a.TYPE_ERROR,"Invalid TXID.");if(!(e=await this.node.getMeta(e)))throw new c(a.MISC_ERROR,"Transaction not found.");var i=e.tx;if(!t)return i.toRaw().toString("hex");let r;return e.block&&(r=await this.chain.getEntry(e.block)),(t=this.txToJSON(i,r)).time=e.mtime,t.hex=i.toRaw().toString("hex"),t}async getTXByHash(e,t){if(t||1!=e.length)throw new c(a.MISC_ERROR,'tx.get "txid": 查询交易记录 交易哈希小端');t=new x([e]).hash(0);return(e=(s(t,"Hash is required."),s(!this.chain.options.spv,"Cannot get TX in SPV mode."),await this.node.getMeta(t)))?(t=await this.node.getMetaView(e),e.getJSON(this.network,t,this.chain.height)):null}async sendRawTransaction(e,t){if(t||e.length<1||2<e.length)throw new c(a.MISC_ERROR,'tx.raw.send "hexstring" ( allowhighfees ): 发送交易数据返回交易ID 交易原始数据 [允许高手续费]');if(t=new x([e]).buf(0))return e=B.fromRaw(t),await this.node.relay(e),e.txid();throw new c(a.TYPE_ERROR,"Invalid hex string.")}async signRawTransaction(e,t){if(t||e.length<1||4<e.length)throw new c(a.MISC_ERROR,'tx.raw.sign.admin "hexstring" ( [{"txid":"id","vout":n,"scriptPubKey":"hex",redeemScript":"hex"},...] ["privatekey1",...] sighashtype ): 签署交易');const i=new x([e]);t=i.buf(0),e=i.array(1);var r=i.array(2),s=i.str(3);if(!t)throw new c(a.TYPE_ERROR,"Invalid hex string.");if(!this.mempool)throw new c(a.MISC_ERROR,"No mempool available.");var o=j.fromRaw(t),h=(o.view=await this.mempool.getSpentView(o),new Map),u=[];if(r){const e=new x([r]);for(let t=0;t<r.length;t++){var l=e.str(t,"");l=n.parseSecret(l,this.network);h.set(l.getPublicKey("hex"),l),u.push(l)}}if(e)for(const t of e){const e=new x([t]);var p=e.hash("txid"),d=e.u32("vout"),f=e.buf("scriptPubKey"),m=e.ufixed("amount",8),g=e.buf("redeemScript");if(!p||null==d||!f||null==m)throw new c(a.INVALID_PARAMETER,"Invalid UTXO.");p=new P(p,d);var v=y.fromRaw(f);d=T.fromScript(v,m);if(o.view.addOutput(p,d),0!==u.length&&g&&(v.isScripthash()||v.isWitnessScripthash())){if(!g)throw new c(a.INVALID_PARAMETER,"P2SH requires redeem script.");var b=y.fromRaw(g);for(const e of b.code)if(e.data){var w=h.get(e.data.toString("hex"));if(w){w.script=b,w.witness=v.isWitnessScripthash(),w.refresh();break}}}}let k=y.hashType.ALL;if(s){if((t=s.split("|")).length<1||2<t.length)throw new c(a.INVALID_PARAMETER,"Invalid sighash type.");if(null==(k=y.hashType[t[0]]))throw new c(a.INVALID_PARAMETER,"Invalid sighash type.");if(2===t.length){if("ANYONECANPAY"!==t[1])throw new c(a.INVALID_PARAMETER,"Invalid sighash type.");k|=y.hashType.ANYONECANPAY}}return await o.signAsync(u,k,this.workers),{hex:o.toRaw().toString("hex"),complete:o.isSigned()}}async createMultisig(e,t){if(t||e.length<2||2<e.length)throw new c(a.MISC_ERROR,'sys.createmultisig nrequired ["key",...]');e=(t=new x([e])).u32(0,0);var i=t.array(1,[]);t=i.length;if(e<1||t<e||16<t)throw new c(a.INVALID_PARAMETER,"Invalid m and n values.");var r=new x([i]);for(let e=0;e<i.length;e++){var s=r.buf(e);if(!s)throw new c(a.TYPE_ERROR,"Invalid key.");if(!d.publicKeyVerify(s))throw new c(a.INVALID_ADDRESS_OR_KEY,"Invalid key.");i[e]=s}if((e=y.createScript([e,t,i],y.types.MULTISIG)).getSize()>u.MAX_SCRIPT_PUSH)throw new c(a.VERIFY_ERROR,"Redeem script exceeds size limit.");return{address:R.fromScript(e).toString(this.network),redeemScript:e.toJSON()}}async createWitnessAddress(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,'sys.createwitnessaddress "script"');if(t=new x([e]).buf(0))return e=y.fromRaw(t).forWitness(),{address:R.fromScript(e).toString(this.network),witnessScript:e.toJSON()};throw new c(a.TYPE_ERROR,"Invalid script hex.")}async validateAddress(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,'sys.validateaddress "address"');let i;t=new x([e]).str(0,"");try{i=R.fromString(t,this.network)}catch(e){return{isvalid:!1}}return e=y.fromAddress(i),{isvalid:!0,address:i.toString(this.network),scriptPubKey:e.toJSON(),ismine:!1,iswatchonly:!1}}async verifyMessage(e,t){if(t||3!==e.length)throw new c(a.MISC_ERROR,'sys.message.verify "bitcoinaddress" "signature" "message"');e=(t=new x([e])).str(0,"");var i,r,s,h=t.buf(1,null,"base64");t=t.str(2);if(h&&t)return e=n.parseAddress(e,this.network),t=Buffer.from(o+t,"utf8"),t=l.hash256(t),i=d.recover(t,h,0,!0),r=d.recover(t,h,1,!0),s=d.recover(t,h,2,!0),t=d.recover(t,h,3,!0),!!i&&p(l.hash160(i),e.hash)||!!r&&p(l.hash160(r),e.hash)||!!s&&p(l.hash160(s),e.hash)||!!t&&p(l.hash160(t),e.hash);throw new c(a.TYPE_ERROR,"Invalid parameters.")}async signMessageWithPrivkey(e,t){if(t||2!==e.length)throw new c(a.MISC_ERROR,'sys.message.signk "privkey" "message"');return e=(t=new x([e])).str(0,""),t=t.str(1,""),e=n.parseSecret(e,this.network),t=Buffer.from(o+t,"utf8"),t=l.hash256(t),e.sign(t).toString("base64")}async estimateFee(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,"estimate.fee nblocks");if(t=new x([e]).u32(0,1),this.fees)return 0===(e=this.fees.estimateFee(t,!1))?-1:g.btc(e,!0);throw new c(a.MISC_ERROR,"Fee estimation not available.")}async estimatePriority(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,"estimate.priority nblocks");if(t=new x([e]).u32(0,1),this.fees)return this.fees.estimatePriority(t,!1);throw new c(a.MISC_ERROR,"Priority estimation not available.")}async estimateSmartFee(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,"estimate.smartfee nblocks");if(t=new x([e]).u32(0,1),!this.fees)throw new c(a.MISC_ERROR,"Fee estimation not available.");let i=this.fees.estimateFee(t,!0);return{fee:i=0===i?-1:g.btc(i,!0),blocks:t}}async estimateSmartPriority(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,"estimate.smartpriority nblocks");if(t=new x([e]).u32(0,1),this.fees)return{priority:this.fees.estimatePriority(t,!0),blocks:t};throw new c(a.MISC_ERROR,"Priority estimation not available.")}async invalidateBlock(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,'block.invalidate.admin "hash": 标记区块为无效 区块哈希(小端)');if(t=new x([e]).hash(0))return await this.chain.invalidate(t),null;throw new c(a.TYPE_ERROR,"Invalid block hash.")}async reconsiderBlock(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,'block.reconsider.admin "hash": 取消区块无效标记 区块哈希(小端)');if(t=new x([e]).hash(0))return this.chain.removeInvalid(t),null;throw new c(a.TYPE_ERROR,"Invalid block hash.")}async setMockTime(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,"sys.setmocktime timestamp");if(null==(t=new x([e]).u32(0)))throw new c(a.TYPE_ERROR,"Invalid timestamp.");return this.network.time.offset=0,e=this.network.now()-t,this.network.time.offset=-e,null}async getMemoryInfo(e,t){if(t||0!==e.length)throw new c(a.MISC_ERROR,"sys.memoryinfo");return w.memoryUsage()}async setLogLevel(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,'sys.setloglevel "level"');return t=new x([e]).str(0,""),this.logger.setLevel(t),null}async handleLongpoll(e){if(74!==e.length)throw new c(a.INVALID_PARAMETER,"Invalid longpoll ID.");var t=e.slice(0,64);e=parseInt(e.slice(64,74),10);if(!w.isHex(t)||!w.isU32(e))throw new c(a.INVALID_PARAMETER,"Invalid longpoll ID.");e=w.revHex(t),this.chain.tip.hash===e&&await this.longpoll()}longpoll(){return new Promise((e,t)=>{this.pollers.push(b.job(e,t))})}refreshBlock(){var e=this.pollers;this.attempt=null,this.lastActivity=0,this.merkleMap.clear(),this.nonce1=0,this.nonce2=0,this.pollers=[];for(const t of e)t.resolve()}bindChain(){this.boundChain||(this.boundChain=!0,this.node.on("block.connect",()=>{this.attempt&&this.refreshBlock()}),this.mempool&&this.node.on("tx",()=>{this.attempt&&10<w.now()-this.lastActivity&&this.refreshBlock()}))}async getTemplate(){this.bindChain();let e=this.attempt;return e?this.miner.updateTime(e):(e=await this.miner.createBlock(),this.attempt=e,this.lastActivity=w.now()),e}async updateWork(){this.bindChain();let e=this.attempt;if(e){if(e.address.isNull())throw new c(a.MISC_ERROR,"No addresses available for coinbase.");this.miner.updateTime(e),4294967296==++this.nonce2&&(this.nonce2=0,this.nonce1++);const t=this.nonce1,i=this.nonce2,r=e.getRoot(t,i).toString("hex");this.merkleMap.set(r,[t,i])}else{if(0===this.miner.addresses.length)throw new c(a.MISC_ERROR,"No addresses available for coinbase.");e=await this.miner.createBlock();const t=this.nonce1,i=this.nonce2,r=e.getRoot(t,i).toString("hex");this.attempt=e,this.lastActivity=w.now(),this.merkleMap.set(r,[t,i])}return e}async addBlock(e){var t=await this.locker.lock(),i=await this.chain.locker.lock();try{return await this._addBlock(e)}finally{i(),t()}}async _addBlock(e){this.logger.info("Handling submitted block: %s.",e.rhash());var t,i=await this.chain.getEntry(e.prevBlock);let r;i&&(await this.chain.getDeployments(e.time,i)).hasWitness()&&e.getCommitmentHash()&&(t=(i=e.txs[0]).inputs[0],i.hasWitness()||(this.logger.warning("Submitted block had no witness nonce."),this.logger.debug(i),t.witness.push(k.ZERO_HASH),t.witness.compile(),i.refresh(),e.refresh()));try{r=await this.chain._add(e)}catch(t){if("VerifyError"===t.type)return this.logger.warning("RPC block rejected: %s (%s).",e.rhash(),t.reason),"rejected: "+t.reason;throw t}return r?null:(this.logger.warning("RPC block rejected: %s (bad-prevblk).",e.rhash()),"rejected: bad-prevblk")}totalTX(){return this.mempool?this.mempool.map.size:0}getSoftforks(){return[{id:"bip66",version:3,reject:{status:this.chain.state.hasBIP66()}}]}async getBIP9Softforks(){var e=this.chain.tip,t={};for(const r of this.network.deploys){var i=await this.chain.getState(e,r);i=f.thresholdStates.getName(i);t[r.name]={status:i,bit:r.bit,startTime:r.startTime,timeout:r.timeout}}return t}async getTXByAddress(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,"tx.list.address address: 按地址查询交易列表");t=new x([e]).str(0);var i=(s(t,"Address is required."),s(!this.chain.options.spv,"Cannot get TX in SPV mode."),[]);for(const e of await this.node.getMetaByAddress(t)){var r=await this.node.getMetaView(e);i.push(e.getJSON(this.network,r,this.chain.height))}return i}async getVpByAddress(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,"prop.byaddress address: 按地址查询道具列表");return t=new x([e]).str(0),s(t,"Address is required."),s(!this.chain.options.spv,"Cannot get TX in SPV mode."),this.chain.db.getVpByAddress(t)}async getVpByHashIndex(e,t){if(t||2!==e.length)throw new c(a.MISC_ERROR,"prop.byHashIndex hash index: 按输出查询道具");var i,r;e=(t=new x([e])).hash(0),t=t.int(1,0);return(e=(s(e,"Hash is required."),await this.node.getMeta(e)))&&(i=e.tx.outputs[t])?((r=i.getReturnData([N.OP_PROPCREATE,N.OP_PROPEXCHANGE]))&&(r.current={hash:e.tx.rhash("hex"),index:t,address:i.getAddress().toString()}),r):null}async getVpById(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,"prop.byid pid: 按道具编码查询道具列表");t=new x([e]).str(0);var i=[];if(e=await this.chain.db.propList.getVp(t)){t=e.current.hash,e.oper="exchange",i.push({oper:"exchange",pid:e.pid,oid:e.oid,cid:e.cid,pst:e.pst,current:{hash:w.revHex(e.current.hash),index:e.current.index,address:e.current.address},height:e.height});let a=[t,e.current.index];for(;;){var r=await this.node.getMeta(a[0]),s=r.tx;if(!s)break;if(!(s=s.outputs[a[1]]))break;var n=s.getReturnData([N.OP_PROPCREATE,N.OP_PROPEXCHANGE]);if(!n)break;if("new"==n.oper){n.current={hash:w.revHex(a[0]),index:a[1],address:s.getAddress().toString()},n.height=r.height,i.push(n);break}if(!n.prev)break;a=[n.prev.hash,n.prev.index]}}return i}async getTXByAddresses(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,"tx.list.addresses addresses: 按地址集合查询交易列表");t=new x([e]).array(0);var i=(s(t,"Address is required."),s(!this.chain.options.spv,"Cannot get TX in SPV mode."),[]);for(const e of await this.node.getMetaByAddress(t)){var r=await this.node.getMetaView(e);i.push(e.getJSON(this.network,r,this.chain.height))}return i}async getHashRate(e,t){let i=this.chain.tip;if(!(i=null!=t?await this.chain.getEntry(t):i))return 0;s("number"==typeof e),s(0<=e),(e=0===e?i.height%this.network.pow.retargetInterval+1:e)>i.height&&(e=i.height);let r=i.time,n=r,o=i;for(let t=0;t<e;t++){if(!(o=await this.chain.getPrevious(o)))throw new c(a.DATABASE_ERROR,"Not found.");r=Math.min(o.time,r),n=Math.max(o.time,n)}var h;return 0==(t=n-r)?0:(h=i.chainwork.sub(o.chainwork),Number(h.toString())/t)}async mineBlocks(e,t,i,r){var s=await this.locker.lock();try{return await this._mineBlocks(e,t,i,r)}finally{s()}}async _mineBlocks(e,t,i,r){var n=[];for(let o=0;o<e;o++){var a=await this.miner.mineBlock(r,t,i);a=await this.chain.add(a);s(a),n.push(a.rhash())}return n}async findFork(e){for(;e;){if(await this.chain.isMainChain(e))return e;e=await this.chain.getPrevious(e)}throw new Error("Fork not found.")}txToJSON(e,t){var i;let r=0,s=null,n=0;t&&(i=t.height,r=t.time,s=t.rhash(),n=this.chain.height-i+1);var a=[];for(const t of e.inputs){var o={coinbase:void 0,txid:void 0,scriptSig:void 0,txinwitness:void 0,sequence:t.sequence};e.isCoinbase()?o.coinbase=t.script.toJSON():(o.txid=t.prevout.txid(),o.vout=t.prevout.index,o.scriptSig={asm:t.script.toASM(),hex:t.script.toJSON()}),0<t.witness.items.length&&(o.txinwitness=t.witness.items.map(e=>e.toString("hex"))),a.push(o)}var c=[];for(let t=0;t<e.outputs.length;t++){var h=e.outputs[t];c.push({value:g.btc(h.value,!0),n:t,scriptPubKey:this.scriptToJSON(h.script,!0)})}return{txid:e.txid(),hash:e.wtxid(),size:e.getSize(),vsize:e.getVirtualSize(),version:e.version,locktime:e.locktime,vin:a,vout:c,blockhash:s,confirmations:n,time:r,blocktime:r,hex:void 0}}scriptToJSON(e,t){var i=e.getType(),[t]=(i={asm:e.toASM(),hex:void 0,type:y.typesByVal[i],reqSigs:1,addresses:[],p2sh:void 0},t&&(i.hex=e.toJSON()),e.getMultisig());return(t=(-1!==t&&(i.reqSigs=t),R.fromScript(e)))&&(e=t.toString(this.network),i.addresses.push(e)),i}async headerToJSON(e){var t=await this.chain.getMedianTime(e),i=await this.chain.getNextHash(e.hash);return{hash:e.rhash(),confirmations:this.chain.height-e.height+1,height:e.height,version:e.version,versionHex:w.hex32(e.version),merkleroot:w.revHex(e.merkleRoot),time:e.time,mediantime:t,bits:e.bits,difficulty:m.bits2Diff(e.bits),chainwork:e.chainwork.toString("hex",64),previousblockhash:e.prevBlock!==k.NULL_HASH?w.revHex(e.prevBlock):null,nextblockhash:i?w.revHex(i):null}}async blockToJSON(e,t,i){var r,s=await this.chain.getMedianTime(e),n=await this.chain.getNextHash(e.hash),a=[];for(const s of t.txs)i?(r=this.txToJSON(s,e),a.push(r)):a.push(s.txid());return{hash:e.rhash(),confirmations:this.chain.height-e.height+1,strippedsize:t.getBaseSize(),size:t.getSize(),weight:t.getWeight(),height:e.height,version:e.version,versionHex:w.hex32(e.version),merkleroot:w.revHex(e.merkleRoot),coinbase:t.txs[0].inputs[0].script.toJSON(),tx:a,time:e.time,mediantime:s,bits:e.bits,nonce:t.nonce,difficulty:m.bits2Diff(e.bits),chainwork:e.chainwork.toString("hex",64),previousblockhash:e.prevBlock!==k.NULL_HASH?w.revHex(e.prevBlock):null,nextblockhash:n?w.revHex(n):null}}entryToJSON(e){return{size:e.size,fee:g.btc(e.deltaFee,!0),modifiedfee:0,time:e.time,height:e.height,startingpriority:e.priority,currentpriority:e.getPriority(this.chain.height),descendantcount:this.mempool.countDescendants(e),descendantsize:e.descSize,descendantfees:e.descFee,ancestorcount:this.mempool.countAncestors(e),ancestorsize:0,ancestorfees:0,depends:this.mempool.getDepends(e.tx).map(w.revHex)}}}},function(e,t,i){"use strict";
|
|
943
|
+
*/const r=i(59),s=i(0),n=i(78),a=n.errors,o=n.MAGIC_STRING,c=i(77),h=i(82),u=i(7),l=i(6),p=i(158),d=i(26),f=i(124),m=i(121),g=i(33),y=i(17),v=i(107),b=i(36),w=i(2),k=i(3),x=i(84),S=i(68),_=i(233).openapi,E=i(152);i(132).vpItem,i(220);const R=i(13),A=i(75),O=i(89),C=i(113),I=i(131),j=i(69),P=i(45),T=i(28),B=i(20),N=i(19).opcodes,M=i(328);function L(e){for(let i=0;i<e.length;i+=4){var t=e.readUInt32LE(i,!0);e.writeUInt32BE(t,i,!0)}}e.exports=class extends n{constructor(e){super(e),s(e,"RPC requires a Node."),this.node=e,this.keys=e.keys,this.workers=e.workers,this.chain=e.chain,this.mempool=e.mempool,this.pool=e.pool,this.fees=e.fees,this.miner=e.miner,this.logger=e.logger.context("rpc"),this.procLimit=0,this.attempt=null,this.lastActivity=0,this.boundChain=!1,this.nonce1=0,this.nonce2=0,this.merkleMap=new Map,this.pollers=[],this.generating=!1,this.init()}init(){this.add("help",this.help),this.add("miner",this.routerOfMiner),this.add("miner.generate.admin",this.generate),this.add("miner.generateto.admin",this.generateToAddress),this.add("miner.setsync.admin",this.setSync),this.add("miner.setaddr.admin",this.setMinerAddr),this.add("miner.addaddr.admin",this.addMinerAddr),this.add("miner.getaddr.admin",this.getMinerAddr),this.add("miner.set.admin",this.setGenerate),this.add("miner.check",this.getGenerate),this.add("miner.difficulty",this.getDifficulty),this.add("vote",this.routerOfVote),this.add("vote.show",this.voteShow),this.add("vote.check",this.voteCheck),this.add("oracle.check",this.oracleCheck),this.add("block",this.routerOfBlock),this.add("block.best",this.getBestBlockHash),this.add("block.count",this.getBlockCount),this.add("block.info",this.getBlock),this.add("block.info.byheight",this.getBlockByHeight),this.add("block.hash",this.getBlockHash),this.add("block.header",this.getBlockHeader),this.add("block.verify",this.verifyBlock),this.add("block.tips",this.getChainTips),this.add("block.template.admin",this.getBlockTemplate),this.add("block.submit.admin",this.submitBlock),this.add("block.reset.admin",this.resetBlock),this.add("block.rollback.admin",this.rollbackBlock),this.add("block.invalidate.admin",this.invalidateBlock),this.add("block.reconsider.admin",this.reconsiderBlock),this.add("cp.byClass",this.cpByClass),this.add("cp.byName",this.cpByName),this.add("cp.byId",this.cpById),this.add("remoteNotify.set",this.remoteNotifySet),this.add("sc.query",this.querySCList),this.add("sc.balance",this.scBalance),this.add("sc.model",this.querySCModel),this.add("comm",this.routerOfComm),this.add("tx",this.routerOfTx),this.add("tx.raw.get",this.getRawTransaction),this.add("tx.raw.decode",this.decodeRawTransaction),this.add("tx.raw.create",this.createRawTransaction),this.add("tx.raw.send",this.sendRawTransaction),this.add("tx.raw.sign.admin",this.signRawTransaction),this.add("tx.get",this.getTXByHash),this.add("tx.list.address",this.getTXByAddress),this.add("tx.list.addresses",this.getTXByAddresses),this.add("tx.broadcast",this.broadcast),this.add("tx.prioritise",this.prioritiseTransaction),this.add("tx.utxo",this.getTXOut),this.add("tx.txo.proof",this.getTXOutProof),this.add("tx.txo.verifyproof",this.verifyTXOutProof),this.add("tx.coins",this.getCoins),this.add("tx.coincache",this.getCoinCache),this.add("tx.coins.byoutpoint",this.getCoinByOutpoint),this.add("tx.spender",this.getSpender),this.add("prop.byaddress",this.getVpByAddress),this.add("prop.byid",this.getVpById),this.add("prop.byHashIndex",this.getVpByHashIndex),this.add("sys",this.routerOfSys),this.add("sys.info",this.getInfo),this.add("sys.dump",this.heapDump),this.add("sys.blockinfo",this.getBlockchainInfo),this.add("sys.peerinfo",this.getPeerInfo),this.add("sys.networkinfo",this.getNetworkInfo),this.add("sys.memoryinfo",this.getMemoryInfo),this.add("sys.mempoolinfo",this.getMempoolInfo),this.add("sys.mininginfo",this.getMiningInfo),this.add("sys.addednodeinfo",this.getAddedNodeInfo),this.add("sys.txoinfo",this.getTXOutSetInfo),this.add("sys.connectioncount",this.getConnectionCount),this.add("sys.disconnectnode",this.disconnectNode),this.add("sys.nettotals",this.getNetTotals),this.add("sys.prune",this.pruneBlockchain),this.add("sys.ping",this.ping),this.add("sys.setloglevel",this.setLogLevel),this.add("sys.setban",this.setBan),this.add("sys.listbanned",this.listBanned),this.add("sys.clearbanned",this.clearBanned),this.add("sys.addnode",this.addNode),this.add("sys.hashrate",this.getNetworkHashPS),this.add("sys.work",this.getWork),this.add("sys.worklp",this.getWorkLongpoll),this.add("sys.setmocktime",this.setMockTime),this.add("sys.message.verify",this.verifyMessage),this.add("sys.message.signk",this.signMessageWithPrivkey),this.add("sys.stop.admin",this.stop),this.add("sys.decodescript",this.decodeScript),this.add("sys.validateaddress",this.validateAddress),this.add("sys.createmultisig",this.createMultisig),this.add("sys.createwitnessaddress",this.createWitnessAddress),this.add("sys.createAuthToken",this.authtoken),this.add("sys.groupPrefix",this.addGroupPrefix),this.add("sys.devmode",this.setDevMode),this.add("sys.groupSuffix",this.addGroupSuffix),this.add("sys.changeSpecialCp",this.changeSpecialCp),this.add("sys.rescan",this.rescan),this.add("sys.alliance",this.routerOfAlliance),this.add("sys.alliance.info",this.getAllianceInfo),this.add("sys.alliance.addpeer",this.addPeer),this.add("sys.alliance.list",this.listAlliance),this.add("sys.alliance.export",this.exportAlliance),this.add("sys.alliance.getpeer",this.getPeer),this.add("sys.alliance.delpeer",this.delPeer),this.add("estimate",this.routerOfEstimate),this.add("estimate.fee",this.estimateFee),this.add("estimate.smartfee",this.estimateSmartFee),this.add("estimate.priority",this.estimatePriority),this.add("estimate.smartpriority",this.estimateSmartPriority),this.add("mempool",this.routerOfMempool),this.add("mempool.info",this.getMempoolInfo),this.add("mempool.orphans",this.getMempoolOrphans),this.add("mempool.reset",this.resetMempool),this.add("mempool.ancestors",this.getMempoolAncestors),this.add("mempool.descendants",this.getMempoolDescendants),this.add("mempool.entry",this.getMempoolEntry),this.add("mempool.raw",this.getRawMempool),this.add("mempool.snap",this.getMempool),Object.values(_).map(e=>{this.add(e.name,async(t,i)=>this.node.conn_s2local.execute(e.name,t))})}get wallet(){return null}set wallet(e){}async routerOfMiner(e){throw new c(a.MISC_ERROR,"\n miner.generate.admin 生产一个区块\n miner.generateto.admin 使用指定的令牌地址生产一个区块\n miner.set.admin 设置矿机运行状态\n miner.setsync.admin 设置块链同步完成\n miner.setaddr.admin 动态设置奖励地址\n miner.addaddr.admin 动态添加奖励地址\n miner.getaddr.admin 查询实时奖励地址\n miner.check 检查矿机运行状态\n miner.difficulty 检查当前挖矿难度\n ")}async routerOfVote(e){throw new c(a.MISC_ERROR,"\n vote.check 查询指定地址的选举结果\n vote.show 查询当前或指定区间票仓信息\n vote.send 进行投票\n oracle.check 查询指定标记的数据上链情况\n oracle.send 进行数据上链操作\n ")}async routerOfBlock(e){throw new c(a.MISC_ERROR,"\n block.best 获取当前主链顶部区块哈希(小端)\n block.count 获取当前主链区块数量\n block.info 查询区块内容\n block.info.byheight 根据高度查询区块内容\n block.hash 根据高度查询区块哈希(小端)\n block.header 根据哈希(小端)查询区块头\n block.verify 校验区块数据\n block.tips 列表所有分叉的头部信息\n block.invalidate.admin 标记区块无效\n block.reconsider.admin 取消区块无效标记\n block.template.admin 生成或校验区块数据模板\n block.submit.admin 提交区块数据\n block.reset.admin 重置区块头至指定区块\n block.rollback.admin 回滚区块至指定区块\n ")}async routerOfMempool(e){throw new c(a.MISC_ERROR,"\n mempool.info 查看交易池概要信息\n mempool.ancestors 向前搠源交易池中指定交易\n mempool.descendants 向后搠源交易池中指定交易\n mempool.entry 查询交易记录\n mempool.raw 查询交易池中原始数据\n mempool.snap 查询交易池中原始数据\n mempool.reset 重置内存池\n ")}async routerOfEstimate(e){throw new c(a.MISC_ERROR,"\n estimate.fee 评估费用\n estimate.smartfee 评估智能费用\n estimate.priority 评估优先权\n estimate.smartpriority 评估智能优先权\n ")}async routerOfSys(e){throw new c(a.MISC_ERROR,"\n sys.info 查询系统概要信息\n sys.blockinfo 查询块链概要信息\n sys.memoryinfo 查询交易池概要信息\n sys.networkinfo 查询网络概要信息\n sys.nettotals 查询网速概要\n sys.peerinfo 查询对等节点概要信息\n sys.mininginfo 查询矿机概要信息\n sys.txoinfo 查询TXO概要信息\n sys.prune 修剪块链数据\n sys.ping PING所有管道\n sys.setloglevel 设置日志等级\n sys.setban 禁止管道\n sys.listbanned 列表已禁管道\n sys.clearbanned 清除已禁管道\n sys.addnode 添加管道\n sys.addednodeinfo 查询管道信息\n sys.connectioncount 查询连接数\n sys.disconnectnode 断开管道\n sys.hashrate 查询网络哈希速率\n sys.work 获取工作量\n sys.worklp 获取工作量\n sys.setmocktime 修改网络时间戳\n sys.message.sign 签署网络消息\n sys.message.signk 指定私钥签署网络消息\n sys.message.verify 验证网络消息\n sys.stop.admin 停止节点运行\n sys.decodescript 解码脚本\n sys.validateaddress 校验地址\n sys.zap 移除超龄挂起交易\n sys.createmultisig 生成多签脚本\n sys.walletdb.stop 停止钱包数据库的运行\n sys.walletdb.create 重新创立钱包数据库\n sys.walletdb.destroy 新增RPC指令:彻底删除钱包数据库\n sys.createwitnessaddress 生成见证地址\n sys.groupPrefix 添加/删除ACL前缀成员\n sys.groupSuffix 添加/删除ACL后缀成员\n sys.createAuthToken 制备令牌固定量\n sys.changeSpecialCp 添加特约商户\n sys.alliance 联盟管理\n sys.log 审计日志查询\n ")}async routerOfAlliance(e){throw new c(a.MISC_ERROR,"\n sys.alliance.info 查询联盟节点信息\n sys.alliance.gettoken 生成信道令牌\n sys.alliance.addpeer 添加认证信道\n sys.alliance.delpeer 删除认证信道\n sys.alliance.getpeer 查看信道列表\n sys.alliance.create 创建联盟节点证书\n sys.alliance.delete 吊销联盟节点证书\n sys.alliance.list 列表联盟节点证书\n sys.alliance.export 导出联盟节点证书\n ")}async routerOfComm(e){throw new c(a.MISC_ERROR,"\n comm.notify 通知(钱包)\n comm.listNotify 查询通知列表(钱包)\n comm.secret 安全通信(钱包)\n comm.listContact 列表安全通信联系人\n comm.scanContact 扫描指定交易,用关联地址建立通信联系人\n ")}async routerOfTx(e){throw new c(a.MISC_ERROR,"\n tx.raw.get 获取交易原始数据\n tx.raw.decode 解码交易原始数据\n tx.raw.create 创建交易,返回交易原始数据\n tx.raw.send 发送交易数据返回交易ID\n tx.get 查询交易记录\n tx.list.address 按地址查询交易列表\n tx.list.addresses 按地址列表查询交易列表\n tx.broadcast 广播交易\n tx.prioritise 优化交易池中的交易\n tx.utxo 查询UTXO信息\n tx.spender 查询硬币的消费者\n tx.mstrans 列表待处理多签交易\n tx.mstrans.sign 签署待处理多签交易\n tx.coins 查询硬币信息\n tx.coins.byoutpoint 查询硬币信息\n tx.txo.proof 查询TXO工作量信息\n tx.txo.verifyproof 校验TXO工作量信息\n tx.get.wallet 查询交易记录(钱包)\n tx.list 交易列表(钱包)\n tx.create 创建交易(钱包)\n tx.send 发送交易(钱包)\n tx.last 最近的交易\n tx.pending 查询挂起交易\n tx.range 范围查询\n tx.history 查询历史交易\n tx.history.since 查询指定日期后的历史交易\n tx.resend.admin 重发当前钱包的挂起交易\n tx.resendall.admin 重发所有钱包的挂起交易\n tx.importprunedfunds.admin 导入修剪资金\n tx.removeprunedfunds.admin 移除已导入修剪资金\n tx.fund.admin 填充资金\n tx.sign.admin 签署交易(钱包)\n tx.abandon.admin 丢弃指定交易\n tx.abandonall.admin 废弃所有挂起的交易\n tx.raw.sign.admin 签署交易\n ")}async exportAlliance(e,t,i){if(t)throw new c(a.MISC_ERROR,"sys.alliance.export: 批量导出联盟成员证书");for(var s of this.node.config.alliances)s=s.split("|"),r.unsupported||r.writeFile(`${this.network.type}-${s[0]}-${s[1]}.keystore`,s[2],"utf8")}async listAlliance(e,t,i){if(t)throw new c(a.MISC_ERROR,"sys.alliance.list: 列表现有联盟成员");var r,s=[];t={name:this.node.config.allianceName,id:this.node.config.allianceNodeId,address:this.node.config.allianceAwardAddress,host:this.node.config.host+":"+this.node.config.port};for(r of(t.voted=(await this.chain.db.voteMgr.checkVote(t.address)).result,s.push(t),this.node.config.alliances)){var n;(n={name:(n=r.split("|"))[0],id:n[1],address:n[4],host:n[5]}).voted=(await this.chain.db.voteMgr.checkVote(n.address)).result,s.push(n)}return s}async addPeer(e,t,i){if(t||e.length<2)throw new c(a.MISC_ERROR,"sys.alliance.addpeer host key: 添加授信信道 地址 信道公钥");return this._addPeer(e)}async _addPeer(e){var t=(e=new x([e])).str(0),i=e.str(1),r=e.str(2);e=e.uint(3);return t&&i?(i==d.publicKeyCreate(Buffer.from(this.node.config.identityKey,"hex"),!0).toString("hex")||this.node.pool.authdb.getKnown(t)||(await this.node.rpc.addNode([t,"add"]),this.node.pool.authdb.addKnown(t,Buffer.from(i,"hex"))),r==this.node.config.allianceName&&e==this.node.config.allianceNodeId&&(this.node.config.abandon=!1,this.node.config.saveAsync()),{host:t,key:i}):null}async getPeer(e,t,i){if(t)throw new c(a.MISC_ERROR,"sys.alliance.getpeer: 查看授信信道");return{peer:this.node.config.knownPeers}}async delPeer(e,t,i){if(t||e.length<1)throw new c(a.MISC_ERROR,"sys.alliance.delpeer hostname: 删除授信信道 信道地址");var r;t={host:new x([e]).str(0)};return(e=this.node.require("walletdb"))&&(e=await e.get("primary"))&&(r=v.signObj(t,e.notifyKey.privateKey),await e.commNotify({dst:e.notifyAddress,content:JSON.stringify({cmd:"peerdel",payload:t,sig:r,pub:e.notifyKey.publicKey.toString("hex")})})),t}async _delPeer(e){let t={host:(e=new x([e])).str(0),allianceName:e.str(1),allianceNodeId:e.uint(2)};var i=this.node.config.knownPeers.length;for(let e=0;e<i;e++)if(-1!=this.node.config.knownPeers[e].indexOf(t.host)){var r=this.node.config.knownPeers.splice(e,1);t.key=r[0].split(" ")[1].replace(" ",""),this.pool.authdb.delKnown(t.host),await this.node.rpc.addNode([t.host,"remove"]),setTimeout(()=>{var e,i;for(e of this.pool.peers.getbyhost(t.host))e.destroy();for(i of this.pool.peers.getInbound())i.destroy()},6e3);break}return t.allianceName==this.node.config.allianceName&&t.allianceNodeId==this.node.config.allianceNodeId&&(this.node.config.abandon=!0,this.node.config.saveAsync()),t}async getAllianceInfo(e,t,i){if(t)throw new c(a.MISC_ERROR,"sys.alliance.info: 获取联盟信息");t=this.node.config.identityKey;var r=(t=d.publicKeyCreate(Buffer.from(t,"hex"),!0)).toString("hex");t=R.fromWitnessPubkeyhash(l.hash160(t),this.network.type).toString();return{alliancePrivateKey:this.node.config.alliancePrivateKey,peerIdentity:r,awardAddress:t,allianceName:this.node.config.allianceName,allianceNodeId:this.node.config.allianceNodeId}}async voteShow(e,t){if(t)throw new c(a.MISC_ERROR,"vote.show [zoneId]: 查询选举结果 [指定区间]");return t=new x([e]).uint(0,0),this.chain.db.voteMgr.getVote(t)}async oracleCheck(e,t){if(t)throw new c(a.MISC_ERROR,"oracle.check key: 查询上链数据 数据标记");return{k:t=new x([e]).str(0,""),v:(e=await this.chain.db.voteMgr.checkOracle(t)).v,vote:e.vote}}async voteCheck(e,t){if(t)throw new c(a.MISC_ERROR,"vote.check address: 查询选举结果 指定地址");return t=new x([e]).str(0,""),this.chain.db.voteMgr.checkVote(t)}async changeSpecialCp(e,t){if(t||e.length<1)throw new c(a.MISC_ERROR,'sys.changeSpecialCp action[0-view 1-add 2-remove 3-clear] "[cid]": 添加/删除/清空特约商户');e=(t=new x([e])).uint(0,0);var i=t.array(1);if(e){switch(e){case 1:for(var r of i)this.node.specialCp.add(r);break;case 2:for(var s of i)this.node.specialCp.del(s);break;case 3:this.node.specialCp.clear()}await this.node.config.saveAsync()}return this.node.specialCp.content()}async setDevMode(e,t){if(t||e.length<1)throw new c(a.MISC_ERROR,"sys.devMode remove[true,false]: 设置/取消开发模式");return t=new x([e]).bool(0,!1),this.node.config.devMode=t,(e=this.node.require("walletdb"))&&(e.options.devMode=t),(e=this.node.require("contractdb"))&&(e.options.devMode=t),await this.node.config.saveAsync(),t}async addGroupPrefix(e,t){if(t||e.length<1)throw new c(a.MISC_ERROR,'sys.groupPrefix "[[prefix, cid]]" remove[true,false]: 添加/删除前缀成员');e=(t=new x([e])).array(0);var i,r=t.bool(1,!1);for(i of e)r?(this.node.acl.delGroupPrefix(i[0],i[1]),await this.logAudit({address:this.node.config.coinbaseAddress||"",oper:"usermanager",hash:"unforbid/"+i[1],index:0})):(this.node.acl.addGroupPrefix(i[0],i[1]),await this.logAudit({address:this.node.config.coinbaseAddress||"",oper:"usermanager",hash:"forbid/"+i[1],index:0}));return await this.node.config.saveAsync(),!0}async logAudit(e){var t=this.node.require("walletdb");t&&await t.logAudit(e)}async rescan(e,t){if(t)throw new c(a.MISC_ERROR,'sys.rescan ( "height" )');var i;t=new x([e]).u32(0,0);for(i of await this.chain.db.db.keys({gte:M.c(k.NULL_HASH,0),lte:M.c(k.HIGH_HASH,4294967295),parse:e=>M.cc(e)})){var r=await this.chain.getCoin(i[0],i[1]);r&&this.node.transmit("utxo.add",{oper:"utxo.add",hash:r.hash,index:r.index,color:r.isColored,address:r.getAddress().toString(),value:r.value,height:r.height})}return this.node.scan(t,null,async(e,t)=>{await this.node.fire("sys.rescan",e,t)}),!0}async addGroupSuffix(e,t){if(t||e.length<1)throw new c(a.MISC_ERROR,'sys.groupSuffix "[[suffix, cid]]" remove[true,false]: 添加/删除后缀成员');e=(t=new x([e])).array(0);var i,r=t.bool(1,!1);for(i of e)r?this.node.acl.delGroupSuffix(i[0],i[1]):this.node.acl.addGroupSuffix(i[0],i[1]);return await this.node.config.saveAsync(),!0}async heapDump(e,t){return this.node.config.heapDump(),!0}async getInfo(e,t){if(t||0!==e.length)throw new c(a.MISC_ERROR,"sys.info");t=this.mempool?this.mempool.map.size:0,e=this.mempool?this.mempool.getSize():0;let i=this.pool.hosts.getLocal();return i=i||this.pool.hosts.address,{version:h.version,testnet:"main"!=this.network.type,paytxfee:g.btc(this.network.feeRate,!0),relayfee:g.btc(this.network.minRelay,!0),network:this.network.type,chain:{height:this.chain.height,tip:this.chain.tip.rhash(),progress:this.chain.getProgress(),difficulty:m.bits2Diff(this.chain.tip.bits)},mempool:{tx:t,size:e},time:{uptime:this.node.uptime(),system:w.now(),adjusted:this.network.now(),offset:this.network.time.offset},memory:w.memoryUsage(),pool:{host:i.host,port:i.port,agent:this.pool.options.agent,protocolversion:this.pool.options.version,services:this.pool.options.services.toString(2),outbound:this.pool.peers.outbound,inbound:this.pool.peers.inbound,connections:this.pool.peers.size()},walletversion:0,balance:0,proxy:"",keypoololdest:0,keypoolsize:0,unlocked_until:0,errors:""}}async help(e){if(0===e.length)throw new c(a.MISC_ERROR,"\n miner 记账相关\n block 区块相关\n cp 实体管理\n prop 道具相关\n contract 交易对相关\n tx 交易相关\n sys 系统管理和状态监控\n estimate 评估\n mempool 交易池\n wallet 钱包管理\n key 密钥管理\n account 账户管理\n address 地址管理\n coin 硬币管理\n vote 投票管理\n htlc 跨链操作\n ");return e={method:e[0],params:[]},this.execute(e,!0)}async broadcast(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,"tx.broadcast tx-hex: 广播交易 交易HEX字符串");return t=new x([e]).buf(0),s(t,"tx.broadcast: tx-hex is required."),e=B.fromRaw(t),await this.node.sendTX(e),!0}async authtoken(e,t,i){if(t||1!==e.length)throw new c(a.TYPE_ERROR,"sys.createAuthToken tids: 制备令牌固定量 终端编码列表(逗分字符串)");if(t=new x([e]).array(0),!Array.isArray(t))throw new c(a.TYPE_ERROR,"sys.createAuthToken tids: 制备令牌固定量 终端编码列表(逗分字符串)");var r,{aeskey:s,aesiv:n}=S.fromKey(k.ONE_HASH,this.node.keys.hmacSalt,this.network.type).getAes(i.options.cid),o=[];for(r of t){if(!("string"==typeof r&&r.length<=40))throw new c(a.TYPE_ERROR,"各终端编码必须是最大长度40字节的字符串");var h=S.getHmac(r.toString(),this.node.keys.hmacSalt).token,u=this.node.require("walletdb");u&&await u.logAudit({address:this.node.config.coinbaseAddress||"",oper:"usermanager",hash:"create/"+r,index:0}),this.node.config.devMode?o.push({cid:r,encry:w.encrypt(s,n,h),token:h}):o.push({cid:r,encry:w.encrypt(s,n,h)})}return o}async querySCModel(e,t){return Object.keys(E.list).map(e=>E.list[e])}async querySCList(e,t){if(t)throw new c(a.MISC_ERROR,"sc.query (conditions): 查询合约 (查询条件)");t=new x([e]).array(0,[]),e=await this.chain.db.getSCList(t);var i,r=await this.node.cdb.get("primary");for(i of e.list){var s=await r.getBalance(i.options.addr);i.options.confirmed=s.toJSON().confirmed}return e}async scBalance(e,t){if(t)throw new c(a.MISC_ERROR,"sc.balance address: 查询合约库地址余额 指定地址");return t=new x([e]).str(0,""),(await(await this.node.cdb.get("primary")).getBalance(t)).toJSON()}async cpByClass(e,t){if(t||e.length<1||3<e.length)throw new c(a.MISC_ERROR,"cp.byClass cls: 查询CP编码集合 游戏类别 页码 页宽");e=(t=new x([e])).str(0,"");var i=t.uint(1,1),r=(t=t.uint(2,10),[]);return r.push(["size",t]),r.push(["page",i]),r.push(["cls",e]),this.chain.db.cpList.query(r)}async cpById(e,t){if(t||e.length<1)throw new c(a.MISC_ERROR,'cp.byId "clientNo": 根据编码查询厂商记录');return(t=new x([e]).str(0))?this.chain.db.cpList.getRecord(t):null}async cpByName(e,t){if(t||e.length<1)throw new c(a.MISC_ERROR,'cp.byName "name": 根据名称查询厂商记录');return(t=new x([e]).str(0))?this.chain.db.cpList.getItemByName(decodeURIComponent(t)):null}async resetBlock(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,"block.reset.admin [hash | height]: 重置区块头 [区块哈希 | 区块高度]");let i=new x([e]).str(0);if(s(i,"Height is required."),1==this.node.config.mining)throw new c(a.MISC_ERROR,'Use "miner.set.admin false" stop miner first!');if(this.generating||this.node.miner&&this.node.miner.cpu.running)throw new c(a.MISC_ERROR,"Miner is running, stop it first!");return 64!==i.length&&(i=parseInt(i,10)),await this.chain.reset(i),this.logger.info("Chain has been reset: %s.",i),!0}async rollbackBlock(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,"block.rollback.admin [hash | height]: 回滚区块 [区块哈希| 区块高度]");if(t=new x([e]).str(0),s(t,"Height is required."),1==this.node.config.mining)throw new c(a.MISC_ERROR,'Use "miner.set.admin false" stop miner first!');if(this.generating||this.node.miner&&this.node.miner.cpu.running)throw new c(a.MISC_ERROR,"Miner is running, stop it first!");let i=null;if(i=64!==t.length?await this.chain.getEntry(parseInt(t,10)):await this.chain.getEntry(w.revHex(t)))return await this.chain.reorganize(i),this.logger.info("Chain has been reorganize: %s.",t),!0;throw new c(a.MISC_ERROR,"Block not found.")}async stop(e,t){if(t||0!==e.length)throw new c(a.MISC_ERROR,"sys.stop.admin");return this.node.close().catch(e=>{setImmediate(()=>{throw e})}).finally(async()=>{await new Promise(e=>{setTimeout(e,1e4)}),process.exit(0)}),"Stopping."}async getNetworkInfo(e,t){if(t||0!==e.length)throw new c(a.MISC_ERROR,"sys.networkinfo");var i=[];for(const e of this.pool.hosts.local.values())i.push({address:e.addr.host,port:e.addr.port,score:e.score});return{version:h.version,subversion:this.pool.options.agent,protocolversion:this.pool.options.version,localservices:w.hex32(this.pool.options.services),localrelay:!this.pool.options.noRelay,timeoffset:this.network.time.offset,networkactive:this.pool.connected,connections:this.pool.peers.size(),networks:[],relayfee:g.btc(this.network.minRelay,!0),incrementalfee:0,localaddresses:i,warnings:""}}async addNode(e,t){if(t||2!==e.length)throw new c(a.MISC_ERROR,'sys.addnode "node" "add|remove|onetry"');var i=(e=new x([e])).str(0,"");switch(e.str(1,"")){case"add":if(-1!=this.node.config.nodes.indexOf(i))break;this.node.config.nodes.push(i),await this.node.config.saveAsync(),this.pool.hosts.addNode(i);case"onetry":var r=n.parseNetAddress(i,this.network);this.pool.peers.get(r.hostname)||(r=this.pool.createOutbound(r),this.pool.peers.add(r));break;case"remove":-1!=(r=this.node.config.nodes.indexOf(i))&&(this.node.config.nodes.splice(r,1),await this.node.config.saveAsync()),this.pool.hosts.removeNode(i)}return this.getAddedNodeInfo([],t)}async disconnectNode(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,'sys.disconnectnode "node"');return t=new x([e]).str(0,""),e=n.parseIP(t,this.network),(t=this.pool.peers.get(e.hostname))&&t.destroy(),null}async getAddedNodeInfo(e,t){if(t||1<e.length)throw new c(a.MISC_ERROR,'sys.addednodeinfo ( "node" )');t=this.pool.hosts;var i=new x([e]).str(0,"");let r;1===e.length&&(r=n.parseIP(i,this.network));var s=[];for(const e of t.nodes){if(r){if(e.host!==r.host)continue;if(e.port!==r.port)continue}var o=this.pool.peers.get(e.hostname);o&&o.connected?s.push({addednode:e.hostname,connected:o.connected,addresses:[{address:o.hostname(),connected:o.outbound?"outbound":"inbound"}]}):s.push({addednode:e.hostname,connected:!1,addresses:[]})}if(r&&0===s.length)throw new c(a.CLIENT_NODE_NOT_ADDED,"Node has not been added.");return s}async getConnectionCount(e,t){if(t||0!==e.length)throw new c(a.MISC_ERROR,"sys.connectioncount");return this.pool.peers.size()}async getNetTotals(e,t){if(t||0<e.length)throw new c(a.MISC_ERROR,"sys.nettotals");let i=0,r=0;for(let e=this.pool.peers.head();e;e=e.next)i+=e.socket.bytesWritten,r+=e.socket.bytesRead;return{totalbytesrecv:r,totalbytessent:i,timemillis:w.ms()}}async getPeerInfo(e,t){if(t)throw new c(a.MISC_ERROR,"sys.peerinfo onlyOutbound");var i=new x([e]).bool(0,!1),r=[];for(let e=this.pool.peers.head();e;e=e.next)if(!i||e.outbound){let t=this.network.time.known.get(e.hostname());var s=[];null==t&&(t=0);for(const t in e.blockMap.keys()){var n=w.revHex(t);s.push(n)}r.push({id:e.id,addr:e.hostname(),addrlocal:e.local.isNull()?void 0:e.local.hostname,services:w.hex32(e.services),relaytxes:!e.noRelay,lastsend:e.lastSend/1e3|0,lastrecv:e.lastRecv/1e3|0,bytessent:e.socket.bytesWritten,bytesrecv:e.socket.bytesRead,conntime:0!==e.time?(w.ms()-e.time)/1e3|0:0,timeoffset:t,pingtime:-1!==e.lastPong?(e.lastPong-e.lastPing)/1e3:-1,minping:-1!==e.minPing?e.minPing/1e3:-1,version:e.version,subver:e.agent,inbound:!e.outbound,startingheight:e.height,besthash:null,bestheight:0,banscore:e.banScore,inflight:s,whitelisted:!1})}return r}async ping(e,t){if(t||0!==e.length)throw new c(a.MISC_ERROR,"sys.ping");for(let e=this.pool.peers.head();e;e=e.next)e.sendPing();return null}async setBan(e,t){var i=(r=new x([e])).str(0,""),r=r.str(1,"");if(t||e.length<2||"add"!==r&&"remove"!==r)throw new c(a.MISC_ERROR,'sys.setban "ip(/netmask)" "add|remove" (bantime) (absolute)');var s=n.parseNetAddress(i,this.network);switch(r){case"add":this.pool.ban(s);break;case"remove":this.pool.unban(s)}return null}async listBanned(e,t){if(t||0!==e.length)throw new c(a.MISC_ERROR,"sys.listbanned");var i,r,s=[];for([i,r]of this.pool.hosts.banned)s.push({address:i,banned_until:r+this.pool.options.banTime,ban_created:r,ban_reason:""});return s}async clearBanned(e,t){if(t||0!==e.length)throw new c(a.MISC_ERROR,"sys.clearbanned");return this.pool.hosts.clearBanned(),null}async getBlockchainInfo(e,t){if(t||0!==e.length)throw new c(a.MISC_ERROR,"sys.blockinfo");return{chain:"testnet"!==this.network.type?this.network.type:"test",blocks:this.chain.height,headers:this.chain.height,orphan:this.chain.orphanMap.size,bestblockhash:this.chain.tip.rhash(),difficulty:m.bits2Diff(this.chain.tip.bits),mediantime:await this.chain.getMedianTime(this.chain.tip),verificationprogress:this.chain.getProgress(),chainwork:this.chain.tip.chainwork.toString("hex",64),pruned:this.chain.options.prune,softforks:this.getSoftforks(),bip9_softforks:await this.getBIP9Softforks(),pruneheight:this.chain.options.prune?Math.max(0,this.chain.height-this.network.block.keepBlocks):null}}async getBestBlockHash(e,t){if(t||0!==e.length)throw new c(a.MISC_ERROR,"block.best: 获取当前主链顶部区块哈希(小端)");return this.chain.tip.rhash()}async getBlockCount(e,t){if(t||0!==e.length)throw new c(a.MISC_ERROR,"block.count: 获取当前主链区块数量");return this.chain.tip.height}async getBlock(e,t){if(t||e.length<1||3<e.length)throw new c(a.MISC_ERROR,"block.info hash ( bool-verbose bool-details ): 查询区块内容 小端哈希 (展开为对象 更多细节)");e=(t=new x([e])).hash(0);var i=t.bool(1,!0);t=t.bool(2,!1);if(!e)throw new c(a.TYPE_ERROR,"Invalid block hash.");if(!(e=await this.chain.getEntry(e)))throw new c(a.MISC_ERROR,"Block not found");var r=await this.chain.getBlock(e.hash);if(r)return i?this.blockToJSON(e,r,t):r.toRaw().toString("hex");if(this.chain.options.spv)throw new c(a.MISC_ERROR,"Block not available (spv mode)");if(this.chain.options.prune)throw new c(a.MISC_ERROR,"Block not available (pruned data)");throw new c(a.MISC_ERROR,"Can't read block from disk")}async getBlockByHeight(e,t){if(t||e.length<1||3<e.length)throw new c(a.MISC_ERROR,'block.info.byheight "height" ( bool-verbose bool-details ): 根据高度查询区块 高度 (展开为对象 更多细节)');e=(t=new x([e])).u32(0,-1);var i=t.bool(1,!0);t=t.bool(2,!1);if(-1===e)throw new c(a.TYPE_ERROR,"Invalid block height.");if(!(e=await this.chain.getEntry(e)))throw new c(a.MISC_ERROR,"Block not found");var r=await this.chain.getBlock(e.hash);if(r)return i?this.blockToJSON(e,r,t):r.toRaw().toString("hex");if(this.chain.options.spv)throw new c(a.MISC_ERROR,"Block not available (spv mode)");if(this.chain.options.prune)throw new c(a.MISC_ERROR,"Block not available (pruned data)");throw new c(a.DATABASE_ERROR,"Can't read block from disk")}async getBlockHash(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,"block.hash height: 根据高度查询区块哈希(小端)");if(null==(t=new x([e]).u32(0))||t>this.chain.height)throw new c(a.INVALID_PARAMETER,"Block height out of range.");if(e=await this.chain.getHash(t))return w.revHex(e);throw new c(a.MISC_ERROR,"Not found.")}async getCoinByOutpoint(e,t){if(t||2!=e.length)throw new c(a.MISC_ERROR,"tx.coins.byoutpoint txid index: 查询硬币列表 交易ID 交易内索引");return e=(t=new x([e])).hash(0),t=t.u32(1),s(e,"Hash is required."),s(null!=t,"Index is required."),s(!this.chain.options.spv,"Cannot get coins in SPV mode."),(e=await this.node.getCoin(e,t))?e.getJSON(this.network):null}async getSpender(e,t,i){var r=(e=new x([e])).hash(0);e=e.u32(1);return(r=(s(r,"Hash is required."),s(null!=e,"Index is required."),await this.chain.db.getSpender(r,e)))&&(r.txid=w.revHex(r.txid)),r}async getCoins(e,t){if(t||1!=e.length)throw new c(a.MISC_ERROR,"tx.coins addr: 查询硬币列表 地址");t=new x([e]).str(0,""),s(t,"Address is required."),s(!this.chain.options.spv,"Cannot get coins in SPV mode."),e=await this.node.getCoinsByAddress(t.split(","));var i=[];for(const t of e)i.push(t.getJSON(this.network));return i}async getCoinCache(e,t){return this.chain.db.coinCache.size}async getBlockHeader(e,t){if(t||e.length<1||2<e.length)throw new c(a.MISC_ERROR,'block.header "hash" ( verbose ): 查询区块头 哈希(小端) (展开为对象)');if(e=(t=new x([e])).hash(0),t=t.bool(1,!0),!e)throw new c(a.MISC_ERROR,"Invalid block hash.");if(e=await this.chain.getEntry(e))return t?this.headerToJSON(e):e.toRaw().toString("hex",0,80);throw new c(a.MISC_ERROR,"Block not found")}async getChainTips(e,t){if(t||0!==e.length)throw new c(a.MISC_ERROR,"block.tips: 列表所有分叉的头部信息");var i=[];for(const e of await this.chain.getTips()){var r=await this.chain.getEntry(e),n=(s(r),await this.findFork(r)),o=await this.chain.isMainChain(r);i.push({height:r.height,hash:r.rhash(),branchlen:r.height-n.height,status:o?"active":"valid-headers"})}return i}async getDifficulty(e,t){if(t||0!==e.length)throw new c(a.MISC_ERROR,"miner.difficulty: 查看当前挖矿难度");return m.bits2Diff(this.chain.tip.bits)}async resetMempool(e,t){if(t||0!==e.length)throw new c(a.MISC_ERROR,"mempool.reset");if(this.mempool)return this.mempool.reset();throw new c(a.MISC_ERROR,"No mempool available.")}async getMempoolInfo(e,t){if(t||0!==e.length)throw new c(a.MISC_ERROR,"sys.mempoolinfo");if(this.mempool)return{waiting:this.mempool.waiting.keys().length,orphans:this.mempool.orphans.size,size:this.mempool.map.size,bytes:this.mempool.getSize(),usage:this.mempool.getSize(),maxmempool:this.mempool.options.maxSize,mempoolminfee:g.btc(this.mempool.options.minRelay,!0)};throw new c(a.MISC_ERROR,"No mempool available.")}async getMempoolOrphans(e,t){if(!this.mempool)throw new c(a.MISC_ERROR,"No mempool available.");let i=[];return this.mempool.orphans.forEach((e,t)=>{i.push(w.revHex(t))}),i}async getMempoolAncestors(e,t){if(t||e.length<1||2<e.length)throw new c(a.MISC_ERROR,"mempool.ancestors txid (bool-verbose): 向前搠源 交易哈希小端 [展开为对象]");if(e=(t=new x([e])).hash(0),t=t.bool(1,!1),!this.mempool)throw new c(a.MISC_ERROR,"No mempool available.");if(!e)throw new c(a.TYPE_ERROR,"Invalid TXID.");const i=this.mempool.getEntry(e);if(!i)throw new c(a.MISC_ERROR,"Transaction not in mempool.");e=this.mempool.getAncestors(i);var r=[];if(t)for(const t of e)r.push(this.entryToJSON(t));else for(const t of e)r.push(t.txid());return r}async getMempoolDescendants(e,t){if(t||e.length<1||2<e.length)throw new c(a.MISC_ERROR,"mempool.descendants txid (bool-verbose): 向后搠源 交易哈希小端 [展开为对象]");if(e=(t=new x([e])).hash(0),t=t.bool(1,!1),!this.mempool)throw new c(a.MISC_ERROR,"No mempool available.");if(!e)throw new c(a.TYPE_ERROR,"Invalid TXID.");const i=this.mempool.getEntry(e);if(!i)throw new c(a.MISC_ERROR,"Transaction not in mempool.");e=this.mempool.getDescendants(i);var r=[];if(t)for(const t of e)r.push(this.entryToJSON(t));else for(const t of e)r.push(t.txid());return r}async getMempoolEntry(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,"mempool.entry txid: 查询交易记录");if(t=new x([e]).hash(0),!this.mempool)throw new c(a.MISC_ERROR,"No mempool available.");if(!t)throw new c(a.TYPE_ERROR,"Invalid TXID.");if(e=this.mempool.getEntry(t))return this.entryToJSON(e);throw new c(a.MISC_ERROR,"Transaction not in mempool.")}async getMempool(e,t){if(t||0<e.length)throw new c(a.MISC_ERROR,"mempool.snap: 查询交易池原始记录");s(this.mempool,"No mempool available.");var i=[];for(const e of this.mempool.getSnapshot())i.push(w.revHex(e));return i}async getRawMempool(e,t){if(t||1<e.length)throw new c(a.MISC_ERROR,"mempool.raw ( bool-verbose ): 查询交易池原始记录 [展开为对象]");if(t=new x([e]).bool(0,!1),!this.mempool)throw new c(a.MISC_ERROR,"No mempool available.");if(t){var i={};for(const e of this.mempool.map.values())i[e.txid()]=this.entryToJSON(e);return i}return this.mempool.getSnapshot().map(w.revHex)}async getTXOut(e,t){if(t||e.length<2||3<e.length)throw new c(a.MISC_ERROR,'tx.utxo "txid" n ( includemempool )');e=(t=new x([e])).hash(0);var i=t.u32(1);t=t.bool(2,!0);if(this.chain.options.spv)throw new c(a.MISC_ERROR,"Cannot get coins in SPV mode.");if(this.chain.options.prune)throw new c(a.MISC_ERROR,"Cannot get coins when pruned.");if(!e||null==i)throw new c(a.TYPE_ERROR,"Invalid outpoint.");let r;if(t){if(!this.mempool)throw new c(a.MISC_ERROR,"No mempool available.");r=this.mempool.getCoin(e,i)}return(r=r||await this.chain.getCoin(e,i))?{bestblock:this.chain.tip.rhash(),confirmations:r.getDepth(this.chain.height),value:g.btc(r.value,!0),scriptPubKey:this.scriptToJSON(r.script,!0),version:r.version,coinbase:r.coinbase}:null}async getTXOutProof(e,t){if(t||1!==e.length&&2!==e.length)throw new c(a.MISC_ERROR,'tx.txo.proof ["txid",...] ( blockhash )');var i=(t=new x([e])).array(0);const r=t.hash(1);if(this.chain.options.spv)throw new c(a.MISC_ERROR,"Cannot get coins in SPV mode.");if(this.chain.options.prune)throw new c(a.MISC_ERROR,"Cannot get coins when pruned.");if(!i||0===i.length)throw new c(a.INVALID_PARAMETER,"Invalid TXIDs.");var s=new x([i]),n=new Set,o=[];let h=null;for(let e=0;e<i.length;e++){const t=s.hash(e);if(!t)throw new c(a.TYPE_ERROR,"Invalid TXID.");if(n.has(t))throw new c(a.INVALID_PARAMETER,"Duplicate txid.");n.add(t),o.push(t),h=t}let u=null;if(r?u=await this.chain.getBlock(r):this.chain.options.indexTX?(e=await this.chain.getMeta(h))&&(u=await this.chain.getBlock(e.block)):(t=await this.chain.getCoin(h,0))&&(u=await this.chain.getBlock(t.height)),!u)throw new c(a.MISC_ERROR,"Block not found.");for(const e of o)if(!u.hasTX(e))throw new c(a.VERIFY_ERROR,"Block does not contain all txids.");return(u=I.fromHashes(u,o)).toRaw().toString("hex")}async verifyTXOutProof(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,'tx.txo.verifyproof "proof"');if(!(t=new x([e]).buf(0)))throw new c(a.TYPE_ERROR,"Invalid hex string.");if(!(e=I.fromRaw(t)).verify())return[];if(!await this.chain.getEntry(e.hash("hex")))throw new c(a.MISC_ERROR,"Block not found in chain.");var i=[];for(const t of e.getTree().matches)i.push(w.revHex(t.toString("hex")));return i}async getTXOutSetInfo(e,t){if(t||0!==e.length)throw new c(a.MISC_ERROR,"sys.txoinfo");if(this.chain.options.spv)throw new c(a.MISC_ERROR,"Chainstate not available (SPV mode).");return{height:this.chain.height,bestblock:this.chain.tip.rhash(),transactions:this.chain.db.state.tx,txouts:this.chain.db.state.coin,bytes_serialized:0,hash_serialized:0,total_amount:g.btc(this.chain.db.state.value,!0)}}async pruneBlockchain(e,t){if(t||0!==e.length)throw new c(a.MISC_ERROR,"sys.prune: 修剪块链数据");if(this.chain.options.spv)throw new c(a.MISC_ERROR,"Cannot prune chain in SPV mode.");if(this.chain.options.prune)throw new c(a.MISC_ERROR,"Chain is already pruned.");if(this.chain.height<this.network.block.pruneAfterHeight)throw new c(a.MISC_ERROR,"Chain is too short for pruning.");try{await this.chain.prune()}catch(e){throw new c(a.DATABASE_ERROR,e.message)}}async submitWork(e){var t=await this.locker.lock();try{return await this._submitWork(e)}finally{t()}}async _submitWork(e){var t=this.attempt;if(!t)return!1;if(128!==e.length)throw new c(a.INVALID_PARAMETER,"Invalid work size.");if(L(e=e.slice(0,80)),(e=O.fromHead(e)).prevBlock!==t.prevBlock||e.bits!==t.bits)return!1;if(!e.verify())return!1;if(!(i=this.merkleMap.get(e.merkleRoot)))return!1;var i,[i,r]=i,s=e.nonce;e=e.time;if(!(i=t.getProof(i,r,e,s)).verify(t.target))return!1;let n;r=t.commit(i);try{n=await this.chain.add(r)}catch(e){if("VerifyError"===e.type)return this.logger.warning("RPC block rejected: %s (%s).",r.rhash(),e.reason),!1;throw e}return!!n||(this.logger.warning("RPC block rejected: %s (bad-prevblk).",r.rhash()),!1)}async createWork(e){var t=await this.locker.lock();try{return await this._createWork(e)}finally{t()}}async _createWork(){var e=await this.updateWork(),t=this.nonce1,i=this.nonce2,r=e.time,s=Buffer.allocUnsafe(128);s.fill(0),t=e.getRoot(t,i);return e.getHeader(t,r,0).copy(s,0),s[80]=128,s.writeUInt32BE(640,s.length-4,!0),L(s),{data:s.toString("hex"),target:e.target.toString("hex"),height:e.height}}async getWorkLongpoll(e,t){if(t||0!=e.length)throw new c(a.MISC_ERROR,"sys.worklp");return await this.longpoll(),this.createWork()}async getWork(e,t){if(t||1<e.length)throw new c(a.MISC_ERROR,'sys.work ( "data" )');if(1!==e.length)return this.createWork();if(t=new x([e]).buf(0))return this.submitWork(t);throw new c(a.TYPE_ERROR,"Invalid work data.")}async submitBlock(e,t){if(t||e.length<1||2<e.length)throw new c(a.MISC_ERROR,'block.submit.admin "hexdata" ( "jsonparametersobject" ): 提交区块数据 区块数据HEX串');return t=new x([e]).buf(0),e=A.fromRaw(t),this.addBlock(e)}async getBlockTemplate(e,t){if(t||1<e.length)throw new c(a.MISC_ERROR,"block.template.admin ( \"{'mode':'template | proposal', 'data': '{hex}', 'rules':['segwit']}\" ): 生成或校验区块数据模板");if(t=new x([e]).obj(0,{}),"template"!==(t=(e=new x([t])).str("mode","template"))&&"proposal"!==t)throw new c(a.INVALID_PARAMETER,"Invalid mode.");if("proposal"===t){if(!(t=e.buf("data")))throw new c(a.TYPE_ERROR,"Missing data parameter.");if((t=A.fromRaw(t)).prevBlock!==this.chain.tip.hash)return"inconclusive-not-best-prevblk";try{await this.chain.verifyBlock(t)}catch(e){if("VerifyError"===e.type)return e.reason;throw e}return null}let i=e.u32("maxversion",-1),r=e.array("rules");r&&(i=-1);let s=!1;if(t=e.array("capabilities")){let e=!1,i=!1;for(const r of t){if("string"!=typeof r)throw new c(a.TYPE_ERROR,"Invalid capability.");switch(r){case"coinbasetxn":e=!0;break;case"coinbasevalue":i=!0}}if(i=!0,e&&!i){if(0===this.miner.addresses.length)throw new c(a.MISC_ERROR,"No addresses available for coinbase.");s=!0}}return(t=e.str("longpollid"))&&await this.handleLongpoll(t),r=r||[],this.createTemplate(i,s,r)}async createTemplate(e,t,i){var r=await this.locker.lock();try{return await this._createTemplate(e,t,i)}finally{r()}}async _createTemplate(e,t,i){var r=await this.getTemplate(),n=r.witness?1:u.WITNESS_SCALE_FACTOR,o=["time","transactions","prevblock"],h=(2<=e&&o.push("version/force"),t&&(o.push("coinbase"),o.push("coinbase/append"),o.push("generation")),new Map);for(let e=0;e<r.items.length;e++){var l=r.items[e];h.set(l.hash,e+1)}var p=[];for(let e=0;e<r.items.length;e++){var d=r.items[e],m=d.tx,g=[];for(let t=0;t<m.inputs.length;t++){var y=m.inputs[t];null!=(y=h.get(y.prevout.hash))&&-1===g.indexOf(y)&&(s(y<e+1),g.push(y))}p.push({data:m.toRaw().toString("hex"),txid:m.txid(),hash:m.wtxid(),depends:g,fee:d.fee,sigops:d.sigops/n|0,weight:m.getWeight()})}let v=r.version;var b={},k=[];for(const e of this.network.deploys){var x=await this.chain.getState(this.chain.tip,e);let t=e.name;switch(x){case f.thresholdStates.DEFINED:case f.thresholdStates.FAILED:break;case f.thresholdStates.LOCKED_IN:v|=1<<e.bit;case f.thresholdStates.STARTED:e.force||(-1===i.indexOf(t)&&(v&=~(1<<e.bit)),e.required&&(t="!"+t)),b[t]=e.bit;break;case f.thresholdStates.ACTIVE:if(!e.force&&e.required){if(-1===i.indexOf(t))throw new c(a.INVALID_PARAMETER,`Client must support ${t}.`);t="!"+t}k.push(t);break;default:s(!1,"Bad state.")}}var S;e={capabilities:["proposal"],mutable:o,version:v>>>=0,rules:k,vbavailable:b,vbrequired:0,height:r.height,previousblockhash:w.revHex(r.prevBlock),target:w.revHex(r.target.toString("hex")),bits:w.hex32(r.bits),noncerange:"00000000ffffffff",curtime:r.time,mintime:r.mtp+1,maxtime:r.time+7200,expires:r.time+7200,sigoplimit:u.MAX_BLOCK_SIGOPS_COST/n|0,sizelimit:u.MAX_BLOCK_SIZE,weightlimit:void 0,longpollid:this.chain.tip.rhash()+w.pad32(this.totalTX()),submitold:!1,coinbaseaux:{flags:r.coinbaseFlags.toString("hex")},coinbasevalue:void 0,coinbasetxn:void 0,default_witness_commitment:void 0,transactions:p};return r.witness&&(e.sizelimit=u.MAX_RAW_BLOCK_SIZE,e.weightlimit=u.MAX_BLOCK_WEIGHT),t?((t=(o=r.toCoinbase()).inputs[0]).script.pop(),t.script.compile(),r.witness&&(S=o.outputs.pop(),s(S.script.isCommitment()),t.witness.clear()),o.refresh(),e.coinbasetxn={data:o.toRaw().toString("hex"),txid:o.txid(),hash:o.wtxid(),depends:[],fee:0,sigops:o.getSigopsCost()/n|0,weight:o.getWeight()}):e.coinbasevalue=r.getReward(),-1!==i.indexOf("segwit")&&(e.default_witness_commitment=r.getWitnessScript().toJSON()),e}async getMiningInfo(e,t){if(t||0!==e.length)throw new c(a.MISC_ERROR,"sys.mininginfo");let i=0,r=0,s=0,n=0;if(t=this.attempt){r=t.weight,s=t.items.length+1,n=t.getDifficulty(),i=1e3;for(const e of t.items)i+=e.tx.getBaseSize()}return{blocks:this.chain.height,currentblocksize:i,currentblockweight:r,currentblocktx:s,difficulty:n,errors:"",genproclimit:this.procLimit,networkhashps:await this.getHashRate(120),pooledtx:this.totalTX(),testnet:"main"!=this.network.type,chain:"testnet"!==this.network.type?this.network.type:"test",generate:this.node.config.mining}}async getNetworkHashPS(e,t){if(t||2<e.length)throw new c(a.MISC_ERROR,"getnetworkhashps ( blocks height )");return e=(t=new x([e])).u32(0,120),t=t.u32(1),this.getHashRate(e,t)}async prioritiseTransaction(e,t){if(t||3!==e.length)throw new c(a.MISC_ERROR,"tx.prioritise <txid> <priority delta> <fee delta>");e=(t=new x([e])).hash(0);var i=t.i64(1);t=t.i64(2);if(!this.mempool)throw new c(a.MISC_ERROR,"No mempool available.");if(!e)throw new c(a.TYPE_ERROR,"Invalid TXID");if(null==i||null==t)throw new c(a.TYPE_ERROR,"Invalid fee or priority.");if(e=this.mempool.getEntry(e))return this.mempool.prioritise(e,i,t),!0;throw new c(a.MISC_ERROR,"Transaction not in mempool.")}async verifyBlock(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,'block.verify "hexdata": 校验区块数据 区块数据HEX串');if(!(t=new x([e]).buf(0)))throw new c(a.TYPE_ERROR,"Invalid block hex.");if(this.chain.options.spv)throw new c(a.MISC_ERROR,"Cannot verify block in SPV mode.");e=A.fromRaw(t);try{await this.chain.verifyBlock(e)}catch(e){if("VerifyError"===e.type)return e.reason;throw e}return null}async getGenerate(e,t){if(t||0!==e.length)throw new c(a.MISC_ERROR,"miner.check: 检查矿机运行状态");return{mode:this.node.config.mining}}async setSync(e,t){this.chain.synced=!0,this.chain.emit("chain.full")}async getMinerAddr(e,t){if(t)throw new c(a.MISC_ERROR,"miner.getaddr.admin: 查询矿机奖励地址");return{address:this.node.config.coinbaseAddress}}async addMinerAddr(e,t){if(t)throw new c(a.MISC_ERROR,"miner.addaddr.admin address: 添加矿机奖励地址");(t=new x([e]).str(0,""))&&(this.miner.addresses.unshift(t),this.node.config.coinbaseAddress=this.miner.addresses.reduce((e,t)=>e+(e?","+t:t),""),await this.node.config.saveAsync())}async setMinerAddr(e,t){if(t)throw new c(a.MISC_ERROR,"miner.setaddr.admin addresses: 设置矿机奖励地址");let i=new x([e]).array(0,"");if(!i||0==i.length){if((t=await this.execute({method:"address.index",params:[1]})).error)throw new c(a.MISC_ERROR,"获取默认矿机奖励地址出错");i=[t.result]}this.miner.addresses=i.reduce((e,t)=>(e.push(new R(t)),e),[]),this.node.config.coinbaseAddress=i.reduce((e,t)=>e+(e?","+t:t),""),await this.node.config.saveAsync()}async remoteNotifySet(e,t){if(t)throw new c(a.MISC_ERROR,"remoteNotify.set url secret: 设置远程消息推送 远程地址 签名密钥");return e.length<1||(e=(t=new x([e])).str(0,""),t=t.str(1,""),this.node.config.remotenotify=e,this.node.config.remotesecret=t,this.node.config.saveAsync()),{url:this.node.config.remotenotify,secret:this.node.config.remotesecret}}async setGenerate(e,t){if(t||e.length<1||2<e.length)throw new c(a.MISC_ERROR,"miner.set.admin mine ( proclimit ): 设置矿机运行状态 [true|false] (进程数限制)");if(e=(t=new x([e])).bool(0,!1),t=t.u32(1,0),e&&0===this.miner.addresses.length)throw new c(a.MISC_ERROR,"No addresses available for coinbase.");return this.node.config.mining!=e&&(this.node.config.mining=e,this.node.config.saveAsync(),this.procLimit=t,e?this.miner.cpu.start():this.miner.cpu.stop()),{mode:this.node.config.mining}}async generate(e,t){if(this.node.spv)throw new c(a.INVALID_REQUEST,"spv node can not generate new block");if(this.node.config.mining)throw new c(a.INVALID_REQUEST,"Miner is auto-running.");if(t||e.length<1||2<e.length)throw new c(a.MISC_ERROR,"miner.generate.admin numblocks ( passphrase tip ): 生产区块 生产数量 ( 解锁密码 区块头 )");e=(t=new x([e])).u32(0,1);var i=t.str(1);t=t.hash(2);if(0===this.miner.addresses.length)throw new c(a.MISC_ERROR,"No addresses available for coinbase.");return this.mineBlocks(e,null,i,t)}async generateToAddress(e,t){if(this.node.spv)throw new c(a.INVALID_REQUEST,"spv node can not generate new block");if(this.node.config.mining)throw new c(a.INVALID_REQUEST,"Miner is auto-running.");if(t||2!==e.length)throw new c(a.MISC_ERROR,"miner.generateto.admin numblocks address: 生产区块并发奖至 生产数量 奖励地址 ( 解锁密码 区块头 )");e=(t=new x([e])).u32(0,1);var i=t.str(1,""),r=t.str(2);t=t.str(3),i=n.parseAddress(i,this.network);return this.mineBlocks(e,i,r,t)}async createRawTransaction(e,t){if(t||e.length<2||3<e.length)throw new c(a.MISC_ERROR,'tx.raw.create [{"txid":"id","vout":n},...] {"address":amount,"data":"hex",...} ( locktime ): 创建交易,返回交易原始数据');const i=new x([e]);t=i.array(0),e=i.obj(1);var r=i.u32(2);if(!t||!e)throw new c(a.TYPE_ERROR,"Invalid parameters (inputs and sendTo).");var s=new j;null!=r&&(s.locktime=r);for(const e of t){const t=new x([e]);var o=t.hash("txid"),h=t.u32("vout");let i=t.u32("sequence",4294967295);if(s.locktime&&i--,!o||null==h)throw new c(a.TYPE_ERROR,"Invalid outpoint.");var u=new C;u.prevout.hash=o,u.prevout.index=h,u.sequence=i,s.inputs.push(u)}var l=new x([e]),p=new Set;for(const t of Object.keys(e))if("data"===t){var d=l.buf(t);if(!d)throw new c(a.TYPE_ERROR,"Invalid nulldata..");var f=new T;f.value=0,f.script.createScript(d,y.types.NULLDATA),s.outputs.push(f)}else{if(f=(d=n.parseAddress(t,this.network)).toString(this.network),p.has(f))throw new c(a.INVALID_PARAMETER,"Duplicate address");p.add(f);var m=l.ufixed(t,8);if(null==m)throw new c(a.TYPE_ERROR,"Invalid output value.");var g=new T;g.value=m,g.script.fromAddress(d),s.outputs.push(g)}return s.toRaw().toString("hex")}async decodeRawTransaction(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,'tx.raw.decode "hexstring": 解码交易原始数据 交易原始数据');if(s(!this.chain.options.spv,"Cannot get TX in SPV mode."),!(t=new x([e]).buf(0)))throw new c(a.TYPE_ERROR,"Invalid hex string.");e=B.fromRaw(t);if((t=await this.node.getMeta(e.hash("hex")))&&t.block)return t=await this.chain.getEntry(t.block),this.txToJSON(e,t);throw new c(a.TYPE_ERROR,"TX Not Confirmed.")}async decodeScript(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,'sys.decodescript "hex"');t=new x([e]).buf(0);let i=new y;return t&&(i=y.fromRaw(t)),e=R.fromScripthash(i.hash160()),(t=this.scriptToJSON(i)).p2sh=e.toString(this.network),t}async getRawTransaction(e,t){if(t||e.length<1||2<e.length)throw new c(a.MISC_ERROR,'tx.raw.get "txid" ( verbose ): 获取交易原始数据 交易哈希小端 [展开为对象]');if(s(!this.chain.options.spv,"Cannot get TX in SPV mode."),e=(t=new x([e])).hash(0),t=t.bool(1,!1),!e)throw new c(a.TYPE_ERROR,"Invalid TXID.");if(!(e=await this.node.getMeta(e)))throw new c(a.MISC_ERROR,"Transaction not found.");var i=e.tx;if(!t)return i.toRaw().toString("hex");let r;return e.block&&(r=await this.chain.getEntry(e.block)),(t=this.txToJSON(i,r)).time=e.mtime,t.hex=i.toRaw().toString("hex"),t}async getTXByHash(e,t){if(t||1!=e.length)throw new c(a.MISC_ERROR,'tx.get "txid": 查询交易记录 交易哈希小端');t=new x([e]).hash(0);return(e=(s(t,"Hash is required."),s(!this.chain.options.spv,"Cannot get TX in SPV mode."),await this.node.getMeta(t)))?(t=await this.node.getMetaView(e),e.getJSON(this.network,t,this.chain.height)):null}async sendRawTransaction(e,t){if(t||e.length<1||2<e.length)throw new c(a.MISC_ERROR,'tx.raw.send "hexstring" ( allowhighfees ): 发送交易数据返回交易ID 交易原始数据 [允许高手续费]');if(t=new x([e]).buf(0))return e=B.fromRaw(t),await this.node.relay(e),e.txid();throw new c(a.TYPE_ERROR,"Invalid hex string.")}async signRawTransaction(e,t){if(t||e.length<1||4<e.length)throw new c(a.MISC_ERROR,'tx.raw.sign.admin "hexstring" ( [{"txid":"id","vout":n,"scriptPubKey":"hex",redeemScript":"hex"},...] ["privatekey1",...] sighashtype ): 签署交易');const i=new x([e]);t=i.buf(0),e=i.array(1);var r=i.array(2),s=i.str(3);if(!t)throw new c(a.TYPE_ERROR,"Invalid hex string.");if(!this.mempool)throw new c(a.MISC_ERROR,"No mempool available.");var o=j.fromRaw(t),h=(o.view=await this.mempool.getSpentView(o),new Map),u=[];if(r){const e=new x([r]);for(let t=0;t<r.length;t++){var l=e.str(t,"");l=n.parseSecret(l,this.network);h.set(l.getPublicKey("hex"),l),u.push(l)}}if(e)for(const t of e){const e=new x([t]);var p=e.hash("txid"),d=e.u32("vout"),f=e.buf("scriptPubKey"),m=e.ufixed("amount",8),g=e.buf("redeemScript");if(!p||null==d||!f||null==m)throw new c(a.INVALID_PARAMETER,"Invalid UTXO.");p=new P(p,d);var v=y.fromRaw(f);d=T.fromScript(v,m);if(o.view.addOutput(p,d),0!==u.length&&g&&(v.isScripthash()||v.isWitnessScripthash())){if(!g)throw new c(a.INVALID_PARAMETER,"P2SH requires redeem script.");var b=y.fromRaw(g);for(const e of b.code)if(e.data){var w=h.get(e.data.toString("hex"));if(w){w.script=b,w.witness=v.isWitnessScripthash(),w.refresh();break}}}}let k=y.hashType.ALL;if(s){if((t=s.split("|")).length<1||2<t.length)throw new c(a.INVALID_PARAMETER,"Invalid sighash type.");if(null==(k=y.hashType[t[0]]))throw new c(a.INVALID_PARAMETER,"Invalid sighash type.");if(2===t.length){if("ANYONECANPAY"!==t[1])throw new c(a.INVALID_PARAMETER,"Invalid sighash type.");k|=y.hashType.ANYONECANPAY}}return await o.signAsync(u,k,this.workers),{hex:o.toRaw().toString("hex"),complete:o.isSigned()}}async createMultisig(e,t){if(t||e.length<2||2<e.length)throw new c(a.MISC_ERROR,'sys.createmultisig nrequired ["key",...]');e=(t=new x([e])).u32(0,0);var i=t.array(1,[]);t=i.length;if(e<1||t<e||16<t)throw new c(a.INVALID_PARAMETER,"Invalid m and n values.");var r=new x([i]);for(let e=0;e<i.length;e++){var s=r.buf(e);if(!s)throw new c(a.TYPE_ERROR,"Invalid key.");if(!d.publicKeyVerify(s))throw new c(a.INVALID_ADDRESS_OR_KEY,"Invalid key.");i[e]=s}if((e=y.createScript([e,t,i],y.types.MULTISIG)).getSize()>u.MAX_SCRIPT_PUSH)throw new c(a.VERIFY_ERROR,"Redeem script exceeds size limit.");return{address:R.fromScript(e).toString(this.network),redeemScript:e.toJSON()}}async createWitnessAddress(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,'sys.createwitnessaddress "script"');if(t=new x([e]).buf(0))return e=y.fromRaw(t).forWitness(),{address:R.fromScript(e).toString(this.network),witnessScript:e.toJSON()};throw new c(a.TYPE_ERROR,"Invalid script hex.")}async validateAddress(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,'sys.validateaddress "address"');let i;t=new x([e]).str(0,"");try{i=R.fromString(t,this.network)}catch(e){return{isvalid:!1}}return e=y.fromAddress(i),{isvalid:!0,address:i.toString(this.network),scriptPubKey:e.toJSON(),ismine:!1,iswatchonly:!1}}async verifyMessage(e,t){if(t||3!==e.length)throw new c(a.MISC_ERROR,'sys.message.verify "bitcoinaddress" "signature" "message"');e=(t=new x([e])).str(0,"");var i,r,s,h=t.buf(1,null,"base64");t=t.str(2);if(h&&t)return e=n.parseAddress(e,this.network),t=Buffer.from(o+t,"utf8"),t=l.hash256(t),i=d.recover(t,h,0,!0),r=d.recover(t,h,1,!0),s=d.recover(t,h,2,!0),t=d.recover(t,h,3,!0),!!i&&p(l.hash160(i),e.hash)||!!r&&p(l.hash160(r),e.hash)||!!s&&p(l.hash160(s),e.hash)||!!t&&p(l.hash160(t),e.hash);throw new c(a.TYPE_ERROR,"Invalid parameters.")}async signMessageWithPrivkey(e,t){if(t||2!==e.length)throw new c(a.MISC_ERROR,'sys.message.signk "privkey" "message"');return e=(t=new x([e])).str(0,""),t=t.str(1,""),e=n.parseSecret(e,this.network),t=Buffer.from(o+t,"utf8"),t=l.hash256(t),e.sign(t).toString("base64")}async estimateFee(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,"estimate.fee nblocks");if(t=new x([e]).u32(0,1),this.fees)return 0===(e=this.fees.estimateFee(t,!1))?-1:g.btc(e,!0);throw new c(a.MISC_ERROR,"Fee estimation not available.")}async estimatePriority(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,"estimate.priority nblocks");if(t=new x([e]).u32(0,1),this.fees)return this.fees.estimatePriority(t,!1);throw new c(a.MISC_ERROR,"Priority estimation not available.")}async estimateSmartFee(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,"estimate.smartfee nblocks");if(t=new x([e]).u32(0,1),!this.fees)throw new c(a.MISC_ERROR,"Fee estimation not available.");let i=this.fees.estimateFee(t,!0);return{fee:i=0===i?-1:g.btc(i,!0),blocks:t}}async estimateSmartPriority(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,"estimate.smartpriority nblocks");if(t=new x([e]).u32(0,1),this.fees)return{priority:this.fees.estimatePriority(t,!0),blocks:t};throw new c(a.MISC_ERROR,"Priority estimation not available.")}async invalidateBlock(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,'block.invalidate.admin "hash": 标记区块为无效 区块哈希(小端)');if(t=new x([e]).hash(0))return await this.chain.invalidate(t),null;throw new c(a.TYPE_ERROR,"Invalid block hash.")}async reconsiderBlock(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,'block.reconsider.admin "hash": 取消区块无效标记 区块哈希(小端)');if(t=new x([e]).hash(0))return this.chain.removeInvalid(t),null;throw new c(a.TYPE_ERROR,"Invalid block hash.")}async setMockTime(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,"sys.setmocktime timestamp");if(null==(t=new x([e]).u32(0)))throw new c(a.TYPE_ERROR,"Invalid timestamp.");return this.network.time.offset=0,e=this.network.now()-t,this.network.time.offset=-e,null}async getMemoryInfo(e,t){if(t||0!==e.length)throw new c(a.MISC_ERROR,"sys.memoryinfo");return w.memoryUsage()}async setLogLevel(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,'sys.setloglevel "level"');return t=new x([e]).str(0,""),this.logger.setLevel(t),null}async handleLongpoll(e){if(74!==e.length)throw new c(a.INVALID_PARAMETER,"Invalid longpoll ID.");var t=e.slice(0,64);e=parseInt(e.slice(64,74),10);if(!w.isHex(t)||!w.isU32(e))throw new c(a.INVALID_PARAMETER,"Invalid longpoll ID.");e=w.revHex(t),this.chain.tip.hash===e&&await this.longpoll()}longpoll(){return new Promise((e,t)=>{this.pollers.push(b.job(e,t))})}refreshBlock(){var e=this.pollers;this.attempt=null,this.lastActivity=0,this.merkleMap.clear(),this.nonce1=0,this.nonce2=0,this.pollers=[];for(const t of e)t.resolve()}bindChain(){this.boundChain||(this.boundChain=!0,this.node.on("block.connect",()=>{this.attempt&&this.refreshBlock()}),this.mempool&&this.node.on("tx",()=>{this.attempt&&10<w.now()-this.lastActivity&&this.refreshBlock()}))}async getTemplate(){this.bindChain();let e=this.attempt;return e?this.miner.updateTime(e):(e=await this.miner.createBlock(),this.attempt=e,this.lastActivity=w.now()),e}async updateWork(){this.bindChain();let e=this.attempt;if(e){if(e.address.isNull())throw new c(a.MISC_ERROR,"No addresses available for coinbase.");this.miner.updateTime(e),4294967296==++this.nonce2&&(this.nonce2=0,this.nonce1++);const t=this.nonce1,i=this.nonce2,r=e.getRoot(t,i).toString("hex");this.merkleMap.set(r,[t,i])}else{if(0===this.miner.addresses.length)throw new c(a.MISC_ERROR,"No addresses available for coinbase.");e=await this.miner.createBlock();const t=this.nonce1,i=this.nonce2,r=e.getRoot(t,i).toString("hex");this.attempt=e,this.lastActivity=w.now(),this.merkleMap.set(r,[t,i])}return e}async addBlock(e){var t=await this.locker.lock(),i=await this.chain.locker.lock();try{return await this._addBlock(e)}finally{i(),t()}}async _addBlock(e){this.logger.info("Handling submitted block: %s.",e.rhash());var t,i=await this.chain.getEntry(e.prevBlock);let r;i&&(await this.chain.getDeployments(e.time,i)).hasWitness()&&e.getCommitmentHash()&&(t=(i=e.txs[0]).inputs[0],i.hasWitness()||(this.logger.warning("Submitted block had no witness nonce."),this.logger.debug(i),t.witness.push(k.ZERO_HASH),t.witness.compile(),i.refresh(),e.refresh()));try{r=await this.chain._add(e)}catch(t){if("VerifyError"===t.type)return this.logger.warning("RPC block rejected: %s (%s).",e.rhash(),t.reason),"rejected: "+t.reason;throw t}return r?null:(this.logger.warning("RPC block rejected: %s (bad-prevblk).",e.rhash()),"rejected: bad-prevblk")}totalTX(){return this.mempool?this.mempool.map.size:0}getSoftforks(){return[{id:"bip66",version:3,reject:{status:this.chain.state.hasBIP66()}}]}async getBIP9Softforks(){var e=this.chain.tip,t={};for(const r of this.network.deploys){var i=await this.chain.getState(e,r);i=f.thresholdStates.getName(i);t[r.name]={status:i,bit:r.bit,startTime:r.startTime,timeout:r.timeout}}return t}async getTXByAddress(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,"tx.list.address address: 按地址查询交易列表");t=new x([e]).str(0);var i=(s(t,"Address is required."),s(!this.chain.options.spv,"Cannot get TX in SPV mode."),[]);for(const e of await this.node.getMetaByAddress(t)){var r=await this.node.getMetaView(e);i.push(e.getJSON(this.network,r,this.chain.height))}return i}async getVpByAddress(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,"prop.byaddress address: 按地址查询道具列表");return t=new x([e]).str(0),s(t,"Address is required."),s(!this.chain.options.spv,"Cannot get TX in SPV mode."),this.chain.db.getVpByAddress(t)}async getVpByHashIndex(e,t){if(t||2!==e.length)throw new c(a.MISC_ERROR,"prop.byHashIndex hash index: 按输出查询道具");var i,r;e=(t=new x([e])).hash(0),t=t.int(1,0);return(e=(s(e,"Hash is required."),await this.node.getMeta(e)))&&(i=e.tx.outputs[t])?((r=i.getReturnData([N.OP_PROPCREATE,N.OP_PROPEXCHANGE]))&&(r.current={hash:e.tx.rhash("hex"),index:t,address:i.getAddress().toString()}),r):null}async getVpById(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,"prop.byid pid: 按道具编码查询道具列表");t=new x([e]).str(0);var i=[];if(e=await this.chain.db.propList.getVp(t)){t=e.current.hash,e.oper="exchange",i.push({oper:"exchange",pid:e.pid,oid:e.oid,cid:e.cid,pst:e.pst,current:{hash:w.revHex(e.current.hash),index:e.current.index,address:e.current.address},height:e.height});let a=[t,e.current.index];for(;;){var r=await this.node.getMeta(a[0]),s=r.tx;if(!s)break;if(!(s=s.outputs[a[1]]))break;var n=s.getReturnData([N.OP_PROPCREATE,N.OP_PROPEXCHANGE]);if(!n)break;if("new"==n.oper){n.current={hash:w.revHex(a[0]),index:a[1],address:s.getAddress().toString()},n.height=r.height,i.push(n);break}if(!n.prev)break;a=[n.prev.hash,n.prev.index]}}return i}async getTXByAddresses(e,t){if(t||1!==e.length)throw new c(a.MISC_ERROR,"tx.list.addresses addresses: 按地址集合查询交易列表");t=new x([e]).array(0);var i=(s(t,"Address is required."),s(!this.chain.options.spv,"Cannot get TX in SPV mode."),[]);for(const e of await this.node.getMetaByAddress(t)){var r=await this.node.getMetaView(e);i.push(e.getJSON(this.network,r,this.chain.height))}return i}async getHashRate(e,t){let i=this.chain.tip;if(!(i=null!=t?await this.chain.getEntry(t):i))return 0;s("number"==typeof e),s(0<=e),(e=0===e?i.height%this.network.pow.retargetInterval+1:e)>i.height&&(e=i.height);let r=i.time,n=r,o=i;for(let t=0;t<e;t++){if(!(o=await this.chain.getPrevious(o)))throw new c(a.DATABASE_ERROR,"Not found.");r=Math.min(o.time,r),n=Math.max(o.time,n)}var h;return 0==(t=n-r)?0:(h=i.chainwork.sub(o.chainwork),Number(h.toString())/t)}async mineBlocks(e,t,i,r){var s=await this.locker.lock();try{return await this._mineBlocks(e,t,i,r)}finally{s()}}async _mineBlocks(e,t,i,r){var n=[];for(let o=0;o<e;o++){var a=await this.miner.mineBlock(r,t,i);a=await this.chain.add(a);s(a),n.push(a.rhash())}return n}async findFork(e){for(;e;){if(await this.chain.isMainChain(e))return e;e=await this.chain.getPrevious(e)}throw new Error("Fork not found.")}txToJSON(e,t){var i;let r=0,s=null,n=0;t&&(i=t.height,r=t.time,s=t.rhash(),n=this.chain.height-i+1);var a=[];for(const t of e.inputs){var o={coinbase:void 0,txid:void 0,scriptSig:void 0,txinwitness:void 0,sequence:t.sequence};e.isCoinbase()?o.coinbase=t.script.toJSON():(o.txid=t.prevout.txid(),o.vout=t.prevout.index,o.scriptSig={asm:t.script.toASM(),hex:t.script.toJSON()}),0<t.witness.items.length&&(o.txinwitness=t.witness.items.map(e=>e.toString("hex"))),a.push(o)}var c=[];for(let t=0;t<e.outputs.length;t++){var h=e.outputs[t];c.push({value:g.btc(h.value,!0),n:t,scriptPubKey:this.scriptToJSON(h.script,!0)})}return{txid:e.txid(),hash:e.wtxid(),size:e.getSize(),vsize:e.getVirtualSize(),version:e.version,locktime:e.locktime,vin:a,vout:c,blockhash:s,confirmations:n,time:r,blocktime:r,hex:void 0}}scriptToJSON(e,t){var i=e.getType(),[t]=(i={asm:e.toASM(),hex:void 0,type:y.typesByVal[i],reqSigs:1,addresses:[],p2sh:void 0},t&&(i.hex=e.toJSON()),e.getMultisig());return(t=(-1!==t&&(i.reqSigs=t),R.fromScript(e)))&&(e=t.toString(this.network),i.addresses.push(e)),i}async headerToJSON(e){var t=await this.chain.getMedianTime(e),i=await this.chain.getNextHash(e.hash);return{hash:e.rhash(),confirmations:this.chain.height-e.height+1,height:e.height,version:e.version,versionHex:w.hex32(e.version),merkleroot:w.revHex(e.merkleRoot),time:e.time,mediantime:t,bits:e.bits,difficulty:m.bits2Diff(e.bits),chainwork:e.chainwork.toString("hex",64),previousblockhash:e.prevBlock!==k.NULL_HASH?w.revHex(e.prevBlock):null,nextblockhash:i?w.revHex(i):null}}async blockToJSON(e,t,i){var r,s=await this.chain.getMedianTime(e),n=await this.chain.getNextHash(e.hash),a=[];for(const s of t.txs)i?(r=this.txToJSON(s,e),a.push(r)):a.push(s.txid());return{hash:e.rhash(),confirmations:this.chain.height-e.height+1,strippedsize:t.getBaseSize(),size:t.getSize(),weight:t.getWeight(),height:e.height,version:e.version,versionHex:w.hex32(e.version),merkleroot:w.revHex(e.merkleRoot),coinbase:t.txs[0].inputs[0].script.toJSON(),tx:a,time:e.time,mediantime:s,bits:e.bits,nonce:t.nonce,difficulty:m.bits2Diff(e.bits),chainwork:e.chainwork.toString("hex",64),previousblockhash:e.prevBlock!==k.NULL_HASH?w.revHex(e.prevBlock):null,nextblockhash:n?w.revHex(n):null}}entryToJSON(e){return{size:e.size,fee:g.btc(e.deltaFee,!0),modifiedfee:0,time:e.time,height:e.height,startingpriority:e.priority,currentpriority:e.getPriority(this.chain.height),descendantcount:this.mempool.countDescendants(e),descendantsize:e.descSize,descendantfees:e.descFee,ancestorcount:this.mempool.countAncestors(e),ancestorsize:0,ancestorfees:0,depends:this.mempool.getDepends(e.tx).map(w.revHex)}}}},function(e,t,i){"use strict";
|
|
944
944
|
/*!
|
|
945
945
|
* fees.js - fee estimation for vallnet
|
|
946
946
|
* Copyright (c) 2019-2022, Bookman Software (MIT License).
|