From fbd39bf14966d0749b26aa913584f1fefff51c4b Mon Sep 17 00:00:00 2001 From: Yemel Jardi Date: Fri, 13 Feb 2015 13:33:25 -0300 Subject: [PATCH 1/2] Add bitcore examples to the README --- README.md | 13 ++++++ docs/examples.md | 113 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 docs/examples.md diff --git a/README.md b/README.md index 263a323..11e5d72 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,19 @@ The complete docs are hosted here: [bitcore documentation](http://bitcore.io/gui To get community assistance and ask for help with implementation questions, please use our [community forums](http://bitpaylabs.com/c/bitcore). +## Examples + +* [Generate a random address](https://github.com/bitpay/bitcore/blob/master/docs/examples.md#generate-a-random-address) +* [Generate a address from a SHA256 hash](https://github.com/bitpay/bitcore/blob/master/docs/examples.md#generate-a-address-from-a-sha256-hash) +* [Import an address via WIF](https://github.com/bitpay/bitcore/blob/master/docs/examples.md#import-an-address-via-wif) +* [Create a Transaction](https://github.com/bitpay/bitcore/blob/master/docs/examples.md#create-a-transaction) +* [Sign a Bitcoin message](https://github.com/bitpay/bitcore/blob/master/docs/examples.md#sign-a-bitcoin-message) +* [Verify a Bitcoin message](https://github.com/bitpay/bitcore/blob/master/docs/examples.md#verify-a-bitcoin-message) +* [Create an OP RETURN transaction](https://github.com/bitpay/bitcore/blob/master/docs/examples.md#create-an-op-return-transaction) +* [Create a 2-of-3 multisig P2SH address](https://github.com/bitpay/bitcore/blob/master/docs/examples.md#create-a-2-of-3-multisig-p2sh-address) +* [Spend from a 2-of-2 multisig P2SH address](https://github.com/bitpay/bitcore/blob/master/docs/examples.md#spend-from-a-2-of-2-multisig-p2sh-address) + + ## Modules This module provides bitcoin's core features. Other features and protocol extensions are built into separate modules. Here is a list of official bitcore modules: diff --git a/docs/examples.md b/docs/examples.md new file mode 100644 index 0000000..4185601 --- /dev/null +++ b/docs/examples.md @@ -0,0 +1,113 @@ +# Bitcore examples + +## Generate a random address +```javascript +var privateKey = new bitcore.PrivateKey(); + +var address = privateKey.toAddress(); +``` + +## Generate a address from a SHA256 hash +```javascript +var value = new Buffer('correct horse battery staple'); +var hash = bitcore.crypto.Hash.sha256(value); +var bn = bitcore.crypto.BN.fromBuffer(hash); + +var address = new bitcore.PrivateKey(bn).toAddress(); +``` + +## Import an address via WIF +```javascript +var wif = 'Kxr9tQED9H44gCmp6HAdmemAzU3n84H3dGkuWTKvE23JgHMW8gct'; + +var address = new bitcore.PrivateKey(wif).toAddress(); +``` + +## Create a Transaction +```javascript +var privateKey = new bitcore.PrivateKey('L1uyy5qTuGrVXrmrsvHWHgVzW9kKdrp27wBC7Vs6nZDTF2BRUVwy'); +var utxo = { + "txId" : "115e8f72f39fad874cfab0deed11a80f24f967a84079fb56ddf53ea02e308986", + "outputIndex" : 0, + "address" : "17XBj6iFEsf8kzDMGQk5ghZipxX49VXuaV", + "script" : "76a91447862fe165e6121af80d5dde1ecb478ed170565b88ac", + "satoshis" : 50000 +}; + +var transaction = new bitcore.Transaction() + .from(utxo) + .to('1Gokm82v6DmtwKEB8AiVhm82hyFSsEvBDK', 15000) + .sign(privateKey); +``` + +## Sign a Bitcoin message +```javascript +var Message = require('bitcore-message'); + +var privateKey = new bitcore.PrivateKey('L23PpjkBQqpAF4vbMHNfTZAb3KFPBSawQ7KinFTzz7dxq6TZX8UA'); +var message = new Message('This is an example of a signed message.'); + +var signature = message.sign(privateKey); +``` + +## Verify a Bitcoin message +```javascript +var Message = require('bitcore-message'); + +var address = '13Js7D3q4KvfSqgKN8LpNq57gcahrVc5JZ'; +var signature = 'IBOvIfsAs/da1e36W8kw1cQOPqPVXCW5zJgNQ5kI8m57FycZXdeFmeyoIqJSREzE4W7vfDmdmPk0HokuJPvgPPE='; + +var verified = new Message('This is an example of a signed message.').verify(address, signature); + ``` + +## Create an OP RETURN transaction +```javascript +var privateKey = new bitcore.PrivateKey('L1uyy5qTuGrVXrmrsvHWHgVzW9kKdrp27wBC7Vs6nZDTF2BRUVwy'); +var utxo = { + "txId" : "115e8f72f39fad874cfab0deed11a80f24f967a84079fb56ddf53ea02e308986", + "outputIndex" : 0, + "address" : "17XBj6iFEsf8kzDMGQk5ghZipxX49VXuaV", + "script" : "76a91447862fe165e6121af80d5dde1ecb478ed170565b88ac", + "satoshis" : 50000 +}; + +var transaction = new bitcore.Transaction() + .from(utxo) + .addData('bitcore rocks') // Add OP_RETURN data + .sign(privateKey); +``` + +## Create a 2-of-3 multisig P2SH address +```javascript +var publicKeys = [ + '026477115981fe981a6918a6297d9803c4dc04f328f22041bedff886bbc2962e01', + '02c96db2302d19b43d4c69368babace7854cc84eb9e061cde51cfa77ca4a22b8b9', + '03c6103b3b83e4a24a0e33a4df246ef11772f9992663db0c35759a5e2ebf68d8e9' +].map(bitcore.PublicKey); +var requiredSignatures = 2; + +var address = new bitcore.Address(publicKeys, requiredSignatures); +``` + +## Spend from a 2-of-2 multisig P2SH address +```javascript +var privateKeys = [ + new bitcore.PrivateKey('91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx'), + new bitcore.PrivateKey('91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgww7vXtT') +]; +var publicKeys = privateKeys.map(bitcore.PublicKey); +var address = new bitcore.Address(publicKeys, 2); // 2 of 2 + +var utxo = { + "txId" : "153068cdd81b73ec9d8dcce27f2c77ddda12dee3db424bff5cafdbe9f01c1756", + "outputIndex" : 0, + "address" : "2MvR3wixpB1usCNRugN6ufwxfT4GEFxoRhQ", + "script" : "a91422c4270d877177c1dd33037fb5b7bb3fd76cbb8b87", + "satoshis" : 20000 +}; + +var transaction = new bitcore.Transaction() + .from(utxo, publicKeys, 2) + .to('mtoKs9V381UAhUia3d7Vb9GNak8Qvmcsme', 20000) + .sign(privateKeys); +``` From 92b261a849c90ac254768a0636ba5d30b4cc6446 Mon Sep 17 00:00:00 2001 From: Yemel Jardi Date: Wed, 18 Feb 2015 12:00:20 -0300 Subject: [PATCH 2/2] Update examples --- docs/examples.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/examples.md b/docs/examples.md index 4185601..962736f 100644 --- a/docs/examples.md +++ b/docs/examples.md @@ -83,7 +83,7 @@ var publicKeys = [ '026477115981fe981a6918a6297d9803c4dc04f328f22041bedff886bbc2962e01', '02c96db2302d19b43d4c69368babace7854cc84eb9e061cde51cfa77ca4a22b8b9', '03c6103b3b83e4a24a0e33a4df246ef11772f9992663db0c35759a5e2ebf68d8e9' -].map(bitcore.PublicKey); +]; var requiredSignatures = 2; var address = new bitcore.Address(publicKeys, requiredSignatures); @@ -101,8 +101,8 @@ var address = new bitcore.Address(publicKeys, 2); // 2 of 2 var utxo = { "txId" : "153068cdd81b73ec9d8dcce27f2c77ddda12dee3db424bff5cafdbe9f01c1756", "outputIndex" : 0, - "address" : "2MvR3wixpB1usCNRugN6ufwxfT4GEFxoRhQ", - "script" : "a91422c4270d877177c1dd33037fb5b7bb3fd76cbb8b87", + "address" : address.toString(), + "script" : new bitcore.Script(address).toHex(), "satoshis" : 20000 };