Skip to content

Instantly share code, notes, and snippets.

@kyledrake
Created July 31, 2014 05:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyledrake/7b105372712534ffacac to your computer and use it in GitHub Desktop.
Save kyledrake/7b105372712534ffacac to your computer and use it in GitHub Desktop.
Testing darkwallet/stealth.js
var stealth = require('stealth.js')
var b = require('bitcoinjs-lib')
// Receiver generates a scan and spend keypair
var receiverScanPrivateKey = new b.ECKey(undefined, true)
var receiverScanPublicKey = receiverScanPrivateKey.getPub()
var receiverSpendPrivateKey = new b.ECKey(undefined, true)
var receiverSpendPublicKey = receiverSpendPrivateKey.getPub()
// Sender generates a keypair
var senderRootPrivateKey = new b.ECKey(undefined, true)
var senderRootPublicKey = senderRootPrivateKey.getPub()
// Receiver generates a stealth address
var recipientStealthAddress = stealth.formatAddress(
receiverScanPublicKey.toBytes(),
[receiverSpendPublicKey.toBytes()]
)
console.log('Receiver Stealth Address:')
console.log(recipientStealthAddress)
// var parsedStealthAddress = stealth.parseAddress(recipientStealthAddress)
// Now the Sender takes that recipient stealth address, and generates a tx
var newTx = new b.Transaction();
var stealthTx = stealth.addStealth(
recipientStealthAddress,
newTx,
undefined,
undefined,
undefined,
1
);
// {address: recipient, ephemKey: ephemKey, pubKey: pubKey}
console.log('Provided address from addStealth:')
console.log(stealthTx.address.toString())
console.log('Script Hex:')
console.log(b.convert.bytesToHex(newTx.outs[0].script.buffer))
// "terrorism and drugs" http://bit.ly/1rJEprL
newTx.addOutput(stealthTx.address+':'+100000)
var rawTx = b.convert.bytesToHex(newTx.serialize())
console.log('Raw TX:')
console.log(rawTx)
// Now, the recipient finds a OP_RETURN transaction and looks to see if it's ours
// TODO: Figure out how this works, it's in the backend probably. For now, we're cheating.
var discoveredEphemKey = stealthTx.ephemKey
// With the discovered ephem key, we need to derive the private key
var receiverStealthPrivateKey = stealth.uncoverPrivate(
receiverScanPrivateKey.toBytes(),
discoveredEphemKey,
receiverSpendPrivateKey.toBytes()
)
console.log('Uncovered private key:')
console.log(receiverStealthPrivateKey.toWif())
console.log('Uncovered address:')
console.log(receiverStealthPrivateKey.getAddress().toString())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment