diff --git a/BTC.scratch.md b/BTC.scratch.md index e92134b2d..9b6e840d9 100644 --- a/BTC.scratch.md +++ b/BTC.scratch.md @@ -140,6 +140,18 @@ process child keen cargo design install parrot hold pole unveil dance reason dri 0x3.109a.2082.eaa6.8925.1465.5393.d635.7fb9.d9b5.e191.3826.8837.69cd.db88.7a4b.b4f0 ``` +## TX Hashing +Final TXID: `0x33f.693f.df99.5a5e.a7fe.5c95.1ab6.858c.7e6a.5fff.c585.7992.2cd4.fc31.9c61.4c5b` +Can lookup in blockchain as: +033f693fdf995a5ea7fe5c951ab6858c7e6a5fffc58579922cd4fc319c614c5b +``` +=btc -build-file %/lib/btc/hoon +=in0 [%32^0x3d6a.f8a8.32b2.ef3f.82f4.dd9c.ccc9.a5af.23db.dbe9.2eae.86f0.7b06.fd41.9525.62b5 62 1 4^0xffff.ffff ~ ~ 10.950.904] +=out0 [address=[%bech32 'bc1q59u5epktervh6fxqay2dlph0wxu9hjnx6v8n66'] value=10.944.414] + +(get-id:txu:btc (encode:txu:btc [~[in0] ~[out0] 0 1])) +``` + ## Unsigned Transactions Using [BIP 143](https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki#P2SHP2WPKH) as a reference. diff --git a/lib/btc.hoon b/lib/btc.hoon index 77dab48e1..17d37527f 100644 --- a/lib/btc.hoon +++ b/lib/btc.hoon @@ -33,42 +33,70 @@ %- ripemd-160 %- sha256 [(met 3 pubkey) pubkey] :: -++ to-script-pubkey - |= script=buffer ^- buffer - %- zing - :~ ~[0x19 0x76 0xa9 0x14] - script - ~[0x88 0xac] - == -++ address-to-script-pubkey - |= =address ^- buffer - =/ hex=byts +++ script-pubkey + |= =address + ^- bytc + =/ h=bytc ?- -.address %bech32 (to-hex:bech32 address) + :: TODO: implement legacy %legacy - =/ h=@ux (@ux +.address) - [(met 3 h) h] + ~|("legacy addresess not supported to script-pubkey yet" !!) == - ?. =(wid.hex 20) - ~|("Only 20-byte addresses supported" !!) - (to-script-pubkey (from-byts:buf hex)) + %- cat:byt + :~ 1^(add 2 wid.h) + 1^0x0 + 1^wid.h + h + == :: +txu: tx utility functions ++ txu |% ++ en - |= data:tx + |% + ++ input + |= i=input:tx + ^- bytc + %- cat:byt + :~ (flip:byt txid.i) + (flip:byt 4^pos.i) + 1^0x0 + (flip:byt sequence.i) + == + :: + ++ output + |= o=output:tx + ^- bytc + %- cat:byt + :~ (flip:byt 8^value.o) + (script-pubkey address.o) + == + -- + ++ encode + |= =data:tx ^- bytc - *bytc - ++ de + %- cat:byt + %- zing + :~ :~ (flip:byt 4^nversion.data) + 1^(lent is.data) + == + (turn is.data input:en) + ~[1^(lent os.data)] + (turn os.data output:en) + ~[(flip:byt 4^locktime.data)] + == + ++ get-id + |= b=bytc + ^- txid + %- txid + %- flip:byt + (dsha256 b) + ++ decode |= b=bytc ^- data:tx *data:tx :: TODO parse out signatures - ++ get-id - |= b=bytc - ^- txid - (dsha256 b) -- :: core to handle BIP174 PSBTs :: @@ -437,7 +465,8 @@ :: goes from a bech32 address to hex. Returns byts to preserve leading 0s :: ++ to-hex - |= b=bech32-address ^- hash + |= b=bech32-address + ^- bytc =/ d=(unit raw-decoded) (decode-raw b) ?~ d ~|("Invalid bech32 address" !!) =/ bs=(list @) diff --git a/sur/btc.hoon b/sur/btc.hoon index 39b6dbdce..ce38cfbf7 100644 --- a/sur/btc.hoon +++ b/sur/btc.hoon @@ -33,7 +33,7 @@ $: is=(list input) os=(list output) locktime=@ud - witness=@ud + nversion=@ud == +$ val $: =txid @@ -48,19 +48,13 @@ inputs=(list val) outputs=(list val) == - +$ unsigned - $: version=@ - locktime=@ - inputs=(list input) - outputs=(list val) - == +$ input $: =txid pos=@ud witness-ver=@ud - sequence=byts - redeem-script=(unit byts) - pubkey=(unit byts) + sequence=bytc + redeem-script=(unit bytc) + pubkey=(unit bytc) value=sats == +$ output