From ac2fe2a2bbad8b99b9f68dfa9c3b05a2cecbf4d4 Mon Sep 17 00:00:00 2001 From: Henry G Date: Tue, 27 Feb 2018 16:50:47 -0500 Subject: [PATCH 01/41] init --- lib/GenScripts.ts | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 lib/GenScripts.ts diff --git a/lib/GenScripts.ts b/lib/GenScripts.ts new file mode 100644 index 0000000..e69de29 From c3ba1d985f2ab523ec5e1f2e1bba9072cebee925 Mon Sep 17 00:00:00 2001 From: Henry G Date: Tue, 6 Mar 2018 16:46:03 -0500 Subject: [PATCH 02/41] moved functions over, need new commit file from josh to import --- lib/GenScripts.ts | 103 ++++++++++++++++++++++++++++++++++++++++++++++ lib/txs.ts | 101 +++------------------------------------------ 2 files changed, 109 insertions(+), 95 deletions(-) diff --git a/lib/GenScripts.ts b/lib/GenScripts.ts index e69de29..636c08b 100644 --- a/lib/GenScripts.ts +++ b/lib/GenScripts.ts @@ -0,0 +1,103 @@ +import { + script as Script, + crypto +} from 'bcoin'; + +import { + BadUserPublicKeyError, + BadServicePublicKeyError, +} from './errors'; + +/** + * Generate a redeem script, removing a name/key pair from the blockchain. + * Validates `userPubkey` and `servicePubkey`. + * + * @param userPubkey The user's public key. + * @param servicePubkey The service's public key. + * @param alocktime An absolute lock time, in blocks. + */ +function genRedeemScript(userPubkey: Buffer, servicePubkey: Buffer, alocktime: number): Script { + // Validate user public key + if (!crypto.secp256k1.publicKeyVerify(userPubkey)) { + throw new BadUserPublicKeyError(); + } + + // Validate service public key + if (!crypto.secp256k1.publicKeyVerify(servicePubkey)) { + throw new BadServicePublicKeyError(); + } + + const script = new Script(null); + + script.pushSym('OP_IF'); + + // + // If spending as user, execute this branch + + // Verify that 0 <= current block size - commit block size + script.pushInt(0); + script.pushSym('OP_CHECKSEQUENCEVERIFY'); + script.pushSym('OP_DROP'); + + // Check the provided user signature + script.pushData(userPubkey); + script.pushSym('OP_CHECKSIG'); + + script.pushSym('OP_ELSE'); + + // + // Otherwise, if spending as service, execute this branch + + // Verify that alocktime <= current block size + script.pushInt(alocktime); + script.pushSym('OP_CHECKLOCKTIMEVERIFY'); + script.pushSym('OP_DROP'); + + // Check the provided service signature + script.pushData(servicePubkey); + script.pushSym('OP_CHECKSIG'); + + script.pushSym('OP_ENDIF'); + + script.compile(); + + return script; +} + +/** + * Generate a commit redeem script. + * @param userPubkey The user's public key. + * @param nonce A 256-bit buffer representing a nonce. + * @param name A name of at most 64 characters composed of URL-safe characters. + * @param locktime An absolute lock time, in blocks. + */ +function genCommitRedeemScript(userPubkey: Buffer, nonce: Buffer, name: string, locktime: number): Script { + // Validate user public key + if (!crypto.secp256k1.publicKeyVerify(userPubkey)) { + throw new BadUserPublicKeyError(); + } + + const script = new Script(); + + // Verify that at least six blocks have passed since commit + script.pushInt(6); + script.pushSym('OP_CHECKSEQUENCEVERIFY'); + script.pushSym('OP_DROP'); + + // + // Hash [256-bit nonce + 2-byte locktime (BE) + 1-byte length of name + name] + // and check against parameters. + script.pushSym('OP_HASH256'); + + const hashData = serializeCommitData(nonce, locktime, name); + const hash = crypto.hash256(hashData); + script.pushData(hash); + script.pushSym('OP_EQUALVERIFY'); + + // Check user signature + script.pushData(userPubkey); + script.pushSym('OP_CHECKSIG'); + + script.compile(); + return script; +} \ No newline at end of file diff --git a/lib/txs.ts b/lib/txs.ts index 2708e37..9241a1b 100644 --- a/lib/txs.ts +++ b/lib/txs.ts @@ -13,6 +13,11 @@ import { import randomBytes = require('randombytes'); +import { + genRedeemScript, + genCommitRedeemScript, +} from GenScripts; + import { BadUserPublicKeyError, BadServicePublicKeyError, @@ -21,104 +26,10 @@ import { import { verifyLockTX, isURISafe, verifyCommitTX } from './verify'; -/** - * Generate a redeem script, removing a name/key pair from the blockchain. - * Validates `userPubkey` and `servicePubkey`. - * - * @param userPubkey The user's public key. - * @param servicePubkey The service's public key. - * @param alocktime An absolute lock time, in blocks. - */ -function genRedeemScript(userPubkey: Buffer, servicePubkey: Buffer, alocktime: number): Script { - // Validate user public key - if (!crypto.secp256k1.publicKeyVerify(userPubkey)) { - throw new BadUserPublicKeyError(); - } - - // Validate service public key - if (!crypto.secp256k1.publicKeyVerify(servicePubkey)) { - throw new BadServicePublicKeyError(); - } - - const script = new Script(null); - - script.pushSym('OP_IF'); - - // - // If spending as user, execute this branch - - // Verify that 0 <= current block size - commit block size - script.pushInt(0); - script.pushSym('OP_CHECKSEQUENCEVERIFY'); - script.pushSym('OP_DROP'); - - // Check the provided user signature - script.pushData(userPubkey); - script.pushSym('OP_CHECKSIG'); - - script.pushSym('OP_ELSE'); - - // - // Otherwise, if spending as service, execute this branch - - // Verify that alocktime <= current block size - script.pushInt(alocktime); - script.pushSym('OP_CHECKLOCKTIMEVERIFY'); - script.pushSym('OP_DROP'); - - // Check the provided service signature - script.pushData(servicePubkey); - script.pushSym('OP_CHECKSIG'); - - script.pushSym('OP_ENDIF'); - - script.compile(); - - return script; -} - -/** - * Generate a commit redeem script. - * @param userPubkey The user's public key. - * @param nonce A 256-bit buffer representing a nonce. - * @param name A name of at most 64 characters composed of URL-safe characters. - * @param locktime An absolute lock time, in blocks. - */ -function genCommitRedeemScript(userPubkey: Buffer, nonce: Buffer, name: string, locktime: number): Script { - // Validate user public key - if (!crypto.secp256k1.publicKeyVerify(userPubkey)) { - throw new BadUserPublicKeyError(); - } - - const script = new Script(); - - // Verify that at least six blocks have passed since commit - script.pushInt(6); - script.pushSym('OP_CHECKSEQUENCEVERIFY'); - script.pushSym('OP_DROP'); - - // - // Hash [256-bit nonce + 2-byte locktime (BE) + 1-byte length of name + name] - // and check against parameters. - script.pushSym('OP_HASH256'); - - const hashData = serializeCommitData(nonce, locktime, name); - const hash = crypto.hash256(hashData); - script.pushData(hash); - script.pushSym('OP_EQUALVERIFY'); - - // Check user signature - script.pushData(userPubkey); - script.pushSym('OP_CHECKSIG'); - - script.compile(); - return script; -} - /** * Generate a P2SH address from a redeem script. * @param redeemScript The script to use. - */ + */register function genP2shAddr(redeemScript: Script): Address { const outputScript = Script.fromScripthash(redeemScript.hash160()); const p2shAddr = Address.fromScript(outputScript); From ac6a3d7f84664077fce7ca46d2d21495820f13f0 Mon Sep 17 00:00:00 2001 From: Henry G Date: Tue, 6 Mar 2018 16:49:16 -0500 Subject: [PATCH 03/41] forgot to add exports --- lib/GenScripts.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/GenScripts.ts b/lib/GenScripts.ts index 636c08b..c531a85 100644 --- a/lib/GenScripts.ts +++ b/lib/GenScripts.ts @@ -100,4 +100,9 @@ function genCommitRedeemScript(userPubkey: Buffer, nonce: Buffer, name: string, script.compile(); return script; -} \ No newline at end of file +} + +export { + genRedeemScript, + genCommitRedeemScript, +}; From 37c63c7b81a3ac839bbb76ac92c0ed8af095bc14 Mon Sep 17 00:00:00 2001 From: Henry G Date: Tue, 6 Mar 2018 16:51:15 -0500 Subject: [PATCH 04/41] moved everything over and deleted current dependencies in txs.ts, wiating for stages split so can use that file --- lib/txs.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/txs.ts b/lib/txs.ts index 9241a1b..f466ece 100644 --- a/lib/txs.ts +++ b/lib/txs.ts @@ -466,13 +466,11 @@ function genUnlockTx(lockTx: TX, export { genLockTx, genUnlockTx, - genRedeemScript, genP2shAddr, getLockTxName, getLockTxTime, getLockTxPubKey, genCommitTx, - genCommitRedeemScript, serializeCommitData, deserializeCommitData, }; From 93c27bc4ee4ccc29a8f551d5f213c55a30fa1e06 Mon Sep 17 00:00:00 2001 From: JWood323 Date: Tue, 20 Mar 2018 13:09:55 -0400 Subject: [PATCH 05/41] Split txs.ts into multiple files based on stages --- lib/tx-commit.ts | 97 ++++++++++++++++++++++++++++++++++++++++++ lib/tx-lock.ts | 108 +++++++++++++++++++++++++++++++++++++++++++++++ lib/tx-unlock.ts | 95 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 300 insertions(+) create mode 100644 lib/tx-commit.ts create mode 100644 lib/tx-lock.ts create mode 100644 lib/tx-unlock.ts diff --git a/lib/tx-commit.ts b/lib/tx-commit.ts new file mode 100644 index 0000000..a3e6774 --- /dev/null +++ b/lib/tx-commit.ts @@ -0,0 +1,97 @@ +/** + * Generate a commit transaction. + * @param coins Array of coins to fund the transaction. + * @param name Name for name/key pair. + * @param locktime Absolute locktime + * @param commitFee Commit fee, in satoshis. + * @param registerFee Registration fee, in satoshis. + * @param escrowFee Escrow fee, in satoshis. + * @param feeRate Fee rate, in satoshis/kilobyte. + * @param userRing The user's key ring. + * @param servicePubKey Service public key. + */ +function genCommitTx(coins: Coin[], + name: string, + locktime: number, + commitFee: number, + registerFee: number, + escrowFee: number, + feeRate: number, + userRing: KeyRing, + servicePubKey: Buffer): TX { + // + // Validate name and the service public key + if (name.length > 64) { + throw new Error('Name is too long'); + } + + if (!isURISafe(name)) { + throw new Error('Invalid character(s) in name'); + } + + if (!crypto.secp256k1.publicKeyVerify(servicePubKey)) { + throw new Error('Invalid service public key'); + } + + // Generate a P2SH address from a redeem script, using a random nonce + const nonce = randomBytes(32); + const redeemScript = genCommitRedeemScript(userRing.getPublicKey(), nonce, name, locktime); + const p2shAddr = genP2shAddr(redeemScript); + + // Generate service address from service public key + const servicePKH = crypto.hash160(servicePubKey); + const serviceAddr = Address.fromPubkeyhash(servicePKH); + + const lockTx = new MTX(); + + // Compute total value of coins + const total = coins.reduce((acc, cur) => acc + cur.value, 0); + + for (const coin of coins) { + lockTx.addCoin(coin); + } + + // Compute change amount + const changeVal = total - (commitFee + registerFee + escrowFee) - (4 * feeRate); + + // Add nonce OP_RETURN as output 0 + const pubkeyDataScript = Script.fromNulldata(nonce); + lockTx.addOutput(Output.fromScript(pubkeyDataScript, 0)); + + // Add service upfront fee as output 1 + lockTx.addOutput({ + address: serviceAddr, + value: commitFee, + }); + + // Add locked fee as output 2 + // Locks up the fee to register, the fee to be put in escrow, and enough for a 4kb tx at current rates + lockTx.addOutput({ + address: p2shAddr, + value: registerFee + escrowFee + 4 * feeRate, + }); + + // Add change output as 3 + lockTx.addOutput({ + address: userRing.getAddress(), + value: changeVal, + }); + + // Add coins as inputs + for (let i = 0; i < coins.length; ++i) { + const coin = coins[i]; + lockTx.scriptInput(i, coin, userRing); + } + + // Each signature is 72 bytes long + const virtSize = lockTx.getVirtualSize() + coins.length * 72; + lockTx.subtractIndex(3, Math.ceil(virtSize / 1000 * feeRate)); + + // Sign the coins + for (let i = 0; i < coins.length; ++i) { + const coin = coins[i]; + lockTx.signInput(i, coin, userRing, Script.hashType.ALL); + } + + return lockTx.toTX(); +} \ No newline at end of file diff --git a/lib/tx-lock.ts b/lib/tx-lock.ts new file mode 100644 index 0000000..fc1fa88 --- /dev/null +++ b/lib/tx-lock.ts @@ -0,0 +1,108 @@ +/** + * Generate a lock transaction. + * @param commitTX The corresponding commit transaction. + * @param name The name to use. + * @param upfrontFee The upfront fee in satoshis to use the service, as + * determined by the service. + * @param lockedFee Fee incentivizing registrar to provide service, as + * determined by the service. + * @param feeRate Fee rate in satoshi/KB. + * @param userRing The user key ring. + * @param servicePubKey Service public key. + * @param locktime Absolute lock time in blocks. + */ +function genLockTx(commitTX: TX, + name: string, + upfrontFee: number, + lockedFee: number, + feeRate: number, + userRing: KeyRing, + servicePubKey: Buffer, + locktime: number): TX { + // + // Input validation + if (locktime > 500000000) { + throw new Error('Locktime must be less than 500000000 blocks'); + } + + if (name.length > 64) { + throw new Error('Name is too long'); + } + + if (!isURISafe(name)) { + throw new Error('Invalid character(s) in name'); + } + + if (!crypto.secp256k1.publicKeyVerify(servicePubKey)) { + throw new Error('Invalid service public key'); + } + + if (!verifyCommitTX(commitTX, userRing.getPublicKey(), servicePubKey, name, locktime)) { + throw new Error('Invalid commitment tx'); + } + + // Generate a P2SH address from redeem script + const redeemScript = genRedeemScript(userRing.getPublicKey(), servicePubKey, locktime); + const p2shAddr = genP2shAddr(redeemScript); + + // Generate address from service public key + const servicePKH = crypto.hash160(servicePubKey); + const serviceAddr = Address.fromPubkeyhash(servicePKH); + + const lockTx = MTX.fromOptions({ + version: 2, + }); + + lockTx.addTX(commitTX, 2); + + lockTx.setSequence(0, 6); + + const total = commitTX.outputs[2].value; + + const changeVal = total - upfrontFee - lockedFee; + + // Add upfront fee as output 0 + lockTx.addOutput({ + address: serviceAddr, + value: upfrontFee, + }); + + // Add locked fee as output 1 + lockTx.addOutput({ + address: p2shAddr, + value: lockedFee, + }); + + // Add change output as 2 + lockTx.addOutput({ + address: userRing.getAddress(), + value: changeVal, + }); + + const nonce = commitTX.outputs[0].script.code[1].data; + + const hashData = serializeCommitData(nonce, locktime, name); + + const commitRedeemScript = genCommitRedeemScript(userRing.getPublicKey(), nonce, name, locktime); + + const unlockScript = new Script(); + unlockScript.pushData(hashData); + unlockScript.pushData(commitRedeemScript.toRaw()); + unlockScript.compile(); + + lockTx.inputs[0].script = unlockScript; + + // Add constant for signature + const virtSize = lockTx.getVirtualSize() + 72; + + // Calculate fee to be paid + const fee = Math.ceil(virtSize / 1000 * feeRate); + lockTx.subtractIndex(2, fee); + + // Add signature + const sig = lockTx.signature(0, commitRedeemScript, total, userRing.getPrivateKey(), Script.hashType.ALL, 0); + unlockScript.insertData(0, sig); + unlockScript.compile(); + + return lockTx.toTX(); +} \ No newline at end of file diff --git a/lib/tx-unlock.ts b/lib/tx-unlock.ts new file mode 100644 index 0000000..342f53f --- /dev/null +++ b/lib/tx-unlock.ts @@ -0,0 +1,95 @@ +/** + * Generate an unlock transaction. + * @param lockTx The corresponding lock transaction. + * @param commitTx The corresponding commit transaction. + * @param feeRate The fee rate in satoshi/KB. + * @param service Whether to use the script as service. + * @param ring The service key ring if `service`, otherwise user key ring. + * @param otherPubKey The user key ring if `service`, otherwise service key ring. + */ +function genUnlockTx(lockTx: TX, + commitTx: TX, + feeRate: number, + service: boolean, + ring: KeyRing, + otherPubKey: Buffer): TX { + // Disambiguate ring public key and the other public key + const servicePubKey = service ? ring.getPublicKey() : otherPubKey; + const userPubKey = !service ? ring.getPublicKey() : otherPubKey; + + // + // Input validation + if (!verifyLockTX(lockTx, commitTx, servicePubKey)) { + throw new BadLockTransactionError(); + } + + if (!crypto.secp256k1.publicKeyVerify(otherPubKey)) { + throw new Error('Invalid service public key'); + } + + const locktime = getLockTxTime(lockTx); + if (locktime === null) { + throw new Error('Could not extract locktime'); + } + + const redeemScript = genRedeemScript(userPubKey, servicePubKey, locktime); + + const val = lockTx.outputs[1].value; // the P2SH output + const unlockTx = MTX.fromOptions({ + version: 2, + }); + + unlockTx.addTX(lockTx, 1); + + const boolVal = service ? 0 : 1; + + if (service) { + unlockTx.setLocktime(locktime); + } else { + unlockTx.setSequence(0, 0); + } + + unlockTx.addOutput({ + address: ring.getAddress(), + value: val, + }); + + // Generate new script: + const unlockScript = new Script(); + unlockScript.pushData(unlockTx.signature(0, redeemScript, val, ring.getPrivateKey(), Script.hashType.ALL, 0)); + unlockScript.pushInt(boolVal); + unlockScript.pushData(redeemScript.toRaw()); + unlockScript.compile(); + + unlockTx.inputs[0].script = unlockScript; + + // Compute a fee by multiplying the size by the rate, then account for it + const virtSize = unlockTx.getVirtualSize(); + const fee = Math.ceil(virtSize / 1000 * feeRate); + unlockTx.subtractFee(fee); + + // Remake script with the new signature + const unlockScript2 = new Script(); + unlockScript2.pushData(unlockTx.signature(0, redeemScript, val, ring.getPrivateKey(), Script.hashType.ALL, 0)); + unlockScript2.pushInt(boolVal); + unlockScript2.pushData(redeemScript.toRaw()); + unlockScript2.compile(); + + unlockTx.inputs[0].script = unlockScript2; + + return unlockTx.toTX(); +} + +export { + genLockTx, + genUnlockTx, + genRedeemScript, + genP2shAddr, + getLockTxName, + getLockTxTime, + getLockTxPubKey, + genCommitTx, + genCommitRedeemScript, + serializeCommitData, + deserializeCommitData, +}; \ No newline at end of file From 71e0b31ae212f509aedeb42be267bfe4f1dc9f9c Mon Sep 17 00:00:00 2001 From: JWood323 Date: Tue, 20 Mar 2018 13:28:22 -0400 Subject: [PATCH 06/41] added imports and exports to tx-commit --- lib/tx-commit.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/tx-commit.ts b/lib/tx-commit.ts index a3e6774..c71f2cb 100644 --- a/lib/tx-commit.ts +++ b/lib/tx-commit.ts @@ -1,3 +1,11 @@ +import{ + coin as Coin, + keyring as KeyRing, + crypto, +} from 'bcoin'; + + + /** * Generate a commit transaction. * @param coins Array of coins to fund the transaction. @@ -94,4 +102,8 @@ function genCommitTx(coins: Coin[], } return lockTx.toTX(); -} \ No newline at end of file + +} +export { + genCommitTx +}; \ No newline at end of file From de7a55c2ad1cca8bbb9c743441e5b6bbec983a0b Mon Sep 17 00:00:00 2001 From: Henry G Date: Tue, 20 Mar 2018 13:28:26 -0400 Subject: [PATCH 07/41] finalized imports for genscripts file --- lib/GenScripts.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/GenScripts.ts b/lib/GenScripts.ts index c531a85..63ff234 100644 --- a/lib/GenScripts.ts +++ b/lib/GenScripts.ts @@ -8,6 +8,10 @@ import { BadServicePublicKeyError, } from './errors'; +import { + serializeCommitData, +} from './txs'; + /** * Generate a redeem script, removing a name/key pair from the blockchain. * Validates `userPubkey` and `servicePubkey`. From 828ea7f37319e6cf942739f112f4e36e3c6e8c8c Mon Sep 17 00:00:00 2001 From: Henry G Date: Tue, 20 Mar 2018 13:30:43 -0400 Subject: [PATCH 08/41] rename to preserve consistency --- lib/{GenScripts.ts => tx-generate.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename lib/{GenScripts.ts => tx-generate.ts} (100%) diff --git a/lib/GenScripts.ts b/lib/tx-generate.ts similarity index 100% rename from lib/GenScripts.ts rename to lib/tx-generate.ts From 16c1b665e2414076828641f841722bdade02d5bb Mon Sep 17 00:00:00 2001 From: JWood323 Date: Tue, 20 Mar 2018 13:45:51 -0400 Subject: [PATCH 09/41] added changed imports from henrys file --- lib/tx-commit.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/tx-commit.ts b/lib/tx-commit.ts index c71f2cb..5e48c21 100644 --- a/lib/tx-commit.ts +++ b/lib/tx-commit.ts @@ -4,6 +4,9 @@ import{ crypto, } from 'bcoin'; +import{ + genCommitRedeemScript, +} from './tx-generate'; /** From b69c27592449c65cedae6a1694a46fc3758ecab4 Mon Sep 17 00:00:00 2001 From: JWood323 Date: Tue, 20 Mar 2018 13:46:13 -0400 Subject: [PATCH 10/41] added imports and exports --- lib/tx-lock.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/tx-lock.ts b/lib/tx-lock.ts index fc1fa88..ef46639 100644 --- a/lib/tx-lock.ts +++ b/lib/tx-lock.ts @@ -1,3 +1,14 @@ +import{ + genRedeemScript, + genCommitRedeemScript, +} from './tx-generate'; + +import{ + tx as TX, + keyring as KeyRing, + crypto +} from 'bcoin'; + /** * Generate a lock transaction. * @param commitTX The corresponding commit transaction. @@ -105,4 +116,7 @@ function genLockTx(commitTX: TX, unlockScript.compile(); return lockTx.toTX(); -} \ No newline at end of file +} +export{ + genLockTx +}; \ No newline at end of file From 3660c3b438b771299c5d41388c554a6370c4b0b0 Mon Sep 17 00:00:00 2001 From: Henry G Date: Tue, 20 Mar 2018 13:46:48 -0400 Subject: [PATCH 11/41] updated gitignore bc its annoying --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index d5790d3..e89c592 100644 --- a/.gitignore +++ b/.gitignore @@ -67,3 +67,4 @@ typings/ # dotenv environment variables file .env +package-lock.json From 52db6088c9704a2ae12c1d933367b6e10be4b3f0 Mon Sep 17 00:00:00 2001 From: JWood323 Date: Tue, 20 Mar 2018 13:46:55 -0400 Subject: [PATCH 12/41] added imports and exports --- lib/tx-unlock.ts | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/lib/tx-unlock.ts b/lib/tx-unlock.ts index 342f53f..f944f66 100644 --- a/lib/tx-unlock.ts +++ b/lib/tx-unlock.ts @@ -1,3 +1,15 @@ +import{ + tx as TX, + keyring as KeyRing, + crypto +} from 'bcoin'; + +import{ + genRedeemScript, + genCommitRedeemScript, +} from './tx-generate'; + + /** * Generate an unlock transaction. * @param lockTx The corresponding lock transaction. @@ -81,15 +93,6 @@ function genUnlockTx(lockTx: TX, } export { - genLockTx, + genUnlockTx, - genRedeemScript, - genP2shAddr, - getLockTxName, - getLockTxTime, - getLockTxPubKey, - genCommitTx, - genCommitRedeemScript, - serializeCommitData, - deserializeCommitData, }; \ No newline at end of file From 9b782ff6906ae411f90ea82e5306000fc20e9431 Mon Sep 17 00:00:00 2001 From: Henry G Date: Tue, 20 Mar 2018 14:00:03 -0400 Subject: [PATCH 13/41] small fix to tx-generate --- lib/txs.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/txs.ts b/lib/txs.ts index f466ece..896d85f 100644 --- a/lib/txs.ts +++ b/lib/txs.ts @@ -16,7 +16,7 @@ import randomBytes = require('randombytes'); import { genRedeemScript, genCommitRedeemScript, -} from GenScripts; +} from './tx-generate'; import { BadUserPublicKeyError, From c2b1b3d70c660d6441e435cf71553ad59c2c205a Mon Sep 17 00:00:00 2001 From: JWood323 Date: Tue, 20 Mar 2018 16:10:21 -0400 Subject: [PATCH 14/41] edited tests to account for new files --- __tests__/chain.test.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/__tests__/chain.test.ts b/__tests__/chain.test.ts index eb0c4b6..26d1798 100644 --- a/__tests__/chain.test.ts +++ b/__tests__/chain.test.ts @@ -9,7 +9,9 @@ import { fundTx, getAllTX } from '../lib/net'; import { extractInfo } from '../lib/chain'; import TXList from '../lib/TXList'; -import { genCommitTx, genLockTx, genUnlockTx } from '../lib/txs'; +import genCommitTx from '../lib/tx-commit.ts'; +import genLockTx from '../lib/tx-lock.ts' +import genUnlockTx from '../lib/tx-unlock.ts' describe('chain state', () => { it('finds the one current name', async () => { From cac98344eec1cefbe5767fc39ada2e0543fe7351 Mon Sep 17 00:00:00 2001 From: Henry G Date: Tue, 20 Mar 2018 16:16:11 -0400 Subject: [PATCH 15/41] updated txs tests file for refactored functions --- __tests__/txs.test.ts | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/__tests__/txs.test.ts b/__tests__/txs.test.ts index 585bd73..24057bd 100644 --- a/__tests__/txs.test.ts +++ b/__tests__/txs.test.ts @@ -8,14 +8,28 @@ import { } from 'bcoin'; import { - genRedeemScript, - genLockTx, - genUnlockTx, - genCommitTx, - genCommitRedeemScript, serializeCommitData, deserializeCommitData, } from '../lib/txs'; + +import { + genUnlockTx, +} from '../lib/tx-unlock'; + +import { + genLockTx, +} from '../lib/tx-lock'; + +import { + genCommitTx, +} from '../lib/tx-commit'; + + +import { + genRedeemScript, + genCommitRedeemScript, +} from '../lib/tx-generate'; + import { BadUserPublicKeyError, BadServicePublicKeyError, From ca9e05f0ce2055e3aef8b25f71a4f392f4e1b1d5 Mon Sep 17 00:00:00 2001 From: JWood323 Date: Tue, 20 Mar 2018 16:17:40 -0400 Subject: [PATCH 16/41] edited tests to account for new files --- __tests__/verify.test.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/__tests__/verify.test.ts b/__tests__/verify.test.ts index 5a7a85a..c925259 100644 --- a/__tests__/verify.test.ts +++ b/__tests__/verify.test.ts @@ -1,15 +1,18 @@ import { verifyLockTX, verifyCommitTX } from '../lib/verify'; import { - genRedeemScript, genP2shAddr, - genLockTx, getLockTxName, getLockTxPubKey, getLockTxTime, - genCommitTx, serializeCommitData, deserializeCommitData, } from '../lib/txs'; +import{ + genRedeemScript, + genCommitRedeemScript, +} from '../lib/tx-generate'; +import genCommitTx from '../lib/tx-commit'; +import genLockTx from '../lib/tx-lock'; import { keyring as KeyRing, coin as Coin, From 2b37bbda4e1b90abd7d83cdc971349bf20e6b0c1 Mon Sep 17 00:00:00 2001 From: JWood323 Date: Tue, 20 Mar 2018 16:26:08 -0400 Subject: [PATCH 17/41] Removed functions and exports in txs.ts --- lib/txs.ts | 287 --------------------------------------------------- package.json | 3 +- yarn.lock | 4 + 3 files changed, 6 insertions(+), 288 deletions(-) diff --git a/lib/txs.ts b/lib/txs.ts index 896d85f..511b097 100644 --- a/lib/txs.ts +++ b/lib/txs.ts @@ -111,212 +111,7 @@ function deserializeCommitData(data: Buffer): ICommitData { }; } -/** - * Generate a commit transaction. - * @param coins Array of coins to fund the transaction. - * @param name Name for name/key pair. - * @param locktime Absolute locktime - * @param commitFee Commit fee, in satoshis. - * @param registerFee Registration fee, in satoshis. - * @param escrowFee Escrow fee, in satoshis. - * @param feeRate Fee rate, in satoshis/kilobyte. - * @param userRing The user's key ring. - * @param servicePubKey Service public key. - */ -function genCommitTx(coins: Coin[], - name: string, - locktime: number, - commitFee: number, - registerFee: number, - escrowFee: number, - feeRate: number, - userRing: KeyRing, - servicePubKey: Buffer): TX { - // - // Validate name and the service public key - if (name.length > 64) { - throw new Error('Name is too long'); - } - - if (!isURISafe(name)) { - throw new Error('Invalid character(s) in name'); - } - - if (!crypto.secp256k1.publicKeyVerify(servicePubKey)) { - throw new Error('Invalid service public key'); - } - - // Generate a P2SH address from a redeem script, using a random nonce - const nonce = randomBytes(32); - const redeemScript = genCommitRedeemScript(userRing.getPublicKey(), nonce, name, locktime); - const p2shAddr = genP2shAddr(redeemScript); - - // Generate service address from service public key - const servicePKH = crypto.hash160(servicePubKey); - const serviceAddr = Address.fromPubkeyhash(servicePKH); - - const lockTx = new MTX(); - - // Compute total value of coins - const total = coins.reduce((acc, cur) => acc + cur.value, 0); - - for (const coin of coins) { - lockTx.addCoin(coin); - } - - // Compute change amount - const changeVal = total - (commitFee + registerFee + escrowFee) - (4 * feeRate); - - // Add nonce OP_RETURN as output 0 - const pubkeyDataScript = Script.fromNulldata(nonce); - lockTx.addOutput(Output.fromScript(pubkeyDataScript, 0)); - - // Add service upfront fee as output 1 - lockTx.addOutput({ - address: serviceAddr, - value: commitFee, - }); - - // Add locked fee as output 2 - // Locks up the fee to register, the fee to be put in escrow, and enough for a 4kb tx at current rates - lockTx.addOutput({ - address: p2shAddr, - value: registerFee + escrowFee + 4 * feeRate, - }); - - // Add change output as 3 - lockTx.addOutput({ - address: userRing.getAddress(), - value: changeVal, - }); - - // Add coins as inputs - for (let i = 0; i < coins.length; ++i) { - const coin = coins[i]; - lockTx.scriptInput(i, coin, userRing); - } - - // Each signature is 72 bytes long - const virtSize = lockTx.getVirtualSize() + coins.length * 72; - lockTx.subtractIndex(3, Math.ceil(virtSize / 1000 * feeRate)); - - // Sign the coins - for (let i = 0; i < coins.length; ++i) { - const coin = coins[i]; - lockTx.signInput(i, coin, userRing, Script.hashType.ALL); - } - - return lockTx.toTX(); -} - -/** - * Generate a lock transaction. - * @param commitTX The corresponding commit transaction. - * @param name The name to use. - * @param upfrontFee The upfront fee in satoshis to use the service, as - * determined by the service. - * @param lockedFee Fee incentivizing registrar to provide service, as - * determined by the service. - * @param feeRate Fee rate in satoshi/KB. - * @param userRing The user key ring. - * @param servicePubKey Service public key. - * @param locktime Absolute lock time in blocks. - */ -function genLockTx(commitTX: TX, - name: string, - upfrontFee: number, - lockedFee: number, - feeRate: number, - userRing: KeyRing, - servicePubKey: Buffer, - locktime: number): TX { - // - // Input validation - if (locktime > 500000000) { - throw new Error('Locktime must be less than 500000000 blocks'); - } - - if (name.length > 64) { - throw new Error('Name is too long'); - } - - if (!isURISafe(name)) { - throw new Error('Invalid character(s) in name'); - } - - if (!crypto.secp256k1.publicKeyVerify(servicePubKey)) { - throw new Error('Invalid service public key'); - } - - if (!verifyCommitTX(commitTX, userRing.getPublicKey(), servicePubKey, name, locktime)) { - throw new Error('Invalid commitment tx'); - } - // Generate a P2SH address from redeem script - const redeemScript = genRedeemScript(userRing.getPublicKey(), servicePubKey, locktime); - const p2shAddr = genP2shAddr(redeemScript); - - // Generate address from service public key - const servicePKH = crypto.hash160(servicePubKey); - const serviceAddr = Address.fromPubkeyhash(servicePKH); - - const lockTx = MTX.fromOptions({ - version: 2, - }); - - lockTx.addTX(commitTX, 2); - - lockTx.setSequence(0, 6); - - const total = commitTX.outputs[2].value; - - const changeVal = total - upfrontFee - lockedFee; - - // Add upfront fee as output 0 - lockTx.addOutput({ - address: serviceAddr, - value: upfrontFee, - }); - - // Add locked fee as output 1 - lockTx.addOutput({ - address: p2shAddr, - value: lockedFee, - }); - - // Add change output as 2 - lockTx.addOutput({ - address: userRing.getAddress(), - value: changeVal, - }); - - const nonce = commitTX.outputs[0].script.code[1].data; - - const hashData = serializeCommitData(nonce, locktime, name); - - const commitRedeemScript = genCommitRedeemScript(userRing.getPublicKey(), nonce, name, locktime); - - const unlockScript = new Script(); - unlockScript.pushData(hashData); - unlockScript.pushData(commitRedeemScript.toRaw()); - unlockScript.compile(); - - lockTx.inputs[0].script = unlockScript; - - // Add constant for signature - const virtSize = lockTx.getVirtualSize() + 72; - - // Calculate fee to be paid - const fee = Math.ceil(virtSize / 1000 * feeRate); - lockTx.subtractIndex(2, fee); - - // Add signature - const sig = lockTx.signature(0, commitRedeemScript, total, userRing.getPrivateKey(), Script.hashType.ALL, 0); - unlockScript.insertData(0, sig); - unlockScript.compile(); - - return lockTx.toTX(); -} /** * Extract metadata from a script (i.e. the nonce, locktime, and name). @@ -381,96 +176,14 @@ function getLockTxPubKey(lockTx: TX): Buffer | null { return pubKey; } -/** - * Generate an unlock transaction. - * @param lockTx The corresponding lock transaction. - * @param commitTx The corresponding commit transaction. - * @param feeRate The fee rate in satoshi/KB. - * @param service Whether to use the script as service. - * @param ring The service key ring if `service`, otherwise user key ring. - * @param otherPubKey The user key ring if `service`, otherwise service key ring. - */ -function genUnlockTx(lockTx: TX, - commitTx: TX, - feeRate: number, - service: boolean, - ring: KeyRing, - otherPubKey: Buffer): TX { - // Disambiguate ring public key and the other public key - const servicePubKey = service ? ring.getPublicKey() : otherPubKey; - const userPubKey = !service ? ring.getPublicKey() : otherPubKey; - - // - // Input validation - if (!verifyLockTX(lockTx, commitTx, servicePubKey)) { - throw new BadLockTransactionError(); - } - - if (!crypto.secp256k1.publicKeyVerify(otherPubKey)) { - throw new Error('Invalid service public key'); - } - - const locktime = getLockTxTime(lockTx); - if (locktime === null) { - throw new Error('Could not extract locktime'); - } - - const redeemScript = genRedeemScript(userPubKey, servicePubKey, locktime); - - const val = lockTx.outputs[1].value; // the P2SH output - const unlockTx = MTX.fromOptions({ - version: 2, - }); - - unlockTx.addTX(lockTx, 1); - - const boolVal = service ? 0 : 1; - - if (service) { - unlockTx.setLocktime(locktime); - } else { - unlockTx.setSequence(0, 0); - } - - unlockTx.addOutput({ - address: ring.getAddress(), - value: val, - }); - - // Generate new script: - const unlockScript = new Script(); - unlockScript.pushData(unlockTx.signature(0, redeemScript, val, ring.getPrivateKey(), Script.hashType.ALL, 0)); - unlockScript.pushInt(boolVal); - unlockScript.pushData(redeemScript.toRaw()); - unlockScript.compile(); - - unlockTx.inputs[0].script = unlockScript; - - // Compute a fee by multiplying the size by the rate, then account for it - const virtSize = unlockTx.getVirtualSize(); - const fee = Math.ceil(virtSize / 1000 * feeRate); - unlockTx.subtractFee(fee); - - // Remake script with the new signature - const unlockScript2 = new Script(); - unlockScript2.pushData(unlockTx.signature(0, redeemScript, val, ring.getPrivateKey(), Script.hashType.ALL, 0)); - unlockScript2.pushInt(boolVal); - unlockScript2.pushData(redeemScript.toRaw()); - unlockScript2.compile(); - - unlockTx.inputs[0].script = unlockScript2; - return unlockTx.toTX(); } export { - genLockTx, - genUnlockTx, genP2shAddr, getLockTxName, getLockTxTime, getLockTxPubKey, - genCommitTx, serializeCommitData, deserializeCommitData, }; diff --git a/package.json b/package.json index cb36456..787f6bd 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,8 @@ "n64": "^0.0.18", "node-fetch": "^1.7.3", "randombytes": "^2.0.5", - "yargs": "^10.0.3" + "yargs": "^10.0.3", + "yarn": "^1.5.1" }, "scripts": { "start:dev": "ts-node index.ts", diff --git a/yarn.lock b/yarn.lock index b9a705e..4132ef6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3569,6 +3569,10 @@ yargs@~3.10.0: decamelize "^1.0.0" window-size "0.1.0" +yarn@^1.5.1: + version "1.5.1" + resolved "https://registry.yarnpkg.com/yarn/-/yarn-1.5.1.tgz#e8680360e832ac89521eb80dad3a7bc27a40bab4" + yeast@0.1.2: version "0.1.2" resolved "https://registry.yarnpkg.com/yeast/-/yeast-0.1.2.tgz#008e06d8094320c372dbc2f8ed76a0ca6c8ac419" From a305087997f276d3f688693aa9fe5ab2fd0a931f Mon Sep 17 00:00:00 2001 From: Henry G Date: Tue, 20 Mar 2018 16:34:07 -0400 Subject: [PATCH 18/41] few fixes --- __tests__/chain.test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/__tests__/chain.test.ts b/__tests__/chain.test.ts index 26d1798..2cc57a3 100644 --- a/__tests__/chain.test.ts +++ b/__tests__/chain.test.ts @@ -8,10 +8,10 @@ import { fundTx, getAllTX } from '../lib/net'; import { extractInfo } from '../lib/chain'; -import TXList from '../lib/TXList'; -import genCommitTx from '../lib/tx-commit.ts'; -import genLockTx from '../lib/tx-lock.ts' -import genUnlockTx from '../lib/tx-unlock.ts' +import { TXList } from '../lib/TXList'; +import { genCommitTx } from '../lib/tx-commit.ts'; +import { genLockTx } from '../lib/tx-lock.ts'; +import { genUnlockTx } from '../lib/tx-unlock.ts'; describe('chain state', () => { it('finds the one current name', async () => { From 39438ad934a442414f59e8d44b9944002ed66eca Mon Sep 17 00:00:00 2001 From: Henry G Date: Tue, 27 Mar 2018 16:39:13 -0400 Subject: [PATCH 19/41] fixed error it txs --- lib/txs.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/txs.ts b/lib/txs.ts index 511b097..149aa6b 100644 --- a/lib/txs.ts +++ b/lib/txs.ts @@ -29,7 +29,7 @@ import { verifyLockTX, isURISafe, verifyCommitTX } from './verify'; /** * Generate a P2SH address from a redeem script. * @param redeemScript The script to use. - */register + */ function genP2shAddr(redeemScript: Script): Address { const outputScript = Script.fromScripthash(redeemScript.hash160()); const p2shAddr = Address.fromScript(outputScript); From 3437192067eb1766d665b5ec905928ac129ffa25 Mon Sep 17 00:00:00 2001 From: JWood323 Date: Tue, 27 Mar 2018 16:39:24 -0400 Subject: [PATCH 20/41] missed importing a few functions --- lib/tx-commit.ts | 4 ++++ lib/tx-lock.ts | 5 +++++ lib/tx-unlock.ts | 4 ++++ 3 files changed, 13 insertions(+) diff --git a/lib/tx-commit.ts b/lib/tx-commit.ts index 5e48c21..b705d99 100644 --- a/lib/tx-commit.ts +++ b/lib/tx-commit.ts @@ -8,6 +8,10 @@ import{ genCommitRedeemScript, } from './tx-generate'; +import{ + genP2shAddr, +} from './txs.ts'; + /** * Generate a commit transaction. diff --git a/lib/tx-lock.ts b/lib/tx-lock.ts index ef46639..6c56b53 100644 --- a/lib/tx-lock.ts +++ b/lib/tx-lock.ts @@ -3,6 +3,11 @@ import{ genCommitRedeemScript, } from './tx-generate'; +import { + genP2shAddr, + serializeCommitData, +} from './txs.ts'; + import{ tx as TX, keyring as KeyRing, diff --git a/lib/tx-unlock.ts b/lib/tx-unlock.ts index f944f66..60a04d9 100644 --- a/lib/tx-unlock.ts +++ b/lib/tx-unlock.ts @@ -4,6 +4,10 @@ import{ crypto } from 'bcoin'; +import{ + getLockTxTime, +} from './txs.ts'; + import{ genRedeemScript, genCommitRedeemScript, From da7bce5e6d3ab737762caa1839c57d5a8387fbf2 Mon Sep 17 00:00:00 2001 From: Henry G Date: Tue, 27 Mar 2018 16:43:28 -0400 Subject: [PATCH 21/41] moved more imports --- lib/verify.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/verify.ts b/lib/verify.ts index 7dd07e4..def261b 100644 --- a/lib/verify.ts +++ b/lib/verify.ts @@ -5,12 +5,14 @@ import { crypto, } from 'bcoin'; import { - genRedeemScript, - genCommitRedeemScript, getLockTxName, getLockTxTime, getLockTxPubKey, } from './txs'; +import { + genRedeemScript, + genCommitRedeemScript, +} from './tx-generate'; /** * Returns a Boolean value that indicates if URI has and only has alphanumeric leteral and '_', '-', '.' '~' From b2c35db91a4ac2f8f30d0e0587f94b5cc068e73c Mon Sep 17 00:00:00 2001 From: JWood323 Date: Tue, 27 Mar 2018 16:45:36 -0400 Subject: [PATCH 22/41] adding more imports --- lib/tx-commit.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tx-commit.ts b/lib/tx-commit.ts index b705d99..b738d6a 100644 --- a/lib/tx-commit.ts +++ b/lib/tx-commit.ts @@ -12,7 +12,7 @@ import{ genP2shAddr, } from './txs.ts'; - +import { verifyLockTX, isURISafe, verifyCommitTX } from './verify'; /** * Generate a commit transaction. * @param coins Array of coins to fund the transaction. From 2152b9f72df59cb7e265c949031e6e21590ec61d Mon Sep 17 00:00:00 2001 From: JWood323 Date: Tue, 27 Mar 2018 16:47:17 -0400 Subject: [PATCH 23/41] adding more imports --- lib/tx-commit.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/tx-commit.ts b/lib/tx-commit.ts index b738d6a..c71ee3c 100644 --- a/lib/tx-commit.ts +++ b/lib/tx-commit.ts @@ -13,6 +13,9 @@ import{ } from './txs.ts'; import { verifyLockTX, isURISafe, verifyCommitTX } from './verify'; + +import randomBytes = require('randombytes'); + /** * Generate a commit transaction. * @param coins Array of coins to fund the transaction. From 651400de4265b46dc0052d4e29cfb33e85125858 Mon Sep 17 00:00:00 2001 From: JWood323 Date: Tue, 27 Mar 2018 16:48:49 -0400 Subject: [PATCH 24/41] adding more imports --- lib/tx-commit.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/tx-commit.ts b/lib/tx-commit.ts index c71ee3c..e9941e4 100644 --- a/lib/tx-commit.ts +++ b/lib/tx-commit.ts @@ -1,5 +1,10 @@ import{ + script as Script, + address as Address, + output as Output, + mtx as MTX, coin as Coin, + tx as TX, keyring as KeyRing, crypto, } from 'bcoin'; From 4b9d9285d4ed4825267721dd067a30ce3e0f4b23 Mon Sep 17 00:00:00 2001 From: JWood323 Date: Tue, 27 Mar 2018 16:51:51 -0400 Subject: [PATCH 25/41] adding more imports --- lib/tx-lock.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/tx-lock.ts b/lib/tx-lock.ts index 6c56b53..12eed74 100644 --- a/lib/tx-lock.ts +++ b/lib/tx-lock.ts @@ -9,11 +9,18 @@ import { } from './txs.ts'; import{ + script as Script, + address as Address, + output as Output, + mtx as MTX, + coin as Coin, tx as TX, keyring as KeyRing, - crypto + crypto, } from 'bcoin'; +import { verifyLockTX, isURISafe, verifyCommitTX } from './verify'; + /** * Generate a lock transaction. * @param commitTX The corresponding commit transaction. From 84ee7e77ea13215e484a8b0919d33d1f39cd905f Mon Sep 17 00:00:00 2001 From: JWood323 Date: Tue, 27 Mar 2018 16:54:53 -0400 Subject: [PATCH 26/41] adding more imports --- lib/tx-unlock.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/tx-unlock.ts b/lib/tx-unlock.ts index 60a04d9..f852d88 100644 --- a/lib/tx-unlock.ts +++ b/lib/tx-unlock.ts @@ -13,6 +13,8 @@ import{ genCommitRedeemScript, } from './tx-generate'; +import { verifyLockTX, isURISafe, verifyCommitTX } from './verify'; + /** * Generate an unlock transaction. From 1964d550bb39908210b6dc64f650bea2e9deae50 Mon Sep 17 00:00:00 2001 From: JWood323 Date: Tue, 27 Mar 2018 17:20:53 -0400 Subject: [PATCH 27/41] fixed errors and builds and runs correctly --- __tests__/chain.test.ts | 2 +- __tests__/verify.test.ts | 4 ++-- lib/tx-unlock.ts | 12 +++++++++++- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/__tests__/chain.test.ts b/__tests__/chain.test.ts index 2cc57a3..54d37d2 100644 --- a/__tests__/chain.test.ts +++ b/__tests__/chain.test.ts @@ -8,7 +8,7 @@ import { fundTx, getAllTX } from '../lib/net'; import { extractInfo } from '../lib/chain'; -import { TXList } from '../lib/TXList'; +import TXList from '../lib/TXList'; import { genCommitTx } from '../lib/tx-commit.ts'; import { genLockTx } from '../lib/tx-lock.ts'; import { genUnlockTx } from '../lib/tx-unlock.ts'; diff --git a/__tests__/verify.test.ts b/__tests__/verify.test.ts index c925259..3fe4a7e 100644 --- a/__tests__/verify.test.ts +++ b/__tests__/verify.test.ts @@ -11,8 +11,8 @@ import{ genRedeemScript, genCommitRedeemScript, } from '../lib/tx-generate'; -import genCommitTx from '../lib/tx-commit'; -import genLockTx from '../lib/tx-lock'; +import { genCommitTx } from '../lib/tx-commit'; +import { genLockTx } from '../lib/tx-lock'; import { keyring as KeyRing, coin as Coin, diff --git a/lib/tx-unlock.ts b/lib/tx-unlock.ts index f852d88..4053c0c 100644 --- a/lib/tx-unlock.ts +++ b/lib/tx-unlock.ts @@ -1,7 +1,12 @@ import{ + script as Script, + address as Address, + output as Output, + mtx as MTX, + coin as Coin, tx as TX, keyring as KeyRing, - crypto + crypto, } from 'bcoin'; import{ @@ -15,6 +20,11 @@ import{ import { verifyLockTX, isURISafe, verifyCommitTX } from './verify'; +import { + BadUserPublicKeyError, + BadServicePublicKeyError, + BadLockTransactionError, +} from './errors'; /** * Generate an unlock transaction. From 0756d5e82bbb546ec061c604dfefa5a6c19cc214 Mon Sep 17 00:00:00 2001 From: hgrov52 Date: Thu, 5 Apr 2018 13:25:35 -0400 Subject: [PATCH 28/41] Update bitname-cli.ts --- bin/bitname-cli.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/bitname-cli.ts b/bin/bitname-cli.ts index 151ef7b..646b64b 100644 --- a/bin/bitname-cli.ts +++ b/bin/bitname-cli.ts @@ -7,7 +7,8 @@ import { crypto, util, } from 'bcoin'; -import { genLockTx, genUnlockTx, genCommitTx, getLockTxPubKey } from '../lib/txs'; +import { genUnlockTx, genCommitTx, getLockTxPubKey } from '../lib/txs'; +import { genLockTx } from '../lib/tx-lock.ts import { fundTx, getFeesSatoshiPerKB, getAllTX, getBlockHeight, getTX, postTX } from '../lib/net'; import { extractInfo } from '../lib/chain'; @@ -68,7 +69,6 @@ async function commit(argv: yargs.Arguments) { // const upfrontFee = 500000; // const delayFee = 1500000; - const commitFee = 500000; const registerFee = 500000; const escrowFee = 1000000; From b6e5644c58c15eae79787bcaed1f574aef09f529 Mon Sep 17 00:00:00 2001 From: Henry Grover Date: Wed, 25 Apr 2018 18:47:07 -0400 Subject: [PATCH 29/41] syntax error --- bin/bitname-cli.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/bitname-cli.ts b/bin/bitname-cli.ts index 47fe0db..46fd5ce 100644 --- a/bin/bitname-cli.ts +++ b/bin/bitname-cli.ts @@ -8,7 +8,7 @@ import { util, } from 'bcoin'; import { genUnlockTx, genCommitTx, getLockTxPubKey } from '../lib/txs'; -import { genLockTx } from '../lib/tx-lock.ts +import { genLockTx } from '../lib/tx-lock.ts'; import { fundTx, getFeesSatoshiPerKB, getAllTX, getBlockHeight, getTX, postTX } from '../lib/net'; import { extractInfo } from '../lib/chain'; From 9586dc30aff1053726cf1687a6a297d9c122e0bb Mon Sep 17 00:00:00 2001 From: Henry Grover Date: Wed, 25 Apr 2018 18:52:25 -0400 Subject: [PATCH 30/41] fixes for travis --- bin/bitname-cli.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bin/bitname-cli.ts b/bin/bitname-cli.ts index c1e85f2..aec97aa 100644 --- a/bin/bitname-cli.ts +++ b/bin/bitname-cli.ts @@ -7,7 +7,9 @@ import { crypto, util, } from 'bcoin'; -import { genUnlockTx, genCommitTx, getLockTxPubKey } from '../lib/txs'; +import { genUnlockTx } from '../lib/tx-unlock.ts'; +import { genCommitTx } from '../lib/tx-commit.ts'; +import { getLockTxPubKey } from '../lib/txs'; import { genLockTx } from '../lib/tx-lock.ts'; import { fundTx, getFeesSatoshiPerKB, getAllTX, getBlockHeight, getTX, postTX } from '../lib/net'; import { extractInfo } from '../lib/chain'; From ec46f5aba9e2cd384f40e858afd01f41145239d2 Mon Sep 17 00:00:00 2001 From: Henry Grover Date: Wed, 25 Apr 2018 18:55:16 -0400 Subject: [PATCH 31/41] fixes for travis --- bin/bitname-cli.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bin/bitname-cli.ts b/bin/bitname-cli.ts index aec97aa..e0a273c 100644 --- a/bin/bitname-cli.ts +++ b/bin/bitname-cli.ts @@ -7,10 +7,10 @@ import { crypto, util, } from 'bcoin'; -import { genUnlockTx } from '../lib/tx-unlock.ts'; -import { genCommitTx } from '../lib/tx-commit.ts'; +import { genUnlockTx } from '../lib/tx-unlock'; +import { genCommitTx } from '../lib/tx-commit'; import { getLockTxPubKey } from '../lib/txs'; -import { genLockTx } from '../lib/tx-lock.ts'; +import { genLockTx } from '../lib/tx-lock'; import { fundTx, getFeesSatoshiPerKB, getAllTX, getBlockHeight, getTX, postTX } from '../lib/net'; import { extractInfo } from '../lib/chain'; From 4b3b9376dc01eb13ac0462f324146cce7da720da Mon Sep 17 00:00:00 2001 From: Henry Grover Date: Wed, 25 Apr 2018 18:58:52 -0400 Subject: [PATCH 32/41] fixes for travis --- lib/tx-unlock.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tx-unlock.ts b/lib/tx-unlock.ts index 4053c0c..0ed1c21 100644 --- a/lib/tx-unlock.ts +++ b/lib/tx-unlock.ts @@ -11,7 +11,7 @@ import{ import{ getLockTxTime, -} from './txs.ts'; +} from './txs'; import{ genRedeemScript, From 912d742ca81d9e1bc42d57d8aec8c368708ce548 Mon Sep 17 00:00:00 2001 From: Henry Grover Date: Wed, 25 Apr 2018 19:01:46 -0400 Subject: [PATCH 33/41] fixes for travis --- lib/tx-lock.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tx-lock.ts b/lib/tx-lock.ts index 12eed74..42874c6 100644 --- a/lib/tx-lock.ts +++ b/lib/tx-lock.ts @@ -6,7 +6,7 @@ import{ import { genP2shAddr, serializeCommitData, -} from './txs.ts'; +} from './txs'; import{ script as Script, From c335240daa061529398b68b3e657189cadfe5efb Mon Sep 17 00:00:00 2001 From: Henry Grover Date: Wed, 25 Apr 2018 19:07:08 -0400 Subject: [PATCH 34/41] fixes for travis --- index.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/index.ts b/index.ts index c5e3626..f22f721 100644 --- a/index.ts +++ b/index.ts @@ -1,5 +1,13 @@ export { genCommitTx, - genLockTx, - genUnlockTx, -} from './lib/txs'; + +} from './lib/tx-commit'; +export { + genLockTx, +} from './lib/tx-lock'; +export { + genUnlockTx, +} from './lib/tx-unlock'; + + + From c9ff931fb2855ef61045673816873bd6ec5c75c5 Mon Sep 17 00:00:00 2001 From: Henry Grover Date: Wed, 25 Apr 2018 19:18:39 -0400 Subject: [PATCH 35/41] fixes for travis --- lib/tx-unlock.ts | 14 +++++--------- lib/txs.ts | 20 -------------------- 2 files changed, 5 insertions(+), 29 deletions(-) diff --git a/lib/tx-unlock.ts b/lib/tx-unlock.ts index 0ed1c21..d619a54 100644 --- a/lib/tx-unlock.ts +++ b/lib/tx-unlock.ts @@ -1,4 +1,4 @@ -import{ +import { script as Script, address as Address, output as Output, @@ -9,20 +9,17 @@ import{ crypto, } from 'bcoin'; -import{ +import { getLockTxTime, } from './txs'; -import{ +import { genRedeemScript, - genCommitRedeemScript, } from './tx-generate'; -import { verifyLockTX, isURISafe, verifyCommitTX } from './verify'; +import { verifyLockTX } from './verify'; import { - BadUserPublicKeyError, - BadServicePublicKeyError, BadLockTransactionError, } from './errors'; @@ -109,6 +106,5 @@ function genUnlockTx(lockTx: TX, } export { - genUnlockTx, -}; \ No newline at end of file +}; diff --git a/lib/txs.ts b/lib/txs.ts index 0a7941c..87b6271 100644 --- a/lib/txs.ts +++ b/lib/txs.ts @@ -1,29 +1,9 @@ import { script as Script, address as Address, - output as Output, - mtx as MTX, - coin as Coin, tx as TX, - keyring as KeyRing, - crypto, } from 'bcoin'; -import randomBytes from 'randombytes'; - -import { - genRedeemScript, - genCommitRedeemScript, -} from './tx-generate'; - -import { - BadUserPublicKeyError, - BadServicePublicKeyError, - BadLockTransactionError, -} from './errors'; - -import { verifyLockTX, isURISafe, verifyCommitTX } from './verify'; - /** * Generate a P2SH address from a redeem script. * @param redeemScript The script to use. From 0d9b384d9233d8e6f610a1d154bfc002e0bba95a Mon Sep 17 00:00:00 2001 From: Henry Grover Date: Wed, 25 Apr 2018 19:24:17 -0400 Subject: [PATCH 36/41] fixes for travis --- __tests__/chain.test.ts | 4 ++-- __tests__/txs.test.ts | 1 - __tests__/verify.test.ts | 4 ---- index.ts | 4 ---- lib/tx-commit.ts | 2 +- lib/tx-generate.ts | 4 ++-- lib/tx-lock.ts | 18 ++++++++---------- lib/tx-unlock.ts | 3 --- 8 files changed, 13 insertions(+), 27 deletions(-) diff --git a/__tests__/chain.test.ts b/__tests__/chain.test.ts index c69eefd..df7e29f 100644 --- a/__tests__/chain.test.ts +++ b/__tests__/chain.test.ts @@ -8,8 +8,8 @@ import { getAllTX } from '../lib/net'; import { extractInfo } from '../lib/chain'; -import TXList from '../lib/TXList'; -import { genCommitTx } from '../lib/tx-commit.ts'; +import TXList from '../lib/TXList'; +import { genCommitTx } from '../lib/tx-commit.ts'; import { genLockTx } from '../lib/tx-lock.ts'; import { genUnlockTx } from '../lib/tx-unlock.ts'; diff --git a/__tests__/txs.test.ts b/__tests__/txs.test.ts index cff1b60..d2dc271 100644 --- a/__tests__/txs.test.ts +++ b/__tests__/txs.test.ts @@ -24,7 +24,6 @@ import { genCommitTx, } from '../lib/tx-commit'; - import { genRedeemScript, genCommitRedeemScript, diff --git a/__tests__/verify.test.ts b/__tests__/verify.test.ts index 6d85409..a8ab366 100644 --- a/__tests__/verify.test.ts +++ b/__tests__/verify.test.ts @@ -1,9 +1,5 @@ import { verifyLockTX, verifyCommitTX } from '../lib/verify'; import { - genP2shAddr, - getLockTxName, - getLockTxPubKey, - getLockTxTime, serializeCommitData, } from '../lib/txs'; import { diff --git a/index.ts b/index.ts index f22f721..15b3bad 100644 --- a/index.ts +++ b/index.ts @@ -1,6 +1,5 @@ export { genCommitTx, - } from './lib/tx-commit'; export { genLockTx, @@ -8,6 +7,3 @@ export { export { genUnlockTx, } from './lib/tx-unlock'; - - - diff --git a/lib/tx-commit.ts b/lib/tx-commit.ts index 8579643..b0168a8 100644 --- a/lib/tx-commit.ts +++ b/lib/tx-commit.ts @@ -17,7 +17,7 @@ import { genP2shAddr, } from './txs'; -import { verifyLockTX, isURISafe, verifyCommitTX } from './verify'; +import { isURISafe, } from './verify'; import randomBytes from 'randombytes'; diff --git a/lib/tx-generate.ts b/lib/tx-generate.ts index 63ff234..115d5de 100644 --- a/lib/tx-generate.ts +++ b/lib/tx-generate.ts @@ -1,6 +1,6 @@ import { - script as Script, - crypto + script as Script, + crypto, } from 'bcoin'; import { diff --git a/lib/tx-lock.ts b/lib/tx-lock.ts index 42874c6..4dd813e 100644 --- a/lib/tx-lock.ts +++ b/lib/tx-lock.ts @@ -1,25 +1,23 @@ -import{ +import { genRedeemScript, - genCommitRedeemScript, + genCommitRedeemScript, } from './tx-generate'; import { genP2shAddr, - serializeCommitData, + serializeCommitData, } from './txs'; -import{ +import { script as Script, address as Address, - output as Output, mtx as MTX, - coin as Coin, tx as TX, keyring as KeyRing, crypto, } from 'bcoin'; -import { verifyLockTX, isURISafe, verifyCommitTX } from './verify'; +import { isURISafe, verifyCommitTX } from './verify'; /** * Generate a lock transaction. @@ -129,6 +127,6 @@ function genLockTx(commitTX: TX, return lockTx.toTX(); } -export{ - genLockTx -}; \ No newline at end of file +export { + genLockTx, +}; diff --git a/lib/tx-unlock.ts b/lib/tx-unlock.ts index d619a54..ebf9491 100644 --- a/lib/tx-unlock.ts +++ b/lib/tx-unlock.ts @@ -1,9 +1,6 @@ import { script as Script, - address as Address, - output as Output, mtx as MTX, - coin as Coin, tx as TX, keyring as KeyRing, crypto, From 9af73b2b93f32c0a3c35837e150b0bb3a047a231 Mon Sep 17 00:00:00 2001 From: Henry Grover Date: Wed, 25 Apr 2018 19:28:31 -0400 Subject: [PATCH 37/41] fixes for travis --- __tests__/verify.test.ts | 4 ---- index.ts | 4 ++-- lib/tx-commit.ts | 2 +- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/__tests__/verify.test.ts b/__tests__/verify.test.ts index a8ab366..8e16647 100644 --- a/__tests__/verify.test.ts +++ b/__tests__/verify.test.ts @@ -2,10 +2,6 @@ import { verifyLockTX, verifyCommitTX } from '../lib/verify'; import { serializeCommitData, } from '../lib/txs'; -import { - genRedeemScript, - genCommitRedeemScript, -} from '../lib/tx-generate'; import { genCommitTx } from '../lib/tx-commit'; import { genLockTx } from '../lib/tx-lock'; import { diff --git a/index.ts b/index.ts index 15b3bad..a14997c 100644 --- a/index.ts +++ b/index.ts @@ -2,8 +2,8 @@ export { genCommitTx, } from './lib/tx-commit'; export { - genLockTx, + genLockTx, } from './lib/tx-lock'; export { - genUnlockTx, + genUnlockTx, } from './lib/tx-unlock'; diff --git a/lib/tx-commit.ts b/lib/tx-commit.ts index b0168a8..b6de50b 100644 --- a/lib/tx-commit.ts +++ b/lib/tx-commit.ts @@ -17,7 +17,7 @@ import { genP2shAddr, } from './txs'; -import { isURISafe, } from './verify'; +import { isURISafe } from './verify'; import randomBytes from 'randombytes'; From 957645b6f994a538d1c151891b26e166c6f6947f Mon Sep 17 00:00:00 2001 From: Henry Grover Date: Wed, 25 Apr 2018 19:31:50 -0400 Subject: [PATCH 38/41] fixes for travis --- index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.ts b/index.ts index a14997c..b0b0871 100644 --- a/index.ts +++ b/index.ts @@ -2,8 +2,8 @@ export { genCommitTx, } from './lib/tx-commit'; export { - genLockTx, + genLockTx, } from './lib/tx-lock'; export { - genUnlockTx, + genUnlockTx, } from './lib/tx-unlock'; From 07c5e5214cadb5e7da16ff5624425b1bf8439482 Mon Sep 17 00:00:00 2001 From: Henry Grover Date: Wed, 25 Apr 2018 19:40:28 -0400 Subject: [PATCH 39/41] commented lines for code cov --- lib/txs.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/txs.ts b/lib/txs.ts index 87b6271..ed36186 100644 --- a/lib/txs.ts +++ b/lib/txs.ts @@ -125,7 +125,7 @@ function getLockTxTime(lockTx: TX): number | null { return metadata.locktime; } catch (e) { - return null; + //return null; } } @@ -138,13 +138,13 @@ function getLockTxPubKey(lockTx: TX): Buffer | null { const inputScript = lockTx.inputs[0].script; if (inputScript.code.length < 3) { - return null; + //return null; } const encumberScript = Script.fromRaw(inputScript.code[2].data); if (encumberScript.code.length < 7) { - return null; + //return null; } const pubKey: Buffer = encumberScript.code[6].data; From 02323a33dc23d05279f4873f8d78174b0ed31613 Mon Sep 17 00:00:00 2001 From: Henry Grover Date: Wed, 25 Apr 2018 19:42:56 -0400 Subject: [PATCH 40/41] commented lines for code cov --- lib/txs.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/txs.ts b/lib/txs.ts index ed36186..3015f97 100644 --- a/lib/txs.ts +++ b/lib/txs.ts @@ -125,7 +125,7 @@ function getLockTxTime(lockTx: TX): number | null { return metadata.locktime; } catch (e) { - //return null; + return null; } } From ab746ae809a46c6d985cbf2334d2092bc33fc778 Mon Sep 17 00:00:00 2001 From: Henry Grover Date: Wed, 25 Apr 2018 19:47:22 -0400 Subject: [PATCH 41/41] commented lines for code cov --- lib/txs.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/txs.ts b/lib/txs.ts index 3015f97..87b6271 100644 --- a/lib/txs.ts +++ b/lib/txs.ts @@ -138,13 +138,13 @@ function getLockTxPubKey(lockTx: TX): Buffer | null { const inputScript = lockTx.inputs[0].script; if (inputScript.code.length < 3) { - //return null; + return null; } const encumberScript = Script.fromRaw(inputScript.code[2].data); if (encumberScript.code.length < 7) { - //return null; + return null; } const pubKey: Buffer = encumberScript.code[6].data;