Skip to content

Commit

Permalink
added build and sign tx example
Browse files Browse the repository at this point in the history
  • Loading branch information
coinables committed May 19, 2018
1 parent d12e879 commit bfc46df
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 10 deletions.
26 changes: 22 additions & 4 deletions README.md
@@ -1,7 +1,6 @@
# Buidl.js

Buidl.js is a bitcoinjs-lib wrapper.

Buidl.js is a bitcoinjs-lib wrapper. This is intended as a tool for developers, hobbyists and crypto-enthusiasts looking for an easy way to create bitcoin keys and build and sign transactions offline without having the hurdle of having to learn the bitcoinjs-lib library. Buidl.js is a simplified (and therefore less powerful) approach to pro grammatically working with bitcoin in javascript. It is NOT intended for production use -- it is 100% client-side javascript.

# Usage
0. Save a copy of buidl.js or clone the repo
Expand All @@ -28,8 +27,21 @@ Buidl.js is a bitcoinjs-lib wrapper.
</script>
</body>

3. Create key pair from sha256 hash of an input aka brainwallet

3. Get details of a private key
<head>
<script src="buidl.js"></script>
</head>
<body>
<script>
let newPair = buidl.createFrom("satoshi");
let address = newPair.addr;
let privateKey = newPair.pk;
console.log(address, privateKey);
</script>
</body>

4. Get details of a private key

<head>
<script src="buidl.js"></script>
Expand All @@ -43,7 +55,7 @@ Buidl.js is a bitcoinjs-lib wrapper.
</body>

4. Build and sign a transaction
5. Build and sign a transaction

* createTransaction(typei, txidi, outni, outputi, amounti, wifi, changeout, changeamt, inputvalue)

Expand All @@ -62,8 +74,14 @@ Buidl.js is a bitcoinjs-lib wrapper.
createTransaction("b", "34eceJ... > spending from p2wpk(bech32)

createTransaction("3", "34eceJ..", 0, "1P5Ef7FsaD1KsJNSTUcACceEWN9vsUe3eN", 350000, "L1RLQhjuGoQ37QP4jfaEFTHMuEUeh4JdUDkx32xeafhnpzRMDMXD", null, null, 4000000)
//returns {"signedtx":"894291f54d..."}

This function will return an object with the key "signedtx" which will contain the raw hex signed transaction that can now be broadcast to any node or API.

See test.html for an example of using the `createTransaction()` function.


## Additional Info

Expand Down
Binary file modified buidl.js
Binary file not shown.
9 changes: 7 additions & 2 deletions create.js
@@ -1,4 +1,6 @@
const bitcoin = require("bitcoinjs-lib");
let bitcoin = require("bitcoinjs-lib");
bitcoin.bigi = require('bigi');
bitcoin.Buffer = require('safe-buffer').Buffer;

function createP2PKH(){
var NETWORK = bitcoin.networks.bitcoin;
Expand Down Expand Up @@ -236,14 +238,17 @@ function createTransaction(typei, txidi, outni, outputi, amounti, wifi, changeou

}



module.exports = {
createP2PKH,
createP2WPKH,
createP2SHP2WPKH,
getNewAddress,
getDetails,
validateAddress,
createTransaction
createTransaction,
bitcoin
}

//binding functions to buidl
Expand Down
74 changes: 70 additions & 4 deletions test.html
Expand Up @@ -4,11 +4,77 @@
<script src="buidl.js"></script>
</head>
<body>
<table width="100%">
<tr>
<td width="65%"><h3>INPUT</h3>
<table width="100%">
<tr>
<td width="10">Type</td>
<td width="60%">Transaction Hash/ID</td>
<td width="10%">N Out</td>
<td width="20%">Input Value</td>
</tr>
<tr>
<td><input type="text" id="typei" size="10" onkeyup="return checkWitness();"></td>
<td><input type="text" id="txidi" size="65"></td>
<td><input type="text" id="outni" size="10"></td>
<td><input type="text" id="inputvalue" size="20"></td>
</tr>
</table>
</td>

<td><h3>OUTPUT(S)</h3>
<table width="100%">
<tr>
<td width="70%">Address</td>
<td width="30%">Value</td>

</tr>
<tr>
<td><input type="text" id="outputi" size="30"></td>
<td><input type="text" id="amounti"></td>
</tr>
<tr>
<td><input type="text" id="changeout" size="30"></td>
<td><input type="text" id="changeamt"></td>
</tr>
</table>
</td>
</tr>
</table>

<p>WIF Private Key of Input</p>
<input type="text" id="wifi" size="45">
<br>
<button onclick="return createTx();">Create Transaction</button>
<br>
<textarea id="signedtxoutput" readonly cols="45" rows="3"></textarea>

<script>
let newPair = buidl.createP2PKH();
let address = newPair.addr;
let privateKey = newPair.pk;
console.log(address, privateKey);
function createTx(){
let typei = document.getElementById("typei").value.trim();
let txidi = document.getElementById("txidi").value.trim();
let outni = parseInt(document.getElementById("outni").value.trim());
let inputvalue = parseInt(document.getElementById("inputvalue").value.trim());
inputvalue ? inputvalue : null;
let outputi = document.getElementById("outputi").value.trim();
let amounti = parseInt(document.getElementById("amounti").value.trim());
let changeout = document.getElementById("changeout").value.trim();
changeout ? changeout : null;
let changeamt = parseInt(document.getElementById("changeamt").value.trim());
changeamt ? changeamt : null;
let wifi = document.getElementById("wifi").value.trim();
let rawtx = buidl.createTransaction(typei, txidi, outni, outputi, amounti, wifi, changeout, changeamt, inputvalue);
document.getElementById("signedtxoutput").innerHTML = rawtx.signedtx;
}
function checkWitness(){
let checkType = document.getElementById("typei").value.trim();
if(checkType === "1"){
document.getElementById("inputvalue").disabled = true;
} else {
document.getElementById("inputvalue").disabled = false;
}
}
</script>
</body>
</html>

0 comments on commit bfc46df

Please sign in to comment.