{"_id":"@brynjarrr/xchain-bitcoin","_rev":"1-c9b0606436ced0003f4c463b05e145b8","name":"@brynjarrr/xchain-bitcoin","dist-tags":{"latest":"0.18.1"},"versions":{"0.18.1":{"name":"@brynjarrr/xchain-bitcoin","version":"0.18.1","description":"Custom Bitcoin client and utilities used by XChainJS clients","keywords":["XChain","Bitcoin"],"author":{"name":"XChainJS"},"homepage":"https://github.com/xchainjs/xchainjs-lib","license":"MIT","main":"lib/index.js","module":"lib/index.esm.js","typings":"lib/index.d.ts","directories":{"lib":"lib","test":"__tests__"},"repository":{"type":"git","url":"git+ssh://git@github.com/xchainjs/xchainjs-lib.git"},"scripts":{"clean":"rimraf lib/**","build":"yarn clean && rollup -c","test":"jest","lint":"eslint \"{src,__tests__}/**/*.ts\" --fix --max-warnings 0","prepublishOnly":"yarn build","postversion":"git push --follow-tags"},"devDependencies":{"@types/bitcoinjs-lib":"^5.0.0","@types/wif":"^2.0.2","@xchainjs/xchain-client":"^0.11.1","@xchainjs/xchain-crypto":"^0.2.6","@xchainjs/xchain-util":"^0.5.1","axios":"^0.25.0","axios-mock-adapter":"^1.20.0","bitcoinjs-lib":"^5.2.0","coinselect":"^3.1.12","wif":"^2.0.6"},"peerDependencies":{"@xchainjs/xchain-client":"^0.11.1","@xchainjs/xchain-crypto":"^0.2.6","@xchainjs/xchain-util":"^0.5.1","axios":"^0.25.0","bitcoinjs-lib":"^5.2.0","coinselect":"^3.1.12","wif":"^2.0.6"},"publishConfig":{"access":"public"},"gitHead":"515ef43a4f0e3aa11644c69f7025caf0f0a0c1db","bugs":{"url":"https://github.com/xchainjs/xchainjs-lib/issues"},"_id":"@brynjarrr/xchain-bitcoin@0.18.1","_nodeVersion":"16.5.0","_npmVersion":"lerna/3.22.1/node@v16.5.0+arm64 (darwin)","dist":{"integrity":"sha512-qdvVMAzH9sYZtKkN5KSRbX+knKJgMHBu0ZYudOWvur6VRTOtRJjSmO3YyeAzxL2ZaN+qt8pHGHXwjlzNKLNiXQ==","shasum":"39c974592eb7e1e7ce16afa2def96237d7091fd0","tarball":"https://registry.npmjs.org/@brynjarrr/xchain-bitcoin/-/xchain-bitcoin-0.18.1.tgz","fileCount":19,"unpackedSize":13718284,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJiPbUFACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmrYrw//VzFluNlbQHKjb9Spxcszhg4Qj+XEWvBO/WXL0y8K8XrkuFE7\r\nsp2Nsp+uZkZzPk49asb5COHpl6L/v2WfG75EDfTGAtYkgBhLhCMdBOKUYCdc\r\nHM+29Jswg92k98gCELTPMzi6wqaClljItOKsXCMYH7LDr7ue+6+KNqHovzuF\r\nCxL9BYRH6NqgIPhLL+UjRq9IcT+x9y+kDMN0tRstJUNm43PWA4C6N/ucjHZI\r\nkjQlv+XA5WMbHEnZs+rSp5aLy7Fgqhtja3JQNBGNZNv85vUwYADlgZ3ejVng\r\nMtWczhrrG3rzaa4faoNqqlj8bm1xDVTGheEWoFNCceCAUodHQoNdlFKXwemJ\r\nWQfWhyBmU4v2M1T22UJsBNi8D081aOQXxpxXyM02dULCoqzij6SNKFv7MzqE\r\n74HoNMc4vw8c3mmvRuB4hFvarKL0szJHu5Awf3NlXexhvzS/3HxEmcefg10C\r\nULbfkdhYSQRdtM3s6TucVZ4GML244eku/22GMoRyhM0a7SgPrrRJLzBB5m6h\r\npgg5vSbhUSOqKOXe1+3nMXFSwXwkjWHtuc5qbhaHABeUtBHhkpiIDyxMLudj\r\ndF6PsITOJQ2AVQJFMMYPYGKgrgpXLC3S2vIe0MwJRCox1xQIHv0dGS0j1T84\r\nEG6vsMc2kQxm0hJ/5dW1mVdo9nN10WMB/Gg=\r\n=3dQA\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIBqN1ZQwiqGprET/73GJl8C0EszVwpt7UCUQ9rgDC8IMAiBTnAbXu9oxwoHuWxDfLHSsbmiEsKdkKikzTl77lvqykQ=="}]},"_npmUser":{"name":"brynjarrr","email":"coinage_trinity_0a@icloud.com"},"maintainers":[{"name":"brynjarrr","email":"coinage_trinity_0a@icloud.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/xchain-bitcoin_0.18.1_1648211205176_0.8247478618803077"},"_hasShrinkwrap":false}},"time":{"created":"2022-03-25T12:26:45.090Z","0.18.1":"2022-03-25T12:26:45.434Z","modified":"2022-04-04T20:50:13.945Z"},"maintainers":[{"name":"brynjarrr","email":"coinage_trinity_0a@icloud.com"}],"description":"Custom Bitcoin client and utilities used by XChainJS clients","homepage":"https://github.com/xchainjs/xchainjs-lib","keywords":["XChain","Bitcoin"],"repository":{"type":"git","url":"git+ssh://git@github.com/xchainjs/xchainjs-lib.git"},"author":{"name":"XChainJS"},"bugs":{"url":"https://github.com/xchainjs/xchainjs-lib/issues"},"license":"MIT","readme":"# `@xchainjs/xchain-bitcoin`\n\n## Modules\n\n- `client` - Custom client for communicating with Bitcoin using [BIP39](https://github.com/bitcoinjs/bip39) [bitcoinjs-lib](https://github.com/bitcoinjs/bitcoinjs-lib) and [WIF](https://github.com/bitcoinjs/wif)\n\n## Installation\n\n```\nyarn add @xchainjs/xchain-bitcoin\n```\n\nFollowing peer dependencies have to be installed into your project. These are not included in `@xchainjs/xchain-bitcoin`.\n\n```\nyarn add @xchainjs/xchain-client @xchainjs/xchain-crypto @xchainjs/xchain-util axios bitcoinjs-lib wif\n```\n\n## Basic Usage Examples\n\n## Generate new phrase, & save to encrypted keystore\n\nUse xchain-crypto for generating and handling crypto related functions\n\n```ts\n//Imports\nimport fs = require('fs'); \nimport { generatePhrase, validatePhrase, encryptToKeyStore } from \"@xchainjs/xchain-crypto\"\n\n// Save the encrypted Keystore using phrase generated\nconst GenerateKeystore = async() => {\n    const phrase = generatePhrase()\n    const isCorrect = validatePhrase(phrase) //validate phrase if needed returns Boolean\n    let password = process.env.PASSWORD\n    const keystore = await encryptToKeyStore(phrase, password)  \n    fs.writeFileSync(`./keystore.json`, JSON.stringify(keystore, null, 4), 'utf8')\n    console.log(`Phrase valid?: ${isCorrect}`)\n}\n\n```\n\n## Connect wallet to new Client and access class methods\n\nDecrypt keystore returns `phrase` \nCreate a new client instance\nUse the client to getAddress() & getBalance() of address\n\n```ts\n//Imports\nimport { Client } from \"@xchainjs/xchain-bitcoin\"\nimport { Network } from \"@xchainjs/xchain-client\"\nimport { decryptFromKeystore} from \"@xchainjs/xchain-crypto\"\n\n// Connect wallet to new btc client \nconst connectWallet = async () => {\n    let keystore = JSON.parse(fs.readFileSync('keystore.json', 'utf8'))\n    let password = process.env.PASSWORD\n    let phrase = await decryptFromKeystore(keystore, password)\n    const btcClient = new Client({ network: Network.Mainnet, phrase})\n    let address = btcClient.getAddress()     \n    console.log(`Asset Address is: ${address}`)\n\n    let balances = await btcClient.getBalance(address)\n    try { \n        let assetAmount = (baseToAsset(balances[0].amount)).amount()\n        console.log(`Asset address balance: ${assetAmount}`)\n    } catch (error) {\n        console.log('Address has no balance')\n    }\n}\n```\n\n## Transfer btc using btcClient\nDefault feeRate is `fast`\nImport helper functions from `@xchainjs/xchain-util`\nDecrypt keystore to retrieve phrase\nUse utils to convert to BaseAmount for tx params\nBuild tx using parameters \nClient.transfer() > returns `Promise<string>` - The transaction hash \n\n```ts \n//Imports\nimport { assetToBase, baseToAsset, assetAmount, AssetBTC } from \"@xchainjs/xchain-util\"\n\nlet amountToTransfer = 0.0001\nlet recipient = 'Recipent_address'\n\nconst transfer = async () => {\n    let keystore = JSON.parse(fs.readFileSync('keystore.json', 'utf8'))\n    let password = process.env.PASSWORD\n    let phrase = await decryptFromKeystore(keystore, password)\n    let btcClient = new Client({network: Network.Mainnet, phrase })\n    let amount = assetToBase(assetAmount(amountToTransfer, 8))\n    try {\n        const txid = await btcClient.transfer({\n            'asset': AssetBTC,\n            'recipient': recipient,\n            'amount': amount,\n            'memo': \"payment\"\n        })\n        console.log(`Amount: ${amount.amount().toString()} ${AssetBTC.symbol} Transaction id: ${txid}`)\n    } catch (error){\n        console.log(`Transfer failed ${error}`)\n    }\n}\n\n```\n\n## Transfer & set feeRate \n\nBuild transaction using parameters\nSet feeRate in transaction parameters\nOr use getFeeRates()\n```ts\n//Returns FeeRates > this allows for dynamic feeRate adjustment on selection\nconst { fast, fastest, average } = await btcClient.getFeeRates()\n\ntry {\n        const txid = await btcClient.transfer({\n            'asset': AssetBTC,\n            'recipient': recipient,\n            'amount': amount,\n            'memo': \"test transfer\",\n            feeRate: fast\n        })\n        console.log(`Amount ${baseToAsset(amount).amount()} ${AssetBTC.symbol} Transaction id ${txid}`)\n    } catch (error){\n        console.log(`Transfer failed ${error}`)\n    }\n```\n\n## Get Fees & FeeRates estimations\n\nClient function getFees() returns object\n`Fast: {\"type\":\"BASE\",\"decimal\":8}`\n\n```ts\n//Get Fees - returns FeeOption & fee in BaseAmount \n` Fees Fast: 0.00001 Fastest: 0.0000468 Average: 0.00001 `\n    try{\n        const { fast, fastest, average } = await btcClient.getFees()\n        console.log(`Fees Fast: ${baseToAsset(fast).amount()} Fastest: ${baseToAsset(fastest).amount()} Average: ${baseToAsset(average).amount()}`)\n\n    }catch (error){\n        console.log(error)\n    }\n\n//Get FeeRates - returns FeeOption & rate  \n` Fast: 12, Fastest 60, Average: 6 `\n\n    try{\n        const { fast, fastest, average } = await btcClient.getFeeRates()\n        console.log(`Fast: ${fast}, Fastest ${fastest}, Average: ${average}`)\n\n    }catch (error){\n        console.log(error)\n    }\n\n```\n\n## Get transaction data\nCreate new btcClient instance\nCall getTransaction(hash) returns JSON object\n```ts\nconst transactionData = async () => {\n    let phrase = await decryptFromKeystore(keystore, password)\n    let btcClient = new Client({network: Network.Mainnet, phrase })\n    let hash = \"txhash string\" \n    try{\n        const txData = await btcClient.getTransactionData(hash)\n        console.log(`From ${JSON.stringify(txData)}`)\n\n    }catch (error) {\n        console.log(`Error: ${error}`)\n    }\n}\n\n```\n\n## Get Transaction History\nSearch the client for transaction history \nCreate new client instance\nCall function with variable `address` \nResults can be filtered with extra parameters { offset, limit, startTime, asset?}\n\n```ts\nconst transactionHistory = async () => {\n    let phrase = await decryptFromKeystore(keystore1, password)\n    let btcClient = new Client({ network: Network.Mainnet, phrase })\n    let Address = keystore1Address\n\n    try {\n        const txHistory = await btcClient.getTransactions({ address: Address, limit: 4 })\n        console.log(`Found ${txHistory.total.toString()}`)\n        txHistory.txs.forEach(tx => console.log(tx.hash))\n\n    } catch (error) {\n            console.log(`Error: ${error}`)\n    }\n}\n\n```\n\n## Service Providers\n\nThis package uses the following service providers:\n\n| Function                    | Service     | Notes                                                                            |\n| --------------------------- | ----------- | -------------------------------------------------------------------------------- |\n| Balances                    | Sochain     | https://sochain.com/api#get-balance                                              |\n| Transaction history         | Sochain     | https://sochain.com/api#get-display-data-address, https://sochain.com/api#get-tx |\n| Transaction details by hash | Sochain     | https://sochain.com/api#get-tx                                                   |\n| Transaction fees            | Bitgo       | https://app.bitgo.com/docs/#operation/v2.tx.getfeeestimate                       |\n| Transaction broadcast       | Sochain     | https://sochain.com/api#send-transaction                                         |\n| Explorer                    | Blockstream | https://blockstream.info                                                         |\n\nSochain API rate limits: https://sochain.com/api#rate-limits (300 requests/minute)\n\nBitgo API rate limits: https://app.bitgo.com/docs/#section/Rate-Limiting (10 requests/second)\n\n### Extras\n\nCompiler options if there are compilation errors\n\n```js\ntsconfig.json\n{\n    \"compilerOptions\": {\n        \"module\":\"commonjs\",\n        \"target\": \"es5\",\n        \"noEmitOnError\": true,\n        \"lib\": [\"es6\", \"dom\", \"es2016\", \"es2017\"]\n    }\n}\n```\n\n\n\n","readmeFilename":"README.md"}