From 9b8940dbbb24ad9efee32b6607cbd08b53c871b9 Mon Sep 17 00:00:00 2001 From: deanmlittle <10921578+deanmlittle@users.noreply.github.com> Date: Mon, 2 Sep 2019 16:01:32 +0800 Subject: [PATCH 1/3] Update index.js Added optional change address to promote single use addresses --- index.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 8e76ec5..aa57984 100644 --- a/index.js +++ b/index.js @@ -62,8 +62,10 @@ var build = function(options, callback) { tx.to(receiver.address, receiver.value) }) } - - tx.fee(defaults.fee).change(address); + + //Pay remaining outputs to next address if defined, or default back to sender's address + const change = options.change ? new bitcoin.Address.fromString(options.change) : address; + tx.fee(defaults.fee).change(change); let opt_pay = options.pay || {}; let myfee = opt_pay.fee || Math.ceil(tx._estimateSize()* (opt_pay.feeb || defaults.feeb)); tx.fee(myfee); From e74acca3a88a73913bfb343db46bb6e60a1ce11d Mon Sep 17 00:00:00 2001 From: deanmlittle <10921578+deanmlittle@users.noreply.github.com> Date: Mon, 2 Sep 2019 16:02:11 +0800 Subject: [PATCH 2/3] Update package.json Updated to latest version of BSV --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ba435c8..17a9362 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "mingo": "^2.2.4" }, "peerDependencies": { - "bsv": "0.27.1" + "bsv": "0.30.0" }, "files": [ "dist" From 093c47be4f2524460aeb09e1640751f386d050b3 Mon Sep 17 00:00:00 2001 From: deanmlittle <10921578+deanmlittle@users.noreply.github.com> Date: Mon, 2 Sep 2019 16:22:38 +0800 Subject: [PATCH 3/3] Update README.md --- README.md | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1d3a826..6e23c12 100644 --- a/README.md +++ b/README.md @@ -92,7 +92,8 @@ var config = { address: "1A2JN4JAUoKCQ5kA4pHhu4qCqma8jZSU81", value: 1000 }] - } + }, + change: "1CBtFi158ieej44LJ9XkoGuH99t1BFh15j" } ``` @@ -436,6 +437,32 @@ datapay.build(tx, function(err, tx) { }) ``` + +#### 6. `change` + +The `change` attribute is used to optionally specify where change for unspent funds pays out to, making it easy to implement single-use addresses. + +- default: sender's address + +``` +const tx = { + safe: true, + data: ["0x6d02", "hello world"], + pay: { + key: "5JZ4RXH4MoXpaUQMcJHo8DxhZtkf5U5VnYd9zZH8BRKZuAbxZEw", + }, + change: "1CBtFi158ieej44LJ9XkoGuH99t1BFh15j" +} +datapay.build(tx, function(err, res) { + /** + * res contains the generated transaction object + * (a signed transaction, since 'key' is included.) + * It also pays any unspent change to the address + * in the change field instead of returning to sender. + **/ +}) +``` + --- ### C. tx