bitcoincashjs
Advanced tools
Comparing version 0.1.0 to 0.1.2
{ | ||
"name": "bitcore-lib", | ||
"main": "./bitcore-lib.min.js", | ||
"version": "0.13.19", | ||
"homepage": "http://bitcore.io", | ||
"name": "bitcoincashjs", | ||
"description": "A simple, safe, and powerful JavaScript Bitcoin Cash library.", | ||
"main": "index.js", | ||
"authors": [ | ||
"BitPay, Inc." | ||
"Emilio Almansi <hi@ealmansi.com>" | ||
], | ||
"description": "A pure, powerful core for your bitcoin project.", | ||
"moduleType": [ | ||
"globals" | ||
], | ||
"license": "MIT", | ||
"keywords": [ | ||
"bitcoin", | ||
"bitcore", | ||
"btc", | ||
"satoshi" | ||
"transaction", | ||
"address", | ||
"p2p", | ||
"ecies", | ||
"cryptocurrency", | ||
"blockchain", | ||
"payment", | ||
"bip21", | ||
"bip32", | ||
"bip37", | ||
"bip69", | ||
"bip70", | ||
"multisig" | ||
], | ||
"license": "MIT", | ||
"homepage": "https://bitcoincashjs.github.io/", | ||
"ignore": [ | ||
"**/.*", | ||
"CONTRIBUTING.md", | ||
"gulpfile.js", | ||
"lib", | ||
"index.js", | ||
"karma.conf.js", | ||
"npm-shrinkwrap.json", | ||
"test" | ||
"node_modules", | ||
"bower_components", | ||
"test", | ||
"tests" | ||
] | ||
} |
@@ -1,8 +0,9 @@ | ||
# 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 | ||
``` | ||
@@ -12,14 +13,44 @@ | ||
```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 | ||
``` | ||
## Translate an address to any Bitcoin Cash address format | ||
```javascript | ||
const Address = bitcoinCash.Address; | ||
const BitpayFormat = Address.BitpayFormat; | ||
const CashAddrFormat = Address.CashAddrFormat; | ||
const address = new Address('1MF7A5H2nHYYJMieouip2SkZiFZMBKqSZe'); | ||
console.log(address.toString()) // 1MF7A5H2nHYYJMieouip2SkZiFZMBKqSZe | ||
console.log(address.toString(BitpayFormat)) // Cchzj7d6fLX5CVd5Vf3jbxNbLNmm4BTYuG | ||
console.log(address.toString(CashAddrFormat)) // bitcoincash:qr0q67nsn66cf3klfufttr0vuswh3w5nt5jqpp20t9 | ||
``` | ||
## Read an address from any Bitcoin Cash address format | ||
```javascript | ||
const Address = bitcoinCash.Address; | ||
const fromString = Address.fromString; | ||
const BitpayFormat = Address.BitpayFormat; | ||
const CashAddrFormat = Address.CashAddrFormat; | ||
const legacy = fromString('1MF7A5H2nHYYJMieouip2SkZiFZMBKqSZe', | ||
'mainnet', 'pubkeyhash'); | ||
const bitpay = fromString('Cchzj7d6fLX5CVd5Vf3jbxNbLNmm4BTYuG', | ||
'mainnet', 'pubkeyhash', BitpayFormat); | ||
const cashaddr = fromString('bitcoincash:qr0q67nsn66cf3klfufttr0vuswh3w5nt5jqpp20t9', | ||
'mainnet', 'pubkeyhash', CashAddrFormat); | ||
``` | ||
## 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 | ||
``` | ||
@@ -29,52 +60,57 @@ | ||
```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... | ||
``` | ||
@@ -84,3 +120,3 @@ | ||
```javascript | ||
var publicKeys = [ | ||
const publicKeys = [ | ||
'026477115981fe981a6918a6297d9803c4dc04f328f22041bedff886bbc2962e01', | ||
@@ -90,5 +126,6 @@ '02c96db2302d19b43d4c69368babace7854cc84eb9e061cde51cfa77ca4a22b8b9', | ||
]; | ||
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 | ||
``` | ||
@@ -98,21 +135,21 @@ | ||
```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... | ||
``` |
@@ -27,4 +27,4 @@ /** | ||
shell.task([[ | ||
'npx browserify index.js --s bitcoinCash -t [ babelify --presets [ env ] ]', '|', | ||
`npx uglifyjs --comments -c -o dist/bitcoincashjs.${npmPackage.version}.min.js`, | ||
'npx browserify index.js --s bitcoinCash -t [ babelify ]', '|', | ||
`npx minify --out-file dist/bitcoincashjs.${npmPackage.version}.min.js`, | ||
].join(' ')]) | ||
@@ -31,0 +31,0 @@ ); |
{ | ||
"name": "bitcoincashjs", | ||
"version": "0.1.0", | ||
"version": "0.1.2", | ||
"description": "A simple, safe, and powerful JavaScript Bitcoin Cash library.", | ||
@@ -8,3 +8,3 @@ "author": "Emilio Almansi <hi@ealmansi.com>", | ||
"scripts": { | ||
"start": "npm install && npx gulp build", | ||
"build": "npm install && npx gulp build", | ||
"test": "npm install && npx gulp test", | ||
@@ -50,2 +50,3 @@ "lint": "npm install && npx gulp lint", | ||
"babel-core": "^6.26.0", | ||
"babel-minify": "^0.2.0", | ||
"babel-preset-env": "^1.6.1", | ||
@@ -52,0 +53,0 @@ "babelify": "^8.0.0", |
@@ -1,3 +0,2 @@ | ||
BitcoinCash.js | ||
============== | ||
# [BitcoinCash.js](https://bitcoincashjs.github.io/): The simple, safe, and powerful JavaScript library for Bitcoin Cash | ||
@@ -10,34 +9,42 @@ [![NPM Package](https://img.shields.io/npm/v/bitcoincashjs.svg?style=flat-square)](https://www.npmjs.org/package/bitcoincashjs) | ||
A simple, safe, and powerful JavaScript Bitcoin Cash library. | ||
## About Bitcoin Cash | ||
## Bitcoin Cash | ||
Bitcoin Cash is peer-to-peer electronic cash for the Internet. It is fully decentralized, has no central bank, and requires no trusted third-parties to operate. Bitcoin Cash is the continuation of the Bitcoin project, upgraded with consensus rules that allow it to grow and scale. | ||
Bitcoin Cash is the continuation of the Bitcoin project as peer-to-peer electronic cash for the Internet. | ||
## About BitcoinCash.js | ||
Bitcoin Cash uses a different `SigHash` for transaction signatures. The implementation in BitcoinCash.js has been tested agains the original Bitcoin Cash test vectors (see sighash.json in `/test`). Modifications in script evaluation have not yet been implemented. | ||
BitcoinCash.js is the first JavaScript library specifically made for Bitcoin Cash. It supports all major Bitcoin Cash uses cases right out of the box, keeping up-to-date with the latest network upgrades. This library can be used - and is thoroughly tested - both in the back-end (Node.js) and the front-end (web browsers). | ||
## Get Started | ||
BitcoinCash.js is a fork from [bitcore-lib](https://github.com/bitpay/bitcore-lib/), which is an extremely easy-to-use and well-tested JavaScript library for Bitcoin developed by Bitpay, Inc. However, as consensus rules between BTC and BCH become more and more incompatible, BitcoinCash.js will not add support for functionality specific to BTC, such as SegWit or the bech32 address format, and will continue to support all Bitcoin Cash uses cases as first-class citizens. | ||
## Installation | ||
### Using NPM | ||
```s | ||
npm install --save bitcoincashjs | ||
$ npm install --save bitcoincashjs | ||
``` | ||
### Using Bower | ||
```s | ||
bower install bitcoincashjs | ||
$ bower install --save bitcoincashjs | ||
``` | ||
## Documentation | ||
### Manually | ||
The complete docs are hosted here: [BitcoinCash.js documentation](http://bitcore.io/guide/). | ||
You may also download the distribution file manually and place it within your third-party scripts directory: [dist/bitcoincashjs.0.1.2.min.js](https://cdn.rawgit.com/bitcoincashjs/bitcoincashjs/master/dist/bitcoincashjs.0.1.2.min.js). | ||
- [Read the Developer Guide](http://bitcore.io/guide/) | ||
## Examples | ||
You can find many useful, up-to-date examples to get you started right away on the [BitcoinCash.js](https://bitcoincashjs.github.io/#Examples) website or by following the next links: | ||
* [Generate a random address](https://github.com/bitcoincashjs/bitcoincashjs/blob/master/docs/examples.md#generate-a-random-address) | ||
* [Generate a address from a SHA256 hash](https://github.com/bitcoincashjs/bitcoincashjs/blob/master/docs/examples.md#generate-a-address-from-a-sha256-hash) | ||
* [Translate an address to any Bitcoin Cash address format](https://github.com/bitcoincashjs/bitcoincashjs/blob/master/docs/examples.md#translate-an-address-to-any-bitcoin-cash-address-format) | ||
* [Read an address from any Bitcoin Cash address format](https://github.com/bitcoincashjs/bitcoincashjs/blob/master/docs/examples.md#read-an-address-from-any-bitcoin-cash-address-format) | ||
* [Import an address via WIF](https://github.com/bitcoincashjs/bitcoincashjs/blob/master/docs/examples.md#import-an-address-via-wif) | ||
* [Create a Transaction](https://github.com/bitcoincashjs/bitcoincashjs/blob/master/docs/examples.md#create-a-transaction) | ||
* [Verify a Bitcoin message](https://github.com/bitcoincashjs/bitcoincashjs/blob/master/docs/examples.md#verify-a-bitcoin-message) | ||
* [Sign a Bitcoin message](https://github.com/bitcoincashjs/bitcoincashjs/blob/master/docs/examples.md#sign-a-bitcoin-message) | ||
* [Verify a Bitcoin message](https://github.com/bitcoincashjs/bitcoincashjs/blob/master/docs/examples.md#verify-a-bitcoin-message) | ||
* [Create an OP RETURN transaction](https://github.com/bitcoincashjs/bitcoincashjs/blob/master/docs/examples.md#create-an-op-return-transaction) | ||
@@ -47,26 +54,23 @@ * [Create a 2-of-3 multisig P2SH address](https://github.com/bitcoincashjs/bitcoincashjs/blob/master/docs/examples.md#create-a-2-of-3-multisig-p2sh-address) | ||
## Security | ||
BitcoinCash.js is built upon Bitcore, which is used in production at Bitpay and many other [projects](http://bitcore.io#projects). | ||
BitcoinCash.js is a fork from [bitcore-lib](https://github.com/bitpay/bitcore-lib/), which is used in production at Bitpay Inc., and many other [projects](http://bitcore.io#projects). If you find a security issue, please email [bitcoincashjs@tuta.io](mailto:bitcoincashjs@tuta.io). | ||
If you find a security issue, please email bitcoincashjs@tuta.io. | ||
## Contributing | ||
Please send pull requests for bug fixes, code optimization, and ideas for improvement. For more information on how to contribute, please refer to our [CONTRIBUTING](https://github.com/bitcoincashjs/bitcoincashjs/blob/master/CONTRIBUTING.md) file. | ||
This is an open-source project, and any form of contribution is welcome. Feel free to create an issue in case you would like to share ideas for improvement, or would like to report a bug. Also, please send pull requests for bug fixes or code optimization. For more information on how to contribute, please refer to our [CONTRIBUTING](https://github.com/bitcoincashjs/bitcoincashjs/blob/master/CONTRIBUTING.md) file. | ||
## Development & Tests | ||
## Development | ||
To get started with development, you should first clone the repository and install any dependencies: | ||
```s | ||
git clone https://github.com/bitcoincashjs/bitcoincashjs | ||
cd bitcoincashjs | ||
npm install | ||
$ git clone https://github.com/bitcoincashjs/bitcoincashjs | ||
$ cd bitcoincashjs | ||
$ npm install | ||
``` | ||
Next, you can check everything is installed correctly by running the full test-suite and verifying that all tests are completed successfully. | ||
Run all the tests: | ||
```s | ||
gulp test:node | ||
gulp test:karma | ||
$ npm test | ||
``` | ||
@@ -73,0 +77,0 @@ |
@@ -284,2 +284,3 @@ 'use strict'; | ||
* @param {string=} type - The type: 'pubkeyhash' or 'scripthash' | ||
* @param {string} format - The format: 'legacy', 'bitpay' or 'cashaddr' | ||
* @returns {Object} An object with keys: hashBuffer, network and type | ||
@@ -294,22 +295,9 @@ * @private | ||
if (format === Address.LegacyFormat) { | ||
return Address._transformBuffer(Base58Check.decode(data), network, type); | ||
return Address._transformStringLegacy(data, network, type); | ||
} | ||
else if (format === Address.BitpayFormat) { | ||
var addressBuffer = Base58Check.decode(data); | ||
if (format === Address.BitpayFormat) { | ||
if (addressBuffer[0] === BITPAY_P2PKH_VERSION_BYTE) { | ||
addressBuffer[0] = 0; | ||
} | ||
else if (addressBuffer[0] === BITPAY_P2SH_VERSION_BYTE) { | ||
addressBuffer[0] = 5; | ||
} | ||
} | ||
return Address._transformBuffer(addressBuffer, network, type); | ||
return Address._transformStringBitpay(data, network, type); | ||
} | ||
else if (format === Address.CashAddrFormat) { | ||
var networkObject = Networks.get(network); | ||
var version = new Buffer([networkObject[type]]); | ||
var hashBuffer = new Buffer(cashaddr.decode(data).hash); | ||
var addressBuffer = Buffer.concat([version, hashBuffer]); | ||
return Address._transformBuffer(addressBuffer, network, type); | ||
return Address._transformStringCashAddr(data, network, type); | ||
} | ||
@@ -320,2 +308,57 @@ throw new TypeError('Unrecognized address format.'); | ||
/** | ||
* Internal function to transform a bitcoin address string in legacy format | ||
* | ||
* @param {string} data | ||
* @param {String|Network=} network - either a Network instance, 'livenet', or 'testnet' | ||
* @param {string=} type - The type: 'pubkeyhash' or 'scripthash' | ||
* @returns {Object} An object with keys: hashBuffer, network and type | ||
* @private | ||
*/ | ||
Address._transformStringLegacy = function(data, network, type) { | ||
const addressBuffer = Base58Check.decode(data); | ||
return Address._transformBuffer(addressBuffer, network, type); | ||
}; | ||
/** | ||
* Internal function to transform a bitcoin address string in Bitpay format | ||
* | ||
* @param {string} data | ||
* @param {String|Network=} network - either a Network instance, 'livenet', or 'testnet' | ||
* @param {string=} type - The type: 'pubkeyhash' or 'scripthash' | ||
* @returns {Object} An object with keys: hashBuffer, network and type | ||
* @private | ||
*/ | ||
Address._transformStringBitpay = function(data, network, type) { | ||
const addressBuffer = Base58Check.decode(data); | ||
if (addressBuffer[0] === BITPAY_P2PKH_VERSION_BYTE) { | ||
addressBuffer[0] = 0; | ||
} | ||
else if (addressBuffer[0] === BITPAY_P2SH_VERSION_BYTE) { | ||
addressBuffer[0] = 5; | ||
} | ||
return Address._transformBuffer(addressBuffer, network, type); | ||
}; | ||
/** | ||
* Internal function to transform a bitcoin address string in CashAddr format | ||
* | ||
* @param {string} data | ||
* @param {String|Network=} network - either a Network instance, 'livenet', or 'testnet' | ||
* @param {string=} type - The type: 'pubkeyhash' or 'scripthash' | ||
* @returns {Object} An object with keys: hashBuffer, network and type | ||
* @private | ||
*/ | ||
Address._transformStringCashAddr = function(data, network, type) { | ||
network = network || 'mainnet'; | ||
type = type || Address.PayToPublicKeyHash; | ||
var networkObject = Networks.get(network); | ||
$.checkArgument(networkObject, 'Invalid network.'); | ||
$.checkArgument(type in networkObject, 'Invalid type.'); | ||
var version = new Buffer([networkObject[type]]); | ||
var hashBuffer = new Buffer(cashaddr.decode(data).hash); | ||
var addressBuffer = Buffer.concat([version, hashBuffer]); | ||
return Address._transformBuffer(addressBuffer, network, type); | ||
}; | ||
/** | ||
* Instantiate an address from a PublicKey instance | ||
@@ -412,2 +455,3 @@ * | ||
* @param {string=} type - The type of address: 'script' or 'pubkey' | ||
* @param {string=} format - The format: 'legacy', 'bitpay' or 'cashaddr' | ||
* @returns {Address} A new valid and frozen instance of an Address | ||
@@ -519,2 +563,3 @@ */ | ||
* | ||
* @param {string=} format - The format: 'legacy', 'bitpay' or 'cashaddr' | ||
* @returns {string} Bitcoin address | ||
@@ -525,21 +570,9 @@ */ | ||
if (format === Address.LegacyFormat) { | ||
return Base58Check.encode(this.toBuffer()); | ||
return this._toStringLegacy(); | ||
} | ||
else if (format === Address.BitpayFormat) { | ||
var buffer = this.toBuffer(); | ||
if (this.network.toString() === 'livenet') { | ||
if (this.type === Address.PayToPublicKeyHash) { | ||
buffer[0] = BITPAY_P2PKH_VERSION_BYTE; | ||
} | ||
else if (this.type === Address.PayToScriptHash) { | ||
buffer[0] = BITPAY_P2SH_VERSION_BYTE; | ||
} | ||
} | ||
return Base58Check.encode(buffer); | ||
return this._toStringBitpay(); | ||
} | ||
else if (format === Address.CashAddrFormat) { | ||
var prefix = this.network.toString() === 'livenet' ? 'bitcoincash' : 'bchtest'; | ||
var type = this.type === Address.PayToPublicKeyHash ? 'P2PKH' : 'P2SH'; | ||
var hash = [...this.hashBuffer]; | ||
return cashaddr.encode(prefix, type, hash); | ||
return this._toStringCashAddr(); | ||
} | ||
@@ -550,2 +583,41 @@ throw new TypeError('Unrecognized address format.'); | ||
/** | ||
* Will return a the string representation of the address in legacy format | ||
* | ||
* @returns {string} Bitcoin address | ||
*/ | ||
Address.prototype._toStringLegacy = function() { | ||
return Base58Check.encode(this.toBuffer()); | ||
} | ||
/** | ||
* Will return a the string representation of the address in Bitpay format | ||
* | ||
* @returns {string} Bitcoin address | ||
*/ | ||
Address.prototype._toStringBitpay = function() { | ||
let buffer = this.toBuffer(); | ||
if (this.network.toString() === 'livenet') { | ||
if (this.type === Address.PayToPublicKeyHash) { | ||
buffer[0] = BITPAY_P2PKH_VERSION_BYTE; | ||
} | ||
else if (this.type === Address.PayToScriptHash) { | ||
buffer[0] = BITPAY_P2SH_VERSION_BYTE; | ||
} | ||
} | ||
return Base58Check.encode(buffer); | ||
} | ||
/** | ||
* Will return a the string representation of the address in CashAddr format | ||
* | ||
* @returns {string} Bitcoin address | ||
*/ | ||
Address.prototype._toStringCashAddr = function() { | ||
const prefix = this.network.toString() === 'livenet' ? 'bitcoincash' : 'bchtest'; | ||
const type = this.type === Address.PayToPublicKeyHash ? 'P2PKH' : 'P2SH'; | ||
const hash = [...this.hashBuffer]; | ||
return cashaddr.encode(prefix, type, hash); | ||
} | ||
/** | ||
* Will return a string formatted for the console | ||
@@ -552,0 +624,0 @@ * |
@@ -622,3 +622,2 @@ 'use strict'; | ||
describe('Address formats', function() { | ||
it('should throw an error if given an invalid format', function() { | ||
@@ -628,2 +627,5 @@ (function() { | ||
}).should.throw('Unrecognized address format.'); | ||
(function() { | ||
Address.fromString(PKHLivenet[0], 'livenet', Address.PayToPublicKeyHash, 'some invalid format'); | ||
}).should.throw('Unrecognized address format.'); | ||
}); | ||
@@ -747,3 +749,2 @@ | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
79
9
2
2653439
22
134
27480