Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added "change" attribute #15

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ var config = {
address: "1A2JN4JAUoKCQ5kA4pHhu4qCqma8jZSU81",
value: 1000
}]
}
},
change: "1CBtFi158ieej44LJ9XkoGuH99t1BFh15j"
}
```

Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"mingo": "^2.2.4"
},
"peerDependencies": {
"bsv": "0.27.1"
"bsv": "0.30.0"
},
"files": [
"dist"
Expand Down