-
-
Save ChristianOConnor/2399a864f38666a2a3305b9ad7d374d1 to your computer and use it in GitHub Desktop.
Make private key into tweaked oridinal tapscript address (C#)
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
using System.Text; | |
using NBitcoin; | |
using NBitcoin.DataEncoders; | |
class Program | |
{ | |
static async Task Main() | |
{ | |
var network = Network.TestNet; | |
var hexKey = "5b10712a7799e29441f6c27918ff27af8a8f3707da14afd352730a99fb417033"; | |
//convert the hexKey to WIF | |
Key key = new Key(Encoders.Hex.DecodeData(hexKey)); | |
var marker = Encoding.UTF8.GetBytes("ord"); | |
var mimetype = Encoding.UTF8.GetBytes("text/plain"); | |
var inscription = Encoding.UTF8.GetBytes("gywJYnla8SOO5PrmVIXlcgXwAbOWlgPgDXjjAOY"); | |
//create spacer that is '01' string in hex | |
var spacer = Encoders.Hex.DecodeData("01"); | |
//create pubkey with the first 2 digits removed | |
var pubkeyWOfirst2 = key.PubKey.ToBytes().Skip(1).ToArray(); | |
var script = new TapScript(new Script(Op.GetPushOp(pubkeyWOfirst2), OpcodeType.OP_CHECKSIG, OpcodeType.OP_0, OpcodeType.OP_IF, Op.GetPushOp(marker), Op.GetPushOp(spacer), Op.GetPushOp(mimetype), OpcodeType.OP_0, Op.GetPushOp(inscription), OpcodeType.OP_ENDIF), TapLeafVersion.C0); | |
var taprootBuilderForAddrs = new TaprootBuilder(); | |
taprootBuilderForAddrs | |
.AddLeaf(0, script); | |
var info = taprootBuilderForAddrs.Finalize(key.PubKey.GetTaprootFullPubKey().InternalKey); | |
var merkleRootVar = info.MerkleRoot; | |
var tweakedKeyPair = key.CreateTaprootKeyPair(merkleRootVar); | |
Console.WriteLine($"Key tweaked taproot address: {tweakedKeyPair.PubKey.GetAddress(network)}"); | |
return; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment