{"_id":"@deanmlittle/rpuzzle","_rev":"1-250e3b76070b540670cbd239e89a1fbb","name":"@deanmlittle/rpuzzle","dist-tags":{"latest":"0.0.1"},"versions":{"0.0.1":{"name":"@deanmlittle/rpuzzle","version":"0.0.1","description":"A simple library to help you create, sign and spend any kind of R Puzzle.","main":"index.js","scripts":{"test":"echo \"Error: Tests coming soon (tm)\" && exit 1"},"repository":{"type":"git","url":"git+https://github.com/deanmlittle/rpuzzle.git"},"author":{"name":"Dean Little"},"license":"ISC","bugs":{"url":"https://github.com/deanmlittle/rpuzzle/issues"},"homepage":"https://github.com/deanmlittle/rpuzzle","peerDependencies":{"bsv":"^1.5.3"},"directories":{"example":"examples","lib":"lib","test":"tests"},"keywords":["BSV","RPuzzle"],"gitHead":"3e760b1692104835722be5066242dae198309523","_id":"@deanmlittle/rpuzzle@0.0.1","_nodeVersion":"13.7.0","_npmVersion":"6.13.6","dist":{"integrity":"sha512-zOWwOuec9btbtviXHpsG7nF+68odpeK3zV16pn62uU+p7MnvbTNbUknIeGVGzGTYGKvRwIv54Iy9bgkbmea6mA==","shasum":"228e90c18b2f744f1736aa92507839135c280b7c","tarball":"https://registry.npmjs.org/@deanmlittle/rpuzzle/-/rpuzzle-0.0.1.tgz","fileCount":8,"unpackedSize":24731,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJevZ4cCRA9TVsSAnZWagAA/qEP/2dMzCDmn61S5NAqMgI+\nTg1dwV0qd7+Hb8ssORbfxjOttLziatkYadky3UCigBDtwIwv5aDIotLUaqT2\nLV3w6qP4eebLiPnv3z7rfwgpcbsFZn5PFVFNyUqFqOBGuiVVU2inIqDZ+Puy\nQ/SfXrD9CL8ohSDYeo+EgdxFRZp6+6sW+zRfDZzn4o0jf6xY/5GQdZF2TZZZ\nAPOLnjkJsHZBYGONcF5HKCmy2UgHQaFfHc7fIVEFFtxGcSKdFZ2luSbbGKse\nN7d5nO8yo5qXzaFcaGx5hsrFtOPQjbrJYkQ/V1AX1NKLUmJrXeG3DrLLTfv0\nO0L2iUOj74YEAAA44H8vHK3h1w6B7gxj6EGPJfYRGX3B+jFjh5Yv5TPSXqy8\nqaaT095Dgze4Gca1fWZVsAx33mbOozKj7AOIp250AkZgTHSlVnOXkdDok0On\nFkQXfZwQT3Uk8giqG7oHNhe4oAV1wZ+tkM9bdx7Qnz0o4UCQK29vjTIMrUqX\ni6eR1hvxgksILp2G3daORwyZtb7wcbxiB0Z5loh+86EKwvfD8P10YZUAoF7H\njBnNOXWYNdytpQ55/v8bU/N+UlHQwTgfbDOM4mljfYAkhPxZblJzTBHNNS3P\nmCh3SDFbb+ZGltJqc0f97dRfT5nw34MPOAeCXVymtGTjc/kdcMCn0ZoPaCJo\nsauK\r\n=iQfl\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCAMKhDnTrla7CFqfl3GXGGBLlgXZcL4fUeNZgD77KczgIhAPkO1Wj7ODlFpatTLbZfw2gZU3GEqmlYyas3vio4Jdv/"}]},"maintainers":[{"name":"deanmlittle","email":"dean@uptimesv.com"}],"_npmUser":{"name":"deanmlittle","email":"dean@uptimesv.com"},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/rpuzzle_0.0.1_1589485084245_0.611809825922345"},"_hasShrinkwrap":false}},"time":{"created":"2020-05-14T19:38:04.098Z","0.0.1":"2020-05-14T19:38:04.366Z","modified":"2022-04-05T03:28:00.330Z"},"maintainers":[{"name":"deanmlittle","email":"dean@uptimesv.com"}],"description":"A simple library to help you create, sign and spend any kind of R Puzzle.","homepage":"https://github.com/deanmlittle/rpuzzle","keywords":["BSV","RPuzzle"],"repository":{"type":"git","url":"git+https://github.com/deanmlittle/rpuzzle.git"},"author":{"name":"Dean Little"},"bugs":{"url":"https://github.com/deanmlittle/rpuzzle/issues"},"license":"ISC","readme":"# RPuzzle\nA simple library to help you create, sign and spend any kind of R Puzzle.\n\n### Installation\n*Please note, RPuzzle has a peer dependency of `bsv` which must be installed independently to avoid version collisions.*\n\nTo get started, simply run:\n```npm i bsv https://github.com/deanmlittle/rpuzzle```\n\n### Quickstart\nIn true Bitcoin fashion, code is well-commented, poorly documented. You can create them randomly, from a PrivateKey, a buffer, a hex string or even an XPriv path. Here's an example of creating and spending a HASH160 P2RPH from Random:\n\n##### Step 1) Generating a locking script.\nWe're going to generate a valid R Puzzle script in ASM format and the K value used to unlock it. Make sure to save these two pieces of information as we'll need them to publish and unlock the puzzle later.\n```js\nconst RPuzzle = require('rpuzzle');\n\nconst rpuzzle = RPuzzle.fromRandom();\nconsole.log(rpuzzle.toASM());\nconsole.log(rpuzzle.k.toHex());\n\n//Returns: \n//k value hex string\n//OP_OVER OP_3 OP_SPLIT OP_NIP OP_1 OP_SPLIT OP_SWAP OP_SPLIT OP_DROP OP_HASH160 <r hash> OP_EQUALVERIFY OP_CHECKSIG\n```\n\n#### Step 2) Funding a UTXO\nOne of the simplest ways to do this is with MoneyButton. Copy your ASM script into the script property in `data-outputs` below:\n\n```html\n<script src=\"https://www.moneybutton.com/moneybutton.js\"></script>\n<div class='money-button'\n  data-outputs='[\n    {\n      \"script\": \"OP_OVER OP_3 OP_SPLIT OP_NIP OP_1 OP_SPLIT OP_SWAP OP_SPLIT OP_DROP OP_HASH160 <r hash> OP_EQUALVERIFY OP_CHECKSIG\",\n      \"amount\": \"0.0218\",\n      \"currency\": \"USD\"\n    }\n  ]'\n></div>\n```\n\nHere's a jsfiddle to make the whole process a little easier: https://jsfiddle.net/wfotksg6/\n\nSwipe to put the TX on chain, copy the TXID from your Moneybutton, then head over to: `http://api.whatsonchain.com/v1/bsv/main/<txid>/hex` to get the raw hex of the transaction.\n\n#### Step 3) Create an unlocking transaction\nRemember that K value hex string from step 1? We'll need that here:\n```js\nconst RPuzzle = require('rpuzzle');\nconst Transaction = require('bsv').Transaction;\nconst Script = require('bsv').Script;\nconst Address = require('bsv').Address;\n\n\n//Create an R puzzle from the K value\nconst rpuzzle = RPuzzle(RPuzzle.KValue.fromHex(\"k value hexstring\"));\n\n//Grab the UTXO output(s) we want to spend from the previous TX we made\nconst fromTX = Transaction(\"tx raw hex\");\nconst utxos = rpuzzle.getUTXOs(fromTX);\n\n//Create a new Transaction to spend these outputs\nlet toTx = Transaction();\ntoTx.from(utxos);\n\n//Get total input amount from UTXO(s)\nconst amount = toTx._getInputAmount();\ntoTx.to(Address.fromString('<a bitcoin address>'), amount-1000); //1000 sats is a pretty generous fee!\n\n//Sign it!\ntoTx = rpuzzle.sign(toTx);\n\n//Copy the raw tx hex so we can publish it!\nconsole.log(toTx.uncheckedSerialize());\n```\n\n#### Step 4) Broadcast transaction\nHead on over to https://whatsonchain.com/broadcast and paste in your new raw tx hex. You can either preview the TX to make sure everything looks correct (also not a bad way to figure out your fee rate), or assuming everything looks okay, hit the broadcast button and you're done!\n\n### Other examples\nYou can actually publish any kind of R Puzzle you want. Here's a transaction I made where I created one output of each type:\nhttps://whatsonchain.com/tx/1ea1d846d0e5212f28654ba0eeb1db346e38e245415bf90ccd70c95060cf7f80\n\nAnd subsequently spent them:\nhttps://whatsonchain.com/tx/a61e8444b717da21da2e6b5d6cc8b2cb724123bc6c8f3f5c088839ed4d2d5f33\n\n### Puzzle Types\n\nRPuzzle implements all of the native hashing functions of Bitcoin, defaulting to `OP_HASH160 <r>`. You can also just pay to the R value itself. To set a specific puzzle type, simply call `setType` on an RPuzzle instance like so:\n\n```js\nconst rpuzzle = RPuzzle.fromRandom();\nrpuzzle.setType('PayToRHASH256');\n```\n\nThe current type options include:\n\n| Puzzle Type | Script |\n| ------ | ------ |\n| PayToRHASH160 | OP_HASH160 <r> |\n| PayToRRIPEMD160 | OP_RIPEMD160 <r> |\n| PayToRSHA256 | OP_SHA256 <r> |\n| PayToRHASH256 | OP_HASH256 <r> |\n| PayToRSHA1 | OP_SHA1 <r> |\n| PayToR | <r> |\n\n### TODO:\n- Typescript definitions\n- Better examples\n- Multisig R Puzzles\n- Testnet testing (Yes, real devs code straight on Mainnet. It should work though with a bit of tweaking!)\n- SIGHASH_NONE and SIGHASH_ANYONECANPAY, haven't tried this at all.\n- Browser testing. Should work, may have Buffer issues? Let me know.\n\n### Development\n\nWant to contribute? Please do! Feel free to submit bugs or pull requests to this repo. \nYou can also tip me at `deanlittle@moneybutton.com`, `1deanlittle` or `/pay @40` on Twetch.\n\n### Legal stuff\nThis code is provided free of charge with no warranties. Use at your own risk. Besides that, I don't really care how you use it so long as you promise to go build something cool. ;)\n","readmeFilename":"readme.md"}