Browse Source

Updated examples.

master
Emilio Almansi 7 years ago
parent
commit
ffcf68eb72
  1. 123
      docs/examples.md

123
docs/examples.md

@ -1,113 +1,122 @@
# Bitcore examples
# BitcoinCash.js Examples
## Generate a random address
```javascript
var privateKey = new bitcore.PrivateKey();
const privateKey = new bitcoinCash.PrivateKey();
const address = privateKey.toAddress();
var address = privateKey.toAddress();
console.log(address.toString()) // 15WZwpw3BofscM2u43ji85BXucai5YGToL
```
## 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);
const value = new Buffer('Bitcoin Cash - Peer-to-Peer Electronic Cash');
const hash = bitcoinCash.crypto.Hash.sha256(value);
const bn = bitcoinCash.crypto.BN.fromBuffer(hash);
const address = new bitcoinCash.PrivateKey(bn).toAddress();
var address = new bitcore.PrivateKey(bn).toAddress();
console.log(address.toString()) // 126tFHmNHNAXDYT1QeEBEwBbEojib1VZyg
```
## Import an address via WIF
```javascript
var wif = 'Kxr9tQED9H44gCmp6HAdmemAzU3n84H3dGkuWTKvE23JgHMW8gct';
const wif = 'Kxr9tQED9H44gCmp6HAdmemAzU3n84H3dGkuWTKvE23JgHMW8gct';
const address = new bitcoinCash.PrivateKey(wif).toAddress();
var address = new bitcore.PrivateKey(wif).toAddress();
console.log(address.toString()) // 19AAjaTUbRjQCMuVczepkoPswiZRhjtg31
```
## Create a Transaction
```javascript
var privateKey = new bitcore.PrivateKey('L1uyy5qTuGrVXrmrsvHWHgVzW9kKdrp27wBC7Vs6nZDTF2BRUVwy');
var utxo = {
"txId" : "115e8f72f39fad874cfab0deed11a80f24f967a84079fb56ddf53ea02e308986",
"outputIndex" : 0,
"address" : "17XBj6iFEsf8kzDMGQk5ghZipxX49VXuaV",
"script" : "76a91447862fe165e6121af80d5dde1ecb478ed170565b88ac",
"satoshis" : 50000
const privateKey = new bitcoinCash.PrivateKey('L1uyy5qTuGrVXrmrsvHWHgVzW9kKdrp27wBC7Vs6nZDTF2BRUVwy');
const utxo = {
'txId' : '115e8f72f39fad874cfab0deed11a80f24f967a84079fb56ddf53ea02e308986',
'outputIndex' : 0,
'address' : '17XBj6iFEsf8kzDMGQk5ghZipxX49VXuaV',
'script' : '76a91447862fe165e6121af80d5dde1ecb478ed170565b88ac',
'satoshis' : 50000
};
var transaction = new bitcore.Transaction()
const transaction = new bitcoinCash.Transaction()
.from(utxo)
.to('1Gokm82v6DmtwKEB8AiVhm82hyFSsEvBDK', 15000)
.sign(privateKey);
console.log(transaction.toString()) // 01000000018689302ea03ef...
```
## Sign a Bitcoin message
## Verify a Bitcoin message
```javascript
var Message = require('bitcore-message');
const Message = require('bitcore-message');
var privateKey = new bitcore.PrivateKey('L23PpjkBQqpAF4vbMHNfTZAb3KFPBSawQ7KinFTzz7dxq6TZX8UA');
var message = new Message('This is an example of a signed message.');
const message = new Message('Bitcoin Cash - Peer-to-Peer Electronic Cash.');
const address = '13Js7D3q4KvfSqgKN8LpNq57gcahrVc5JZ';
const signature = 'IJuZCwN/4HtIRulOb/zRLU1oCPVMiPvT5dJhgXxOuQNFaXoytoejPePUerSs9KSIvPL/BDimPe2cj/JabeDGmbc=';
var signature = message.sign(privateKey);
console.log(message.verify(address, signature)) // true
```
## Verify a Bitcoin message
## Sign a Bitcoin message
```javascript
var Message = require('bitcore-message');
const Message = require('bitcore-message');
var address = '13Js7D3q4KvfSqgKN8LpNq57gcahrVc5JZ';
var signature = 'IBOvIfsAs/da1e36W8kw1cQOPqPVXCW5zJgNQ5kI8m57FycZXdeFmeyoIqJSREzE4W7vfDmdmPk0HokuJPvgPPE=';
const message = new Message('Bitcoin Cash - Peer-to-Peer Electronic Cash.');
const privateKey =
new bitcoinCash.PrivateKey('L23PpjkBQqpAF4vbMHNfTZAb3KFPBSawQ7KinFTzz7dxq6TZX8UA');
const signature = message.sign(privateKey);
var verified = new Message('This is an example of a signed message.').verify(address, signature);
```
console.log(signature.toString()) // IJuZCwN/4HtIRulOb/zRLU1oCP...
```
## Create an OP RETURN transaction
```javascript
var privateKey = new bitcore.PrivateKey('L1uyy5qTuGrVXrmrsvHWHgVzW9kKdrp27wBC7Vs6nZDTF2BRUVwy');
var utxo = {
"txId" : "115e8f72f39fad874cfab0deed11a80f24f967a84079fb56ddf53ea02e308986",
"outputIndex" : 0,
"address" : "17XBj6iFEsf8kzDMGQk5ghZipxX49VXuaV",
"script" : "76a91447862fe165e6121af80d5dde1ecb478ed170565b88ac",
"satoshis" : 50000
const privateKey = new bitcoinCash.PrivateKey('L1uyy5qTuGrVXrmrsvHWHgVzW9kKdrp27wBC7Vs6nZDTF2BRUVwy');
const utxo = {
'txId' : '115e8f72f39fad874cfab0deed11a80f24f967a84079fb56ddf53ea02e308986',
'outputIndex' : 0,
'address' : '17XBj6iFEsf8kzDMGQk5ghZipxX49VXuaV',
'script' : '76a91447862fe165e6121af80d5dde1ecb478ed170565b88ac',
'satoshis' : 50000
};
const transaction = new bitcoinCash.Transaction()
.from(utxo)
.addData('Bitcoin Cash - Peer-to-Peer Electronic Cash.') // Add OP_RETURN data
.sign(privateKey);
var transaction = new bitcore.Transaction()
.from(utxo)
.addData('bitcore rocks') // Add OP_RETURN data
.sign(privateKey);
console.log(transaction.toString()) // 01000000018689302ea03ef...
```
## Create a 2-of-3 multisig P2SH address
```javascript
var publicKeys = [
const publicKeys = [
'026477115981fe981a6918a6297d9803c4dc04f328f22041bedff886bbc2962e01',
'02c96db2302d19b43d4c69368babace7854cc84eb9e061cde51cfa77ca4a22b8b9',
'03c6103b3b83e4a24a0e33a4df246ef11772f9992663db0c35759a5e2ebf68d8e9'
];
var requiredSignatures = 2;
const requiredSignatures = 2;
const address = new bitcoinCash.Address(publicKeys, requiredSignatures);
var address = new bitcore.Address(publicKeys, requiredSignatures);
console.log(address.toString()) // 36NUkt6FWUi3LAWBqWRdDmdTWbt91Yvfu7
```
## Spend from a 2-of-2 multisig P2SH address
```javascript
var privateKeys = [
new bitcore.PrivateKey('91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx'),
new bitcore.PrivateKey('91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgww7vXtT')
const privateKeys = [
new bitcoinCash.PrivateKey('91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx'),
new bitcoinCash.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" : address.toString(),
"script" : new bitcore.Script(address).toHex(),
"satoshis" : 20000
const publicKeys = privateKeys.map(bitcoinCash.PublicKey);
const address = new bitcoinCash.Address(publicKeys, 2); // 2 of 2
const utxo = {
'txId' : '153068cdd81b73ec9d8dcce27f2c77ddda12dee3db424bff5cafdbe9f01c1756',
'outputIndex' : 0,
'address' : address.toString(),
'script' : new bitcoinCash.Script(address).toHex(),
'satoshis' : 20000
};
var transaction = new bitcore.Transaction()
const transaction = new bitcoinCash.Transaction()
.from(utxo, publicKeys, 2)
.to('mtoKs9V381UAhUia3d7Vb9GNak8Qvmcsme', 20000)
.sign(privateKeys);
console.log(transaction.toString()) // 010000000156171cf0e9dba...
```

Loading…
Cancel
Save