From 0f4767f1b6291de9b7d1c4f4612c945f05be87a6 Mon Sep 17 00:00:00 2001 From: Nodari Chkuaselidze Date: Wed, 18 Oct 2023 17:04:01 +0400 Subject: [PATCH] covenant: set methods on covenant. --- lib/covenants/rules.js | 4 +- lib/mempool/mempoolentry.js | 2 +- lib/mining/template.js | 15 +- lib/primitives/address.js | 3 +- lib/primitives/claim.js | 16 +-- lib/primitives/coin.js | 2 +- lib/primitives/covenant.js | 248 +++++++++++++++++++++++++++++++++- lib/primitives/outpoint.js | 2 +- lib/primitives/output.js | 2 +- lib/primitives/tx.js | 4 +- lib/types.js | 10 +- lib/wallet/wallet.js | 113 ++++++---------- test/anyone-can-renew-test.js | 51 ++++--- test/contractstate-test.js | 21 +-- test/interactive-swap-test.js | 17 +-- test/mempool-test.js | 17 +-- test/util/memwallet.js | 73 +++------- test/wallet-http-test.js | 5 +- test/wallet-test.js | 27 ++-- 19 files changed, 399 insertions(+), 233 deletions(-) diff --git a/lib/covenants/rules.js b/lib/covenants/rules.js index dc0c7a719..fc4bcf947 100644 --- a/lib/covenants/rules.js +++ b/lib/covenants/rules.js @@ -144,7 +144,7 @@ rules.MAX_COVENANT_SIZE = (0 /** * Hash a domain name. * @param {String|Buffer} name - * @returns {Buffer} + * @returns {Hash} */ rules.hashName = function hashName(name) { @@ -156,7 +156,7 @@ rules.hashName = function hashName(name) { /** * Hash a domain name. * @param {String} name - * @returns {Buffer} + * @returns {Hash} */ rules.hashString = function hashString(name) { diff --git a/lib/mempool/mempoolentry.js b/lib/mempool/mempoolentry.js index 6749543b9..e6b102e8d 100644 --- a/lib/mempool/mempoolentry.js +++ b/lib/mempool/mempoolentry.js @@ -142,7 +142,7 @@ class MempoolEntry extends bio.Struct { /** * Calculate reverse transaction hash. - * @returns {Hash} + * @returns {HexHash} */ txid() { diff --git a/lib/mining/template.js b/lib/mining/template.js index 57bd8033d..9540ee486 100644 --- a/lib/mining/template.js +++ b/lib/mining/template.js @@ -294,13 +294,14 @@ class BlockTemplate { output.value = claim.value - claim.fee; output.address = claim.address; - output.covenant.type = rules.types.CLAIM; - output.covenant.pushHash(claim.nameHash); - output.covenant.pushU32(this.height); - output.covenant.push(claim.name); - output.covenant.pushU8(flags); - output.covenant.pushHash(claim.commitHash); - output.covenant.pushU32(claim.commitHeight); + output.covenant.setClaim( + claim.nameHash, + this.height, + claim.name, + flags, + claim.commitHash, + claim.commitHeight + ); cb.outputs.push(output); } diff --git a/lib/primitives/address.js b/lib/primitives/address.js index d4c72bb21..7b6b83370 100644 --- a/lib/primitives/address.js +++ b/lib/primitives/address.js @@ -541,9 +541,8 @@ class Address extends bio.Struct { if (!data) throw new Error('Object is not an address.'); - if (Buffer.isBuffer(data)) { + if (Buffer.isBuffer(data)) return data; - } if (data instanceof Address) return data.hash; diff --git a/lib/primitives/claim.js b/lib/primitives/claim.js index a1b7a1e1e..c18a84da6 100644 --- a/lib/primitives/claim.js +++ b/lib/primitives/claim.js @@ -17,7 +17,6 @@ const InvItem = require('./invitem'); const TX = require('./tx'); const Input = require('./input'); const Output = require('./output'); -const {types} = rules; const {OwnershipProof} = Ownership; /* @@ -170,13 +169,14 @@ class Claim extends bio.Struct { if (data.weak) flags |= 1; - output.covenant.type = types.CLAIM; - output.covenant.pushHash(rules.hashName(data.name)); - output.covenant.pushU32(height); - output.covenant.pushString(data.name); - output.covenant.pushU8(flags); - output.covenant.pushHash(data.commitHash); - output.covenant.pushU32(data.commitHeight); + output.covenant.setClaim( + rules.hashName(data.name), + height, + Buffer.from(data.name, 'binary'), + flags, + data.commitHash, + data.commitHeight, + ); tx.inputs.push(input); tx.outputs.push(output); diff --git a/lib/primitives/coin.js b/lib/primitives/coin.js index 3816cc90a..59a37d300 100644 --- a/lib/primitives/coin.js +++ b/lib/primitives/coin.js @@ -173,7 +173,7 @@ class Coin extends Output { /** * Get little-endian hash. - * @returns {Hash} + * @returns {HexHash?} */ txid() { diff --git a/lib/primitives/covenant.js b/lib/primitives/covenant.js index f2deac8f1..dc2d509b1 100644 --- a/lib/primitives/covenant.js +++ b/lib/primitives/covenant.js @@ -41,7 +41,9 @@ class Covenant extends bio.Struct { /** * Inject properties from options object. * @private - * @param {Object} options + * @param {types} type + * @param {Buffer[]} items + * @returns {Covenant} */ fromOptions(type, items) { @@ -84,7 +86,7 @@ class Covenant extends bio.Struct { * Set an item. * @param {Number} index * @param {Buffer} item - * @returns {Buffer} + * @returns {Covenant} */ set(index, item) { @@ -102,6 +104,7 @@ class Covenant extends bio.Struct { /** * Push an item. * @param {Buffer} item + * @returns {Coventant} */ push(item) { @@ -125,6 +128,7 @@ class Covenant extends bio.Struct { /** * Push a uint8. * @param {Number} num + * @returns {Covenant} */ pushU8(num) { @@ -150,6 +154,7 @@ class Covenant extends bio.Struct { /** * Push a uint32. * @param {Number} num + * @returns {Covenant} */ pushU32(num) { @@ -163,7 +168,7 @@ class Covenant extends bio.Struct { /** * Get a hash. * @param {Number} index - * @returns {Buffer} + * @returns {Hash} */ getHash(index) { @@ -174,7 +179,8 @@ class Covenant extends bio.Struct { /** * Push a hash. - * @param {Buffer} hash + * @param {Hash} hash + * @returns {Covenant} */ pushHash(hash) { @@ -199,6 +205,7 @@ class Covenant extends bio.Struct { /** * Push a string. * @param {String} str + * @returns {Covenant} */ pushString(str) { @@ -334,6 +341,232 @@ class Covenant extends bio.Struct { return this.type === types.REVOKE; } + /** + * Build helpers + */ + + /** + * Set covenant to NONE. + * @returns {Covenant} + */ + + setNone() { + this.type = types.NONE; + this.items = []; + return this; + } + + /** + * Set covenant to OPEN. + * @param {Hash} nameHash + * @param {Buffer} rawName + * @returns {Covenant} + */ + + setOpen(nameHash, rawName) { + this.type = types.OPEN; + this.items = []; + this.pushHash(nameHash); + this.pushU32(0); + this.push(rawName); + return this; + } + + /** + * Set covenant to BID. + * @param {Hash} nameHash + * @param {Number} start + * @param {Buffer} rawName + * @param {Hash} blind + * @returns {Covenant} + */ + + setBid(nameHash, start, rawName, blind) { + this.type = types.BID; + this.items = []; + this.pushHash(nameHash); + this.pushU32(start); + this.push(rawName); + this.pushHash(blind); + + return this; + } + + /** + * Set covenant to REVEAL. + * @param {Hash} nameHash + * @param {Number} height + * @param {Hash} nonce + * @returns {Covenant} + */ + + setReveal(nameHash, height, nonce) { + this.type = types.REVEAL; + this.items = []; + this.pushHash(nameHash); + this.pushU32(height); + this.pushHash(nonce); + + return this; + } + + /** + * Set covenant to REDEEM. + * @param {Hash} nameHash + * @param {Number} height + * @returns {Covenant} + */ + + setRedeem(nameHash, height) { + this.type = types.REDEEM; + this.items = []; + this.pushHash(nameHash); + this.pushU32(height); + + return this; + } + + /** + * Set covenant to REGISTER. + * @param {Hash} nameHash + * @param {Number} height + * @param {Buffer} record + * @param {Hash} blockHash + * @returns {Covenant} + */ + + setRegister(nameHash, height, record, blockHash) { + this.type = types.REGISTER; + this.items = []; + this.pushHash(nameHash); + this.pushU32(height); + this.push(record); + this.pushHash(blockHash); + + return this; + } + + /** + * Set covenant to UPDATE. + * @param {Hash} nameHash + * @param {Number} height + * @param {Buffer} resource + * @returns {Covenant} + */ + + setUpdate(nameHash, height, resource) { + this.type = types.UPDATE; + this.items = []; + this.pushHash(nameHash); + this.pushU32(height); + this.push(resource); + + return this; + } + + /** + * Set covenant to RENEW. + * @param {Hash} nameHash + * @param {Number} height + * @param {Hash} blockHash + * @returns {Covenant} + */ + + setRenew(nameHash, height, blockHash) { + this.type = types.RENEW; + this.items = []; + this.pushHash(nameHash); + this.pushU32(height); + this.pushHash(blockHash); + + return this; + } + + /** + * Set covenant to TRANSFER. + * @param {Hash} nameHash + * @param {Number} height + * @param {Address} address + * @returns {Covenant} + */ + + setTransfer(nameHash, height, address) { + this.type = types.TRANSFER; + this.items = []; + this.pushHash(nameHash); + this.pushU32(height); + this.pushU8(address.version); + this.push(address.hash); + + return this; + } + + /** + * Set covenant to REVOKE. + * @param {Hash} nameHash + * @param {Number} height + * @param {Buffer} rawName + * @param {Number} flags + * @param {Number} claimed + * @param {Number} renewals + * @param {Hash} blockHash + * @returns {Covenant} + */ + + setFinalize(nameHash, height, rawName, flags, claimed, renewals, blockHash) { + this.type = types.FINALIZE; + this.items = []; + this.pushHash(nameHash); + this.pushU32(height); + this.push(rawName); + this.pushU8(flags); + this.pushU32(claimed); + this.pushU32(renewals); + this.pushHash(blockHash); + + return this; + } + + /** + * Set covenant to REVOKE. + * @param {Hash} nameHash + * @param {Number} height + * @returns {Covenant} + */ + + setRevoke(nameHash, height) { + this.type = types.REVOKE; + this.items = []; + this.pushHash(nameHash); + this.pushU32(height); + + return this; + } + + /** + * Set covenant to CLAIM. + * @param {Hash} nameHash + * @param {Number} height + * @param {Buffer} rawName + * @param {Number} flags + * @param {Hash} commitHash + * @param {Number} commitHeight + * @returns {Covenant} + */ + + setClaim(nameHash, height, rawName, flags, commitHash, commitHeight) { + this.type = types.CLAIM; + this.items = []; + this.pushHash(nameHash); + this.pushU32(height); + this.push(rawName); + this.pushU8(flags); + this.pushHash(commitHash); + this.pushU32(commitHeight); + + return this; + } + /** * Test whether the covenant is name-related. * @returns {Boolean} @@ -506,6 +739,7 @@ class Covenant extends bio.Struct { /** * Write covenant to a buffer writer. * @param {BufferWriter} bw + * @returns {BufferWriter} */ write(bw) { @@ -549,8 +783,8 @@ class Covenant extends bio.Struct { /** * Inject properties from json object. - * @private * @param {String} json + * @returns {Covenant} */ fromJSON(json) { @@ -570,8 +804,8 @@ class Covenant extends bio.Struct { /** * Inject properties from buffer reader. - * @private * @param {BufferReader} br + * @returns {Covenant} */ read(br) { @@ -590,8 +824,8 @@ class Covenant extends bio.Struct { /** * Inject items from string. - * @private * @param {String|String[]} items + * @returns {Covenant} */ fromString(items) { diff --git a/lib/primitives/outpoint.js b/lib/primitives/outpoint.js index f4bc3b611..6932ad6f3 100644 --- a/lib/primitives/outpoint.js +++ b/lib/primitives/outpoint.js @@ -109,7 +109,7 @@ class Outpoint extends bio.Struct { /** * Get little-endian hash. - * @returns {Hash} + * @returns {HexHash} */ txid() { diff --git a/lib/primitives/output.js b/lib/primitives/output.js index 76fd5c54f..152ada178 100644 --- a/lib/primitives/output.js +++ b/lib/primitives/output.js @@ -145,7 +145,7 @@ class Output extends bio.Struct { /** * Get the address hash. - * @returns {Hash} hash + * @returns {Hash} */ getHash() { diff --git a/lib/primitives/tx.js b/lib/primitives/tx.js index 9d340def7..0f016c093 100644 --- a/lib/primitives/tx.js +++ b/lib/primitives/tx.js @@ -1602,7 +1602,7 @@ class TX extends bio.Struct { /** * Get little-endian tx hash. - * @returns {Hash} + * @returns {HexHash} */ txid() { @@ -1611,7 +1611,7 @@ class TX extends bio.Struct { /** * Get little-endian wtx hash. - * @returns {Hash} + * @returns {HexHash} */ wtxid() { diff --git a/lib/types.js b/lib/types.js index d34116a4a..f68fb6c3d 100644 --- a/lib/types.js +++ b/lib/types.js @@ -57,8 +57,14 @@ */ /** - * Buffer or hex-string hash. - * @typedef {Buffer|String} Hash + * 32 byte buffer. + * @typedef {Buffer} Hash + * @global + */ + +/** + * Hex-string hash. + * @typedef {String} HexHash * @global */ diff --git a/lib/wallet/wallet.js b/lib/wallet/wallet.js index c094cca1b..4e15acfd4 100644 --- a/lib/wallet/wallet.js +++ b/lib/wallet/wallet.js @@ -1675,10 +1675,7 @@ class Wallet extends EventEmitter { const output = new Output(); output.address = addr; output.value = 0; - output.covenant.type = types.OPEN; - output.covenant.pushHash(nameHash); - output.covenant.pushU32(0); - output.covenant.push(rawName); + output.covenant.setOpen(nameHash, rawName); if (!mtx) mtx = new MTX(); @@ -1807,11 +1804,7 @@ class Wallet extends EventEmitter { const output = new Output(); output.address = addr; output.value = lockup; - output.covenant.type = types.BID; - output.covenant.pushHash(nameHash); - output.covenant.pushU32(start); - output.covenant.push(rawName); - output.covenant.pushHash(blind); + output.covenant.setBid(nameHash, start, rawName, blind); if (!mtx) mtx = new MTX(); @@ -1944,10 +1937,8 @@ class Wallet extends EventEmitter { const output = new Output(); output.address = bidCoin.address; output.value = value; - output.covenant.type = types.REVEAL; - output.covenant.pushHash(nameHash); - output.covenant.pushU32(height); - output.covenant.pushHash(nonce); + output.covenant.setReveal(nameHash, height, nonce); + reveal.addOutpoint(Outpoint.fromTX(bid, bidOuputIndex)); reveal.outputs.push(output); @@ -2035,10 +2026,7 @@ class Wallet extends EventEmitter { const output = new Output(); output.address = coin.address; output.value = value; - output.covenant.type = types.REVEAL; - output.covenant.pushHash(nameHash); - output.covenant.pushU32(ns.height); - output.covenant.pushHash(nonce); + output.covenant.setReveal(nameHash, ns.height, nonce); mtx.addOutpoint(prevout); mtx.outputs.push(output); @@ -2165,10 +2153,7 @@ class Wallet extends EventEmitter { const output = new Output(); output.address = coin.address; output.value = value; - output.covenant.type = types.REVEAL; - output.covenant.pushHash(nameHash); - output.covenant.pushU32(ns.height); - output.covenant.pushHash(nonce); + output.covenant.setReveal(nameHash, ns.height, nonce); mtx.addOutpoint(prevout); mtx.outputs.push(output); @@ -2311,9 +2296,7 @@ class Wallet extends EventEmitter { const output = new Output(); output.address = coin.address; output.value = coin.value; - output.covenant.type = types.REDEEM; - output.covenant.pushHash(nameHash); - output.covenant.pushU32(ns.height); + output.covenant.setRedeem(nameHash, ns.height); mtx.outputs.push(output); pushed++; @@ -2436,9 +2419,7 @@ class Wallet extends EventEmitter { const output = new Output(); output.address = coin.address; output.value = coin.value; - output.covenant.type = types.REDEEM; - output.covenant.pushHash(nameHash); - output.covenant.pushU32(ns.height); + output.covenant.setRedeem(nameHash, ns.height); mtx.addOutpoint(prevout); mtx.outputs.push(output); @@ -2569,9 +2550,7 @@ class Wallet extends EventEmitter { output.address = coin.address; output.value = ns.value; - output.covenant.type = types.REGISTER; - output.covenant.pushHash(nameHash); - output.covenant.pushU32(ns.height); + let rawResource = EMPTY; if (resource) { const raw = resource.encode(); @@ -2582,12 +2561,12 @@ class Wallet extends EventEmitter { `(${rules.MAX_RESOURCE_SIZE}) for name: ${name}.` ); - output.covenant.push(raw); - } else { - output.covenant.push(EMPTY); + rawResource = raw; } - output.covenant.pushHash(await this.wdb.getRenewalBlock()); + const blockHash = await this.wdb.getRenewalBlock(); + + output.covenant.setRegister(nameHash, ns.height, rawResource, blockHash); if (!mtx) mtx = new MTX(); @@ -2664,10 +2643,7 @@ class Wallet extends EventEmitter { const output = new Output(); output.address = coin.address; output.value = coin.value; - output.covenant.type = types.UPDATE; - output.covenant.pushHash(nameHash); - output.covenant.pushU32(ns.height); - output.covenant.push(raw); + output.covenant.setUpdate(nameHash, ns.height, raw); if (!mtx) mtx = new MTX(); @@ -2803,10 +2779,8 @@ class Wallet extends EventEmitter { const output = new Output(); output.address = coin.address; output.value = coin.value; - output.covenant.type = types.RENEW; - output.covenant.pushHash(nameHash); - output.covenant.pushU32(ns.height); - output.covenant.pushHash(await this.wdb.getRenewalBlock()); + const blockHash = await this.wdb.getRenewalBlock(); + output.covenant.setRenew(nameHash, ns.height, blockHash); if (!mtx) mtx = new MTX(); @@ -2881,10 +2855,7 @@ class Wallet extends EventEmitter { const output = new Output(); output.address = coin.address; output.value = coin.value; - output.covenant.type = types.RENEW; - output.covenant.pushHash(ns.nameHash); - output.covenant.pushU32(ns.height); - output.covenant.pushHash(renewalBlock); + output.covenant.setRenew(ns.nameHash, ns.height, renewalBlock); mtx.addOutpoint(new Outpoint(coin.hash, coin.index)); mtx.outputs.push(output); @@ -3020,11 +2991,7 @@ class Wallet extends EventEmitter { const output = new Output(); output.address = coin.address; output.value = coin.value; - output.covenant.type = types.TRANSFER; - output.covenant.pushHash(nameHash); - output.covenant.pushU32(ns.height); - output.covenant.pushU8(address.version); - output.covenant.push(address.hash); + output.covenant.setTransfer(nameHash, ns.height, address); if (!mtx) mtx = new MTX(); @@ -3157,10 +3124,7 @@ class Wallet extends EventEmitter { const output = new Output(); output.address = coin.address; output.value = coin.value; - output.covenant.type = types.UPDATE; - output.covenant.pushHash(nameHash); - output.covenant.pushU32(ns.height); - output.covenant.push(EMPTY); + output.covenant.setUpdate(nameHash, ns.height, EMPTY); if (!mtx) mtx = new MTX(); @@ -3297,14 +3261,15 @@ class Wallet extends EventEmitter { const output = new Output(); output.address = address; output.value = coin.value; - output.covenant.type = types.FINALIZE; - output.covenant.pushHash(nameHash); - output.covenant.pushU32(ns.height); - output.covenant.push(rawName); - output.covenant.pushU8(flags); - output.covenant.pushU32(ns.claimed); - output.covenant.pushU32(ns.renewals); - output.covenant.pushHash(await this.wdb.getRenewalBlock()); + output.covenant.setFinalize( + nameHash, + ns.height, + rawName, + flags, + ns.claimed, + ns.renewals, + await this.wdb.getRenewalBlock(), + ); if (!mtx) mtx = new MTX(); @@ -3362,14 +3327,15 @@ class Wallet extends EventEmitter { const output = new Output(); output.address = address; output.value = coin.value; - output.covenant.type = types.FINALIZE; - output.covenant.pushHash(ns.nameHash); - output.covenant.pushU32(ns.height); - output.covenant.push(Buffer.from(ns.name, 'ascii')); - output.covenant.pushU8(flags); - output.covenant.pushU32(ns.claimed); - output.covenant.pushU32(ns.renewals); - output.covenant.pushHash(await this.wdb.getRenewalBlock()); + output.covenant.setFinalize( + ns.nameHash, + ns.height, + Buffer.from(ns.name, 'ascii'), + flags, + ns.claimed, + ns.renewals, + await this.wdb.getRenewalBlock() + ); mtx.addOutpoint(new Outpoint(coin.hash, coin.index)); mtx.outputs.push(output); @@ -3510,9 +3476,8 @@ class Wallet extends EventEmitter { const output = new Output(); output.address = coin.address; output.value = coin.value; - output.covenant.type = types.REVOKE; - output.covenant.pushHash(nameHash); - output.covenant.pushU32(ns.height); + + output.covenant.setRevoke(nameHash, ns.height); if (!mtx) mtx = new MTX(); diff --git a/test/anyone-can-renew-test.js b/test/anyone-can-renew-test.js index da123b690..ee039bc4a 100644 --- a/test/anyone-can-renew-test.js +++ b/test/anyone-can-renew-test.js @@ -184,10 +184,12 @@ describe('Anyone-can-renew address', function() { value: coin.value, address: coin.address })); - mtx.output(0).covenant.type = rules.types.RENEW; - mtx.output(0).covenant.pushHash(nameHash); - mtx.output(0).covenant.pushU32(heightBeforeOpen + 1); - mtx.output(0).covenant.pushHash(node.chain.tip.hash); + + mtx.output(0).covenant.setRenew( + nameHash, + heightBeforeOpen + 1, + node.chain.tip.hash + ); await alice.fund(mtx, {coins: [coin]}); await alice.finalize(mtx, {coins: [coin]}); @@ -212,10 +214,12 @@ describe('Anyone-can-renew address', function() { value: coin.value, address: coin.address })); - mtx.output(0).covenant.type = rules.types.UPDATE; - mtx.output(0).covenant.pushHash(nameHash); - mtx.output(0).covenant.pushU32(heightBeforeOpen + 1); - mtx.output(0).covenant.push(Buffer.alloc(1)); + + mtx.output(0).covenant.setUpdate( + nameHash, + heightBeforeOpen + 1, + Buffer.alloc(1) + ); await alice.fund(mtx, {coins: [coin]}); await alice.finalize(mtx, {coins: [coin]}); @@ -240,11 +244,12 @@ describe('Anyone-can-renew address', function() { value: coin.value, address: coin.address })); - mtx.output(0).covenant.type = rules.types.TRANSFER; - mtx.output(0).covenant.pushHash(nameHash); - mtx.output(0).covenant.pushU32(heightBeforeOpen + 1); - mtx.output(0).covenant.pushU8(0); - mtx.output(0).covenant.push(Buffer.alloc(20)); + + mtx.output(0).covenant.setTransfer( + nameHash, + heightBeforeOpen + 1, + new Address({ version: 0, hash: Buffer.alloc(20) }) + ); await alice.fund(mtx, {coins: [coin]}); await alice.finalize(mtx, {coins: [coin]}); @@ -269,10 +274,12 @@ describe('Anyone-can-renew address', function() { value: coin.value, address: coin.address })); - mtx.output(0).covenant.type = rules.types.RENEW; - mtx.output(0).covenant.pushHash(nameHash); - mtx.output(0).covenant.pushU32(heightBeforeOpen + 1); - mtx.output(0).covenant.pushHash(node.chain.tip.hash); + + mtx.output(0).covenant.setRenew( + nameHash, + heightBeforeOpen + 1, + node.chain.tip.hash + ); await alice.fund(mtx, {coins: [coin]}); await alice.finalize(mtx, {coins: [coin]}); @@ -294,10 +301,12 @@ describe('Anyone-can-renew address', function() { value: coin.value, address: coin.address })); - mtx.output(0).covenant.type = rules.types.RENEW; - mtx.output(0).covenant.pushHash(nameHash); - mtx.output(0).covenant.pushU32(heightBeforeOpen + 1); - mtx.output(0).covenant.pushHash(node.chain.tip.hash); + + mtx.output(0).covenant.setRenew( + nameHash, + heightBeforeOpen + 1, + node.chain.tip.hash + ); await bob.fund(mtx, {coins: [coin]}); await bob.finalize(mtx, {coins: [coin]}); diff --git a/test/contractstate-test.js b/test/contractstate-test.js index 3dbd65409..03bee42f1 100644 --- a/test/contractstate-test.js +++ b/test/contractstate-test.js @@ -23,30 +23,17 @@ function nameContext(name, type) { switch (type) { case types.OPEN: - output.covenant.type = types.OPEN; - output.covenant.pushHash(nameHash); // nameHash - output.covenant.pushU32(0); // start - output.covenant.push(rawName); // rawName + output.covenant.setOpen(nameHash, rawName); break; case types.BID: - output.covenant.type = types.BID; - output.covenant.pushHash(nameHash); // nameHash - output.covenant.pushU32(0); // start - output.covenant.push(rawName); // rawName - output.covenant.pushHash(Buffer.alloc(32)); // blind + output.covenant.setBid(nameHash, 0, rawName, Buffer.alloc(32)); break; case types.REVEAL: - output.covenant.type = types.REVEAL; - output.covenant.pushHash(nameHash); // nameHash - output.covenant.pushU32(100); // height - output.covenant.pushHash(Buffer.alloc(32)); // nonce + output.covenant.setReveal(nameHash, 100, Buffer.alloc(32)); break; case types.UPDATE: { const data = Buffer.from('hello world', 'ascii'); - output.covenant.type = types.UPDATE; - output.covenant.pushHash(nameHash); // nameHash - output.covenant.pushU32(100); // height - output.covenant.push(data); // record + output.covenant.setUpdate(nameHash, 100, data); break; } } diff --git a/test/interactive-swap-test.js b/test/interactive-swap-test.js index b79372419..305ea6047 100644 --- a/test/interactive-swap-test.js +++ b/test/interactive-swap-test.js @@ -163,14 +163,15 @@ describe('Interactive name swap', function() { const output0 = new Output(); output0.value = coin.value; output0.address = bobReceive; - output0.covenant.type = types.FINALIZE; - output0.covenant.pushHash(nameHash); - output0.covenant.pushU32(ns.height); - output0.covenant.push(Buffer.from(name, 'ascii')); - output0.covenant.pushU8(0); // flags, may be required if name was CLAIMed - output0.covenant.pushU32(ns.claimed); - output0.covenant.pushU32(ns.renewals); - output0.covenant.pushHash(await wdb.getRenewalBlock()); + output0.covenant.setFinalize( + nameHash, + ns.height, + Buffer.from(name, 'ascii'), + 0, // flags, may be required if name was CLAIMed + ns.claimed, + ns.renewals, + await wdb.getRenewalBlock(), + ); const output1 = new Output(); output1.address = aliceReceive; diff --git a/test/mempool-test.js b/test/mempool-test.js index a8a0d9026..c193354c9 100644 --- a/test/mempool-test.js +++ b/test/mempool-test.js @@ -30,7 +30,6 @@ const ALL = Script.hashType.ALL; const common = require('../lib/blockchain/common'); const VERIFY_BODY = common.flags.VERIFY_BODY; const rules = require('../lib/covenants/rules'); -const {types} = rules; const NameState = require('../lib/covenants/namestate'); const {states} = NameState; const {ownership} = require('../lib/covenants/ownership'); @@ -960,10 +959,7 @@ describe('Mempool', function() { const name = rules.grindName(10, 0, mempool.network); const rawName = Buffer.from(name, 'ascii'); const nameHash = rules.hashName(rawName); - open.outputs[0].covenant.type = types.OPEN; - open.outputs[0].covenant.pushHash(nameHash); - open.outputs[0].covenant.pushU32(0); - open.outputs[0].covenant.push(rawName); + open.outputs[0].covenant.setOpen(nameHash, rawName); chaincoins.sign(open); open = open.toTX(); @@ -993,11 +989,12 @@ describe('Mempool', function() { bid.addCoin(bidCoin); bid.addOutput(addr, 70000); - bid.outputs[0].covenant.type = types.BID; - bid.outputs[0].covenant.pushHash(nameHash); - bid.outputs[0].covenant.pushU32(ns.height); - bid.outputs[0].covenant.push(rawName); - bid.outputs[0].covenant.pushHash(Buffer.alloc(32, 0x01)); + bid.outputs[0].covenant.setBid( + nameHash, + ns.height, + rawName, + Buffer.alloc(32, 0x01) + ); chaincoins.sign(bid); bid = bid.toTX(); diff --git a/test/util/memwallet.js b/test/util/memwallet.js index 7c37ee210..57d47c15c 100644 --- a/test/util/memwallet.js +++ b/test/util/memwallet.js @@ -1030,10 +1030,7 @@ class MemWallet { const output = new Output(); output.address = addr; output.value = 0; - output.covenant.type = types.OPEN; - output.covenant.pushHash(nameHash); - output.covenant.pushU32(0); - output.covenant.push(rawName); + output.covenant.setOpen(nameHash, rawName); const mtx = new MTX(); mtx.outputs.push(output); @@ -1084,11 +1081,7 @@ class MemWallet { const output = new Output(); output.address = addr; output.value = lockup; - output.covenant.type = types.BID; - output.covenant.pushHash(nameHash); - output.covenant.pushU32(start); - output.covenant.push(rawName); - output.covenant.pushHash(blind); + output.covenant.setBid(nameHash, start, rawName, blind); const mtx = new MTX(); mtx.outputs.push(output); @@ -1148,10 +1141,7 @@ class MemWallet { const output = new Output(); output.address = coin.address; output.value = value; - output.covenant.type = types.REVEAL; - output.covenant.pushHash(nameHash); - output.covenant.pushU32(ns.height); - output.covenant.pushHash(nonce); + output.covenant.setReveal(nameHash, ns.height, nonce); mtx.addOutpoint(prevout); mtx.outputs.push(output); @@ -1208,9 +1198,7 @@ class MemWallet { const output = new Output(); output.address = coin.address; output.value = coin.value; - output.covenant.type = types.REDEEM; - output.covenant.pushHash(nameHash); - output.covenant.pushU32(ns.height); + output.covenant.setRedeem(nameHash, ns.height); mtx.outputs.push(output); } @@ -1268,16 +1256,13 @@ class MemWallet { output.address = coin.address; output.value = ns.value; - output.covenant.type = types.REGISTER; - output.covenant.pushHash(nameHash); - output.covenant.pushU32(ns.height); + let record = EMPTY; if (resource) - output.covenant.push(resource); - else - output.covenant.push(EMPTY); + record = resource; - output.covenant.pushHash(this.getRenewalBlock()); + const blockHash = this.getRenewalBlock(); + output.covenant.setRegister(nameHash, ns.height, record, blockHash); const mtx = new MTX(); mtx.addOutpoint(ns.owner); @@ -1337,10 +1322,7 @@ class MemWallet { const output = new Output(); output.address = coin.address; output.value = coin.value; - output.covenant.type = types.UPDATE; - output.covenant.pushHash(nameHash); - output.covenant.pushU32(ns.height); - output.covenant.push(resource); + output.covenant.setUpdate(nameHash, ns.height, resource); const mtx = new MTX(); mtx.addOutpoint(ns.owner); @@ -1392,10 +1374,7 @@ class MemWallet { const output = new Output(); output.address = coin.address; output.value = coin.value; - output.covenant.type = types.RENEW; - output.covenant.pushHash(nameHash); - output.covenant.pushU32(ns.height); - output.covenant.pushHash(this.getRenewalBlock()); + output.covenant.setRenew(nameHash, ns.height, this.getRenewalBlock()); const mtx = new MTX(); mtx.addOutpoint(ns.owner); @@ -1445,11 +1424,7 @@ class MemWallet { const output = new Output(); output.address = coin.address; output.value = coin.value; - output.covenant.type = types.TRANSFER; - output.covenant.pushHash(nameHash); - output.covenant.pushU32(ns.height); - output.covenant.pushU8(address.version); - output.covenant.push(address.hash); + output.covenant.setTransfer(nameHash, ns.height, address); const mtx = new MTX(); mtx.addOutpoint(ns.owner); @@ -1499,10 +1474,7 @@ class MemWallet { const output = new Output(); output.address = coin.address; output.value = coin.value; - output.covenant.type = types.UPDATE; - output.covenant.pushHash(nameHash); - output.covenant.pushU32(ns.height); - output.covenant.push(EMPTY); + output.covenant.setUpdate(nameHash, ns.height, EMPTY); const mtx = new MTX(); mtx.addOutpoint(ns.owner); @@ -1559,14 +1531,15 @@ class MemWallet { const output = new Output(); output.address = address; output.value = coin.value; - output.covenant.type = types.FINALIZE; - output.covenant.pushHash(nameHash); - output.covenant.pushU32(ns.height); - output.covenant.push(rawName); - output.covenant.pushU8(flags); - output.covenant.pushU32(ns.claimed); - output.covenant.pushU32(ns.renewals); - output.covenant.pushHash(this.getRenewalBlock()); + output.covenant.setFinalize( + nameHash, + ns.height, + rawName, + flags, + ns.claimed, + ns.renewals, + this.getRenewalBlock(), + ); const mtx = new MTX(); mtx.addOutpoint(ns.owner); @@ -1615,9 +1588,7 @@ class MemWallet { const output = new Output(); output.address = coin.address; output.value = coin.value; - output.covenant.type = types.REVOKE; - output.covenant.pushHash(nameHash); - output.covenant.pushU32(ns.height); + output.covenant.setRevoke(nameHash, ns.height); const mtx = new MTX(); mtx.addOutpoint(ns.owner); diff --git a/test/wallet-http-test.js b/test/wallet-http-test.js index 8c3639abb..a6e81b409 100644 --- a/test/wallet-http-test.js +++ b/test/wallet-http-test.js @@ -2103,10 +2103,7 @@ function openOutput(name, address) { const output = new Output(); output.address = Address.fromString(address); output.value = 0; - output.covenant.type = types.OPEN; - output.covenant.pushHash(nameHash); - output.covenant.pushU32(0); - output.covenant.push(rawName); + output.covenant.setOpen(nameHash, rawName); return output; } diff --git a/test/wallet-test.js b/test/wallet-test.js index da47e0445..2d8d4e5f1 100644 --- a/test/wallet-test.js +++ b/test/wallet-test.js @@ -26,7 +26,6 @@ const HDPrivateKey = require('../lib/hd/private'); const Mnemonic = require('../lib/hd/mnemonic'); const Wallet = require('../lib/wallet/wallet'); const rules = require('../lib/covenants/rules'); -const {types, hashName} = rules; const {forValue} = require('./util/common'); const KEY1 = 'xprv9s21ZrQH143K3Aj6xQBymM31Zb4BVc7wxqfUhMZrzewdDVCt' @@ -1690,7 +1689,7 @@ describe('Wallet', function() { await wdb.addTXMap(b, mtx.hash(), wid); // Add one name to NameMap - await wdb.addNameMap(b, hashName('test123'), wid); + await wdb.addNameMap(b, rules.hashName('test123'), wid); await b.write(); @@ -1699,7 +1698,7 @@ describe('Wallet', function() { assert(wids.has(wid)); // Should have wid in NameMap - let map = await wdb.getNameMap(hashName('test123')); + let map = await wdb.getNameMap(rules.hashName('test123')); assert(map.wids.has(wid)); // Remove wallet @@ -1710,7 +1709,7 @@ describe('Wallet', function() { assert.strictEqual(wids, null); // Should not return wid from NameMap after wid is removed - map = await wdb.getNameMap(hashName('test123')); + map = await wdb.getNameMap(rules.hashName('test123')); assert.strictEqual(map, null); // Should not return wallet after it is removed @@ -2308,11 +2307,11 @@ describe('Wallet', function() { const output = new Output(); output.value = secondHighest; - output.covenant.type = types.REVEAL; - output.covenant.pushHash(nameHash); - output.covenant.pushU32(start); - output.covenant.push(Buffer.from(name, 'ascii')); - output.covenant.pushHash(Buffer.alloc(32)); + output.covenant.setReveal( + nameHash, + start, + Buffer.alloc(32) + ); const mtx = new MTX(); mtx.outputs.push(output); @@ -2674,11 +2673,11 @@ describe('Wallet', function() { const output = new Output(); output.value = secondHighest; - output.covenant.type = types.REVEAL; - output.covenant.pushHash(nameHash); - output.covenant.pushU32(start); - output.covenant.push(Buffer.from(name, 'ascii')); - output.covenant.pushHash(Buffer.alloc(32)); + output.covenant.setReveal( + nameHash, + start, + Buffer.alloc(32), + ); const mtx = new MTX(); mtx.outputs.push(output);