2018-07-23 09:45:01 +02:00
|
|
|
const { describe, it } = require('mocha')
|
2018-06-25 08:24:37 +02:00
|
|
|
const assert = require('assert')
|
|
|
|
const bitcoin = require('../../')
|
|
|
|
const ecc = require('tiny-secp256k1')
|
2018-05-22 09:43:25 +02:00
|
|
|
|
2018-06-27 05:54:47 +02:00
|
|
|
function getAddress (node, network) {
|
|
|
|
return bitcoin.payments.p2pkh({ pubkey: node.publicKey, network }).address
|
2018-05-22 09:43:25 +02:00
|
|
|
}
|
2016-08-17 05:06:31 +02:00
|
|
|
|
2016-12-10 05:01:41 +01:00
|
|
|
// vG = (dG \+ sha256(e * dG)G)
|
2016-08-17 05:06:31 +02:00
|
|
|
function stealthSend (e, Q) {
|
2018-06-25 08:37:45 +02:00
|
|
|
const eQ = ecc.pointMultiply(Q, e, true) // shared secret
|
|
|
|
const c = bitcoin.crypto.sha256(eQ)
|
|
|
|
const Qc = ecc.pointAddScalar(Q, c)
|
|
|
|
const vG = bitcoin.ECPair.fromPublicKey(Qc)
|
2016-08-17 05:06:31 +02:00
|
|
|
|
2016-10-05 02:05:28 +02:00
|
|
|
return vG
|
2016-08-17 05:06:31 +02:00
|
|
|
}
|
|
|
|
|
2016-12-10 02:34:06 +01:00
|
|
|
// v = (d + sha256(eG * d))
|
2016-08-17 05:06:31 +02:00
|
|
|
function stealthReceive (d, eG) {
|
2018-06-25 08:37:45 +02:00
|
|
|
const eQ = ecc.pointMultiply(eG, d) // shared secret
|
|
|
|
const c = bitcoin.crypto.sha256(eQ)
|
|
|
|
const dc = ecc.privateAdd(d, c)
|
|
|
|
const v = bitcoin.ECPair.fromPrivateKey(dc)
|
2016-08-17 05:06:31 +02:00
|
|
|
|
2016-10-05 02:05:28 +02:00
|
|
|
return v
|
2016-08-17 05:06:31 +02:00
|
|
|
}
|
2016-08-13 03:42:53 +02:00
|
|
|
|
2016-12-10 02:34:06 +01:00
|
|
|
// d = (v - sha256(e * dG))
|
|
|
|
function stealthRecoverLeaked (v, e, Q) {
|
2018-06-25 08:37:45 +02:00
|
|
|
const eQ = ecc.pointMultiply(Q, e) // shared secret
|
|
|
|
const c = bitcoin.crypto.sha256(eQ)
|
|
|
|
const vc = ecc.privateSub(v, c)
|
|
|
|
const d = bitcoin.ECPair.fromPrivateKey(vc)
|
2016-11-25 03:42:17 +01:00
|
|
|
|
2016-12-10 02:34:06 +01:00
|
|
|
return d
|
2016-11-25 03:42:17 +01:00
|
|
|
}
|
|
|
|
|
2016-12-10 05:01:41 +01:00
|
|
|
// vG = (rG \+ sha256(e * dG)G)
|
2016-12-10 03:52:08 +01:00
|
|
|
function stealthDualSend (e, R, Q) {
|
2018-06-25 08:37:45 +02:00
|
|
|
const eQ = ecc.pointMultiply(Q, e) // shared secret
|
|
|
|
const c = bitcoin.crypto.sha256(eQ)
|
|
|
|
const Rc = ecc.pointAddScalar(R, c)
|
|
|
|
const vG = bitcoin.ECPair.fromPublicKey(Rc)
|
2016-12-10 03:52:08 +01:00
|
|
|
|
|
|
|
return vG
|
|
|
|
}
|
|
|
|
|
2016-12-10 05:01:41 +01:00
|
|
|
// vG = (rG \+ sha256(eG * d)G)
|
2016-12-10 03:52:08 +01:00
|
|
|
function stealthDualScan (d, R, eG) {
|
2018-06-25 08:37:45 +02:00
|
|
|
const eQ = ecc.pointMultiply(eG, d) // shared secret
|
|
|
|
const c = bitcoin.crypto.sha256(eQ)
|
|
|
|
const Rc = ecc.pointAddScalar(R, c)
|
|
|
|
const vG = bitcoin.ECPair.fromPublicKey(Rc)
|
2016-12-10 03:52:08 +01:00
|
|
|
|
|
|
|
return vG
|
|
|
|
}
|
|
|
|
|
2016-12-10 04:33:51 +01:00
|
|
|
// v = (r + sha256(eG * d))
|
2016-12-10 03:52:08 +01:00
|
|
|
function stealthDualReceive (d, r, eG) {
|
2018-06-25 08:37:45 +02:00
|
|
|
const eQ = ecc.pointMultiply(eG, d) // shared secret
|
|
|
|
const c = bitcoin.crypto.sha256(eQ)
|
|
|
|
const rc = ecc.privateAdd(r, c)
|
|
|
|
const v = bitcoin.ECPair.fromPrivateKey(rc)
|
2016-12-10 03:52:08 +01:00
|
|
|
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2016-08-13 03:42:53 +02:00
|
|
|
describe('bitcoinjs-lib (crypto)', function () {
|
|
|
|
it('can generate a single-key stealth address', function () {
|
2016-08-17 05:06:31 +02:00
|
|
|
// XXX: should be randomly generated, see next test for example
|
2018-06-25 08:37:45 +02:00
|
|
|
const recipient = bitcoin.ECPair.fromWIF('5KYZdUEo39z3FPrtuX2QbbwGnNP5zTd7yyr2SC1j299sBCnWjss') // private to recipient
|
|
|
|
const nonce = bitcoin.ECPair.fromWIF('KxVqB96pxbw1pokzQrZkQbLfVBjjHFfp2mFfEp8wuEyGenLFJhM9') // private to sender
|
2016-08-17 05:06:31 +02:00
|
|
|
|
|
|
|
// ... recipient reveals public key (recipient.Q) to sender
|
2018-06-25 08:37:45 +02:00
|
|
|
const forSender = stealthSend(nonce.privateKey, recipient.publicKey)
|
2018-05-22 09:43:25 +02:00
|
|
|
assert.equal(getAddress(forSender), '1CcZWwCpACJL3AxqoDbwEt4JgDFuTHUspE')
|
2016-08-17 05:06:31 +02:00
|
|
|
assert.throws(function () { forSender.toWIF() }, /Error: Missing private key/)
|
|
|
|
|
|
|
|
// ... sender reveals nonce public key (nonce.Q) to recipient
|
2018-06-25 08:37:45 +02:00
|
|
|
const forRecipient = stealthReceive(recipient.privateKey, nonce.publicKey)
|
2018-05-22 09:43:25 +02:00
|
|
|
assert.equal(getAddress(forRecipient), '1CcZWwCpACJL3AxqoDbwEt4JgDFuTHUspE')
|
2016-08-17 05:06:31 +02:00
|
|
|
assert.equal(forRecipient.toWIF(), 'L1yjUN3oYyCXV3LcsBrmxCNTa62bZKWCybxVJMvqjMmmfDE8yk7n')
|
|
|
|
|
|
|
|
// sender and recipient, both derived same address
|
2018-05-22 09:43:25 +02:00
|
|
|
assert.equal(getAddress(forSender), getAddress(forRecipient))
|
2016-08-17 05:06:31 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('can generate a single-key stealth address (randomly)', function () {
|
2018-06-25 08:37:45 +02:00
|
|
|
const recipient = bitcoin.ECPair.makeRandom() // private to recipient
|
|
|
|
const nonce = bitcoin.ECPair.makeRandom() // private to sender
|
2016-08-17 05:06:31 +02:00
|
|
|
|
|
|
|
// ... recipient reveals public key (recipient.Q) to sender
|
2018-06-25 08:37:45 +02:00
|
|
|
const forSender = stealthSend(nonce.privateKey, recipient.publicKey)
|
2016-08-17 05:06:31 +02:00
|
|
|
assert.throws(function () { forSender.toWIF() }, /Error: Missing private key/)
|
|
|
|
|
|
|
|
// ... sender reveals nonce public key (nonce.Q) to recipient
|
2018-06-25 08:37:45 +02:00
|
|
|
const forRecipient = stealthReceive(recipient.privateKey, nonce.publicKey)
|
2016-08-17 05:06:31 +02:00
|
|
|
assert.doesNotThrow(function () { forRecipient.toWIF() })
|
|
|
|
|
|
|
|
// sender and recipient, both derived same address
|
2018-05-22 09:43:25 +02:00
|
|
|
assert.equal(getAddress(forSender), getAddress(forRecipient))
|
2016-08-13 03:42:53 +02:00
|
|
|
})
|
|
|
|
|
2016-11-25 03:42:17 +01:00
|
|
|
it('can recover parent recipient.d, if a derived private key is leaked [and nonce was revealed]', function () {
|
2018-06-25 08:37:45 +02:00
|
|
|
const recipient = bitcoin.ECPair.makeRandom() // private to recipient
|
|
|
|
const nonce = bitcoin.ECPair.makeRandom() // private to sender
|
2016-11-25 03:42:17 +01:00
|
|
|
|
|
|
|
// ... recipient reveals public key (recipient.Q) to sender
|
2018-06-25 08:37:45 +02:00
|
|
|
const forSender = stealthSend(nonce.privateKey, recipient.publicKey)
|
2016-11-25 03:42:17 +01:00
|
|
|
assert.throws(function () { forSender.toWIF() }, /Error: Missing private key/)
|
|
|
|
|
|
|
|
// ... sender reveals nonce public key (nonce.Q) to recipient
|
2018-06-25 08:37:45 +02:00
|
|
|
const forRecipient = stealthReceive(recipient.privateKey, nonce.publicKey)
|
2016-11-25 03:42:17 +01:00
|
|
|
assert.doesNotThrow(function () { forRecipient.toWIF() })
|
|
|
|
|
|
|
|
// ... recipient accidentally leaks forRecipient.d on the blockchain
|
2018-06-25 08:37:45 +02:00
|
|
|
const leaked = stealthRecoverLeaked(forRecipient.privateKey, nonce.privateKey, recipient.publicKey)
|
2016-11-25 03:42:17 +01:00
|
|
|
assert.equal(leaked.toWIF(), recipient.toWIF())
|
|
|
|
})
|
|
|
|
|
2016-12-10 04:37:14 +01:00
|
|
|
it('can generate a dual-key stealth address', function () {
|
2016-12-10 03:52:08 +01:00
|
|
|
// XXX: should be randomly generated, see next test for example
|
2018-06-25 08:37:45 +02:00
|
|
|
const recipient = bitcoin.ECPair.fromWIF('5KYZdUEo39z3FPrtuX2QbbwGnNP5zTd7yyr2SC1j299sBCnWjss') // private to recipient
|
|
|
|
const scan = bitcoin.ECPair.fromWIF('L5DkCk3xLLoGKncqKsWQTdaPSR4V8gzc14WVghysQGkdryRudjBM') // private to scanner/recipient
|
|
|
|
const nonce = bitcoin.ECPair.fromWIF('KxVqB96pxbw1pokzQrZkQbLfVBjjHFfp2mFfEp8wuEyGenLFJhM9') // private to sender
|
2016-12-10 03:52:08 +01:00
|
|
|
|
|
|
|
// ... recipient reveals public key(s) (recipient.Q, scan.Q) to sender
|
2018-06-25 08:37:45 +02:00
|
|
|
const forSender = stealthDualSend(nonce.privateKey, recipient.publicKey, scan.publicKey)
|
2016-12-10 03:52:08 +01:00
|
|
|
assert.throws(function () { forSender.toWIF() }, /Error: Missing private key/)
|
|
|
|
|
|
|
|
// ... sender reveals nonce public key (nonce.Q) to scanner
|
2018-06-25 08:37:45 +02:00
|
|
|
const forScanner = stealthDualScan(scan.privateKey, recipient.publicKey, nonce.publicKey)
|
2016-12-10 03:52:08 +01:00
|
|
|
assert.throws(function () { forScanner.toWIF() }, /Error: Missing private key/)
|
|
|
|
|
|
|
|
// ... scanner reveals relevant transaction + nonce public key (nonce.Q) to recipient
|
2018-06-25 08:37:45 +02:00
|
|
|
const forRecipient = stealthDualReceive(scan.privateKey, recipient.privateKey, nonce.publicKey)
|
2016-12-10 03:52:08 +01:00
|
|
|
assert.doesNotThrow(function () { forRecipient.toWIF() })
|
|
|
|
|
|
|
|
// scanner, sender and recipient, all derived same address
|
2018-05-22 09:43:25 +02:00
|
|
|
assert.equal(getAddress(forSender), getAddress(forScanner))
|
|
|
|
assert.equal(getAddress(forSender), getAddress(forRecipient))
|
2016-12-10 03:52:08 +01:00
|
|
|
})
|
2016-12-10 04:37:14 +01:00
|
|
|
|
|
|
|
it('can generate a dual-key stealth address (randomly)', function () {
|
2018-06-25 08:37:45 +02:00
|
|
|
const recipient = bitcoin.ECPair.makeRandom() // private to recipient
|
|
|
|
const scan = bitcoin.ECPair.makeRandom() // private to scanner/recipient
|
|
|
|
const nonce = bitcoin.ECPair.makeRandom() // private to sender
|
2016-12-10 04:37:14 +01:00
|
|
|
|
|
|
|
// ... recipient reveals public key(s) (recipient.Q, scan.Q) to sender
|
2018-06-25 08:37:45 +02:00
|
|
|
const forSender = stealthDualSend(nonce.privateKey, recipient.publicKey, scan.publicKey)
|
2016-12-10 04:37:14 +01:00
|
|
|
assert.throws(function () { forSender.toWIF() }, /Error: Missing private key/)
|
|
|
|
|
|
|
|
// ... sender reveals nonce public key (nonce.Q) to scanner
|
2018-06-25 08:37:45 +02:00
|
|
|
const forScanner = stealthDualScan(scan.privateKey, recipient.publicKey, nonce.publicKey)
|
2016-12-10 04:37:14 +01:00
|
|
|
assert.throws(function () { forScanner.toWIF() }, /Error: Missing private key/)
|
|
|
|
|
|
|
|
// ... scanner reveals relevant transaction + nonce public key (nonce.Q) to recipient
|
2018-06-25 08:37:45 +02:00
|
|
|
const forRecipient = stealthDualReceive(scan.privateKey, recipient.privateKey, nonce.publicKey)
|
2016-12-10 04:37:14 +01:00
|
|
|
assert.doesNotThrow(function () { forRecipient.toWIF() })
|
|
|
|
|
|
|
|
// scanner, sender and recipient, all derived same address
|
2018-05-22 09:43:25 +02:00
|
|
|
assert.equal(getAddress(forSender), getAddress(forScanner))
|
|
|
|
assert.equal(getAddress(forSender), getAddress(forRecipient))
|
2016-12-10 04:37:14 +01:00
|
|
|
})
|
2016-08-13 03:42:53 +02:00
|
|
|
})
|