Last active
January 10, 2024 15:57
-
-
Save supertestnet/8df4b12812dd5a66ce965e0ae2f2d550 to your computer and use it in GitHub Desktop.
How to do arbitrary data script with p2tr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://mutinynet.com/tx/5e2be1456d1917338729449e60913781fe12b008b5b32cd76eacb953f66bd636 | |
```html | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, user-scalable=no"> | |
<script src="https://cdn.jsdelivr.net/gh/6502/sha256@main/sha256.js"></script> | |
<script src="https://unpkg.com/@cmdcode/[email protected]"></script> | |
<script src="https://bundle.run/[email protected]"></script> | |
<style> | |
* { | |
box-sizing: border-box; | |
font-size: 1.15rem; | |
font-family: Arial, sans-serif; | |
} | |
html { | |
max-width: 800px; | |
padding: 3rem 1rem; | |
margin: auto; | |
line-height: 1.25; | |
padding: 0; | |
} | |
body { | |
margin: 0; | |
} | |
h1 { | |
font-size: 2rem; | |
} | |
h2 { | |
font-size: 1.5rem; | |
} | |
input { | |
line-height: 1.25; | |
width: 100%; | |
height: 1.8rem; | |
font-size: 1.15rem; | |
border: 1px solid grey; | |
} | |
.texto { | |
margin-top: 10rem; | |
word-wrap: break-word; | |
margin: 3rem; | |
} | |
@media screen and (max-width: 600px) { | |
} | |
</style> | |
<script> | |
var $ = document.querySelector.bind( document ); | |
var $$ = document.querySelectorAll.bind( document ); | |
var url_params = new URLSearchParams( window.location.search ); | |
var url_keys = url_params.keys(); | |
var $_GET = {} | |
for ( var key of url_keys ) $_GET[ key ] = url_params.get( key ); | |
</script> | |
<script> | |
var SHA256 = string_or_uint8array => bytesToHex( sha256( string_or_uint8array ) ); | |
var bytesToHex = bytes => bytes.reduce( ( str, byte ) => str + byte.toString( 16 ).padStart( 2, "0" ), "" ); | |
var hexToBytes = hex => Uint8Array.from( hex.match( /.{1,2}/g ).map( byte => parseInt( byte, 16 ) ) ); | |
var runscript = async () => { | |
var privkey = bytesToHex( nobleSecp256k1.utils.randomPrivateKey() ); | |
var pubkey = nobleSecp256k1.getPublicKey( privkey, true ).substring( 2 ); | |
var keyhash = SHA256( hexToBytes( pubkey ) ); | |
var script = [ "this is some arbitrary data", 'OP_VERIFY', 'OP_DUP', 'OP_SHA256', keyhash, 'OP_EQUALVERIFY', 'OP_CHECKSIG' ]; | |
var tapleaf = tapscript.Tap.encodeScript(script); | |
var [ tpubkey, cblock ] = tapscript.Tap.getPubKey(pubkey, { target: tapleaf }) | |
var address = tapscript.Address.p2tr.fromPubKey(tpubkey, 'testnet'); | |
console.log( "deposit at least 10k sats to this address:" ); | |
console.log( address ); | |
var txid = prompt( `deposit at least 10k sats to this address (which is also in your console):\n\n${address}\n\nthen enter the txid of your deposit` ); | |
var vout = prompt( "and the vout" ); | |
vout = Number( vout ); | |
var amt = prompt( "and the amount in sats" ); | |
amt = Number( amt ); | |
var txdata = tapscript.Tx.create({ | |
vin: [{ | |
txid: txid, | |
vout: vout, | |
prevout: { | |
value: amt, | |
scriptPubKey: tapscript.Address.toScriptPubKey( address ), | |
}, | |
}], | |
vout: [{ | |
value: amt - 500, | |
scriptPubKey: tapscript.Address.toScriptPubKey( 'bcrt1q6zpf4gefu4ckuud3pjch563nm7x27u4ruahz3y' ), | |
}], | |
}); | |
var sig = tapscript.Signer.taproot.sign( privkey, txdata, 0, { extension: tapleaf }).hex; | |
txdata.vin[ 0 ].witness = [ sig, pubkey, script, cblock ]; | |
var isValid = await tapscript.Signer.taproot.verify( txdata, 0, { pubkey }); | |
console.log( "sig is valid, right?", isValid ); | |
var txhex = tapscript.Tx.encode( txdata ).hex; | |
var div = document.createElement( "div" ); | |
div.innerHTML = `broadcast this tx:<br>${txhex}`; | |
$( '.texto' ).append( div ); | |
} | |
</script> | |
</head> | |
<body> | |
<div class="texto"></div> | |
<script> | |
runscript(); | |
</script> | |
</body> | |
</html> | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment