Skip to content

minimal changes to make datacash compatible with the bsv chain #12

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

Open
wants to merge 5 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
40 changes: 20 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

![logo](logo.png)

Datacash is the simplest library for building and broadcasting Bitcoin Cash OP_RETURN transactions.
Datacash is the simplest library for building and broadcasting Bitcoin SV OP_RETURN transactions.

---

Expand Down Expand Up @@ -68,7 +68,7 @@ datacash.send({
});
```

Above code builds an `OP_RETURN` transaction with `0x6d02 hello` as push data, and broadcasts it to Bitcoin Cash network.
Above code builds an `OP_RETURN` transaction with `0x6d02 hello` as push data, and broadcasts it to Bitcoin SV network.

---

Expand All @@ -81,7 +81,7 @@ var config = {
data: ["0x6d02", "hello from datacash"],
cash: {
key: "5JZ4RXH4MoXpaUQMcJHo8DxhZtkf5U5VnYd9zZH8BRKZuAbxZEw",
rpc: "https://cashexplorer.bitcoin.com",
rpc: "https://bchsvexplorer.com",
fee: 250,
to: [{
address: "1A2JN4JAUoKCQ5kA4pHhu4qCqma8jZSU81",
Expand All @@ -96,7 +96,7 @@ Above config describes a transaction that:
- Posts `"hello from datacash"` to [memo.cash](https://memo.cash) network (See the protocol at [https://memo.cash/protocol](https://memo.cash/protocol)),
- paying the fee of `250` satoshis,
- signed with a private key: `5JZ4RXH4MoXpaUQMcJHo8DxhZtkf5U5VnYd9zZH8BRKZuAbxZEw`,
- through a public JSON-RPC endpoint at [https://cashexplorer.bitcoin.com](https://cashexplorer.bitcoin.com)
- through a public JSON-RPC endpoint at [https://bchsvexplorer.com](https://bchsvexplorer.com)
- while tipping the user `1A2JN4JAUoKCQ5kA4pHhu4qCqma8jZSU81` a value of `1000` satoshis.

All you need to do to invoke it is call:
Expand All @@ -119,11 +119,11 @@ And that's it! No complex APIs, but you can construct pretty much all kinds of O

# How it works

`datacash` is powered by [bitcore-lib-cash](https://github.com/bitpay/bitcore-lib-cash), which in turn is a fork of [bitcore-lib](https://github.com/bitpay/bitcore-lib), which means all the low level transactions are completely robust and secure.
`datacash` is powered by [bsv](https://github.com/moneybutton/bsv) a fork of [bitcore-lib-cash](https://github.com/bitpay/bitcore-lib-cash), which in turn is a fork of [bitcore-lib](https://github.com/bitpay/bitcore-lib), which means all the low level transactions are completely robust and secure.

`datacash` was created in order to make it dead simple to construct `OP_RETURN` related transactions, but you can even use it to build regular transactions.

Also `datacash` exposes `datacash.bch` endpoint which you can use to access the underlying `bitcore-lib-cash` library. If you need more sophisticated features (in most cases you won't), feel free to use this feature. Best of both worlds!
Also `datacash` exposes `datacash.bsv` endpoint which you can use to access the underlying `bsv` library. If you need more sophisticated features (in most cases you won't), feel free to use this feature. Best of both worlds!

---

Expand Down Expand Up @@ -168,8 +168,8 @@ const tx = {
}
datacash.build(tx, function(err, tx) {
/**
* res contains the generated transaction object, powered by bitcore-lib-cash
* You can check it out at https://github.com/bitpay/bitcore-lib-cash/blob/master/lib/transaction/transaction.js
* res contains the generated transaction object, powered by the bsv library
* You can check it out at https://github.com/moneybutton/bsv/blob/master/lib/transaction/transaction.js
* Some available methods you can call on the tx object are:
* 1. tx.toString() => Export as string
* 2. tx.toObject() => Inspect the transaction as JSON object
Expand Down Expand Up @@ -197,8 +197,8 @@ const tx = {
}
datacash.build(tx, function(err, tx) {
/**
* res contains the generated transaction object, powered by bitcore-lib-cash
* You can check it out at https://github.com/bitpay/bitcore-lib-cash/blob/master/lib/transaction/transaction.js
* res contains the generated transaction object, powered by the bsv library
* You can check it out at https://github.com/moneybutton/bsv/blob/master/lib/transaction/transaction.js
* Some available methods you can call on the tx object are:
* 1. tx.toString() => Export as string
* 2. tx.toObject() => Inspect the transaction as JSON object
Expand Down Expand Up @@ -241,14 +241,14 @@ datacash.build(tx, function(err, tx) {

The `rpc` attribute is used to manually set the JSON-RPC endpoint you wish to broadcast through.

- default: `https://cashexplorer.bitcoin.com`
- default: `https://bchsvexplorer.com`

```
const tx = {
data: ["0x6d02", "hello world"],
cash: {
key: "5JZ4RXH4MoXpaUQMcJHo8DxhZtkf5U5VnYd9zZH8BRKZuAbxZEw",
rpc: "https://cashexplorer.bitcoin.com"
rpc: "https://bchsvexplorer.com"
}
};
datacash.build(tx, function(err, res) {
Expand All @@ -270,7 +270,7 @@ const tx = {
data: ["0x6d02", "hello world"],
cash: {
key: "5JZ4RXH4MoXpaUQMcJHo8DxhZtkf5U5VnYd9zZH8BRKZuAbxZEw",
rpc: "https://cashexplorer.bitcoin.com",
rpc: "https://bchsvexplorer.com",
fee: 250
}
}
Expand All @@ -288,7 +288,7 @@ The `to` attribute is an array of receivers to send the OP_RETURN to. Normally t

- default: `null`
- Each item in the `to` array can have 2 attributes:
- address: Bitcoin cash address string
- address: Bitcoin SV address string
- value: number (in satoshi)

```
Expand Down Expand Up @@ -436,19 +436,19 @@ datacash.send({

Datacash depends on two powerful libraries for low level stuff.

1. bitcore-lib-cash: https://github.com/bitpay/bitcore-lib-cash
1. bsv: https://github.com/moneybutton/bsv
2. bitcore-explorers: https://github.com/bitpay/bitcore-explorers

While Datacash is designed to be the simplest possible way to write data to the blockchain, you may want to sometimes access the low level libraries that power datacash.

Datacash exposes additional endpoints so you can simply access these libraries without having to install or include any additional libraries.

## 1. datacash.bch
## 1. datacash.bsv

This endpoint exposes the [bitcore-lib-cash](https://github.com/bitpay/bitcore-lib-cash) library object. Basically by referncing `bch` you have access to the entire bitcore-lib-cash library.
This endpoint exposes the [bsv](https://github.com/moneybutton/bsv) library object. Basically by referncing `bsv` you have access to the entire bsv library.

```
const privateKey = new datacash.bch.PrivateKey();
const privateKey = new datacash.bsv.PrivateKey();
const address = privateKey.toAddress();
console.log(address.toString()) // 15WZwpw3BofscM2u43ji85BXucai5YGToL
```
Expand All @@ -465,7 +465,7 @@ Using this endpoint you can connect to a public JSON-RPC endpoint to let you mak
datacash.connect([RPC ENDPOINT]).[METHOD]
```

If you leave the `RPC ENDPOINT` part out, it will automatically use the default https://cashexplorer.bitcoin.com node
If you leave the `RPC ENDPOINT` part out, it will automatically use the default https://bchsvexplorer.com node

### Example 1: Connecting to default node and calling `getUnspentUtxos()` method:

Expand All @@ -482,7 +482,7 @@ datacash.connect().getUnspentUtxos("14xMz8rKm4L83RuZdmsHXD2jvENZbv72vR", functio
### Example 2. Specifying a JSON-RPC endpoint

```
datacash.connect('https://cashexplorer.bitcoin.com').getUnspentUtxos("14xMz8rKm4L83RuZdmsHXD2jvENZbv72vR", function(err, utxos) {
datacash.connect('https://bchsvexplorer.com').getUnspentUtxos("14xMz8rKm4L83RuZdmsHXD2jvENZbv72vR", function(err, utxos) {
if (err) {
console.log("Error: ", err)
} else {
Expand Down
6 changes: 3 additions & 3 deletions example/composer.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
update(e.target.value)
}
howEl.onclick = function(e) {
alert("The first column is where you declaratively describe your transaction (See datacash documentation). The second column is where the generated transaction is displayed. The third column displays the actual hex encoded string that can be broadcasted to BCH network. The default transaction description is just a simple data-only OP_RETURN transaction which only spends 300 satoshi transaction fee and nothing more, but you can experiment with different options as well. WARNING: THIS ACTUALLY WORKS, SO HANDLE WITH CARE.")
alert("The first column is where you declaratively describe your transaction (See datacash documentation). The second column is where the generated transaction is displayed. The third column displays the actual hex encoded string that can be broadcasted to the BSV network. The default transaction description is just a simple data-only OP_RETURN transaction which only spends 300 satoshi transaction fee and nothing more, but you can experiment with different options as well. WARNING: THIS ACTUALLY WORKS, SO HANDLE WITH CARE.")
return false;
}
var update = function(html) {
Expand All @@ -100,7 +100,7 @@
}
sendEl.onclick = function(e) {
var t = hexEl.value;
var isvalid = new datacash.bch.Transaction(t).verify();
var isvalid = new datacash.bsv.Transaction(t).verify();
if (isvalid) {
send(t)
}
Expand All @@ -123,7 +123,7 @@
})
.then(function(response) {
console.log(response);
document.querySelector("#sent").innerHTML = "<a href='https://explorer.bitcoin.com/bch/tx/" + response.tx + "' target='_blank'>Success! Click to view Transaction</a>";
document.querySelector("#sent").innerHTML = "<a href='https://bchsvexplorer.com/tx/" + response.tx + "' target='_blank'>Success! Click to view Transaction</a>";
document.querySelector("#sent").className = "";
document.querySelector("#send").className = "hidden";
})
Expand Down
8 changes: 4 additions & 4 deletions example/playground.html
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
.then(function(response) {
console.log("Response = ", response)
e.target.textContent = "Sent!";
confirmEl.innerHTML = "<a href='https://explorer.bitcoin.com/bch/tx/" + response.tx + "' target='_blank'>Success! Click to view Transaction</a>";
confirmEl.innerHTML = "<a href='https://bchsvexplorer.com/tx/" + response.tx + "' target='_blank'>Success! Click to view Transaction</a>";
})
}
}
Expand Down Expand Up @@ -165,10 +165,10 @@
<body>
<div class='container'>
<div class='row noflex'>
<h1>Bitcoin Cash Microblogging Playground</h1>
This website uses an open source Bitcoin Cash library called <a href='https://github.com/unwriter/datacash'>Datacash</a> to construct and broadcast OP_RETURN Bitcoin Cash Transactions.<br><br>
<h1>Bitcoin SV Microblogging Playground</h1>
This website uses an open source Bitcoin SV library called <a href='https://github.com/unwriter/datacash'>Datacash</a> to construct and broadcast OP_RETURN Bitcoin SV Transactions.<br><br>
1. All the signing takes place 100% on the client side so your private key is safe. (Check out the source code to make sure)<br><br>
2. After the signed transaction is constructed, you can broadcast it to Bitcoin Cash network by pressing the "Send" button. This will broadcast the signed transaction through a public JSON-RPC endpoint.
2. After the signed transaction is constructed, you can broadcast it to Bitcoin SV network by pressing the "Send" button. This will broadcast the signed transaction through a public JSON-RPC endpoint.
</div>
<div class='row'>
<h2>Step 1. Select a protocol</h2>
Expand Down
24 changes: 12 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const bch = require('bitcore-lib-cash');
const bsv = require('bsv');
const explorer = require('bitcore-explorers');
const defaults = {
rpc: "https://cashexplorer.bitcoin.com",
rpc: "https://bchsvexplorer.com",
fee: 400
}
// The end goal of 'build' is to create a hex formated transaction object
Expand All @@ -15,7 +15,7 @@ var build = function(options, callback) {
// if it's a signed transaction
// and the request is trying to override using 'data' or 'cash',
// we should throw an error
let tx = new bch.Transaction(options.tx)
let tx = new bsv.Transaction(options.tx)
// transaction is already signed
if (tx.inputs.length > 0 && tx.inputs[0].script) {
if (options.cash || options.data) {
Expand All @@ -34,13 +34,13 @@ var build = function(options, callback) {
if (options.cash && options.cash.key) {
// key exists => create a signed transaction
let key = options.cash.key;
const privateKey = new bch.PrivateKey(key);
const privateKey = new bsv.PrivateKey(key);
const address = privateKey.toAddress();
const insight = new explorer.Insight(rpcaddr)
insight.getUnspentUtxos(address, function (err, res) {
let tx = new bch.Transaction(options.tx).from(res);
let tx = new bsv.Transaction(options.tx).from(res);
if (script) {
tx.addOutput(new bch.Transaction.Output({ script: script, satoshis: 0 }));
tx.addOutput(new bsv.Transaction.Output({ script: script, satoshis: 0 }));
}
if (options.cash.to && Array.isArray(options.cash.to)) {
options.cash.to.forEach(function(receiver) {
Expand Down Expand Up @@ -69,9 +69,9 @@ var build = function(options, callback) {
} else {
// key doesn't exist => create an unsigned transaction
let fee = (options.cash && options.cash.fee) ? options.cash.fee : defaults.fee;
let tx = new bch.Transaction(options.tx).fee(fee);
let tx = new bsv.Transaction(options.tx).fee(fee);
if (script) {
tx.addOutput(new bch.Transaction.Output({ script: script, satoshis: 0 }));
tx.addOutput(new bsv.Transaction.Output({ script: script, satoshis: 0 }));
}
callback(null, tx)
}
Expand All @@ -92,9 +92,9 @@ var _script = function(options) {
var s = null;
if (options.data) {
if (Array.isArray(options.data)) {
s = new bch.Script();
s = new bsv.Script();
// Add op_return
s.add(bch.Opcode.OP_RETURN);
s.add(bsv.Opcode.OP_RETURN);
options.data.forEach(function(item) {
// add push data
if (/^0x/i.test(item)) {
Expand All @@ -107,7 +107,7 @@ var _script = function(options) {
})
} else if (typeof options.data === 'string') {
// Exported transaction
s = bch.Script.fromHex(options.data);
s = bsv.Script.fromHex(options.data);
}
}
return s;
Expand All @@ -119,7 +119,7 @@ var connect = function(endpoint) {
module.exports = {
build: build,
send: send,
bch: bch,
bsv: bsv,
connect: connect,
}

14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"unpkg": "dist/datacash.min.js",
"dependencies": {
"bitcore-explorers": "^1.0.1",
"bitcore-lib-cash": "^0.18.1"
"bsv": "^0.18.1"
},
"files": [
"dist"
Expand Down