PSBT docs; parse PSBTs

This commit is contained in:
timlucmiptev 2020-12-08 16:48:40 +02:00 committed by ixv
parent ecb40418e6
commit 1497b4127c
3 changed files with 48 additions and 14 deletions

28
PSBT.md
View File

@ -13,11 +13,37 @@ bitcoin-cli -rpcuser=__cookie__ -rpcpassword=9e0ca21c70d6b7307b750c8012d12df04ef
=btc -build-file %/lib/btc/hoon
=psbt 'cHNidP8BAKACAAAAAqsJSaCMWvfEm4IS9Bfi8Vqz9cM9zxU4IagTn4d6W3vkAAAAAAD+////qwlJoIxa98SbghL0F+LxWrP1wz3PFTghqBOfh3pbe+QBAAAAAP7///8CYDvqCwAAAAAZdqkUdopAu9dAy+gdmI5x3ipNXHE5ax2IrI4kAAAAAAAAGXapFG9GILVT+glechue4O/p+gOcykWXiKwAAAAAAAEHakcwRAIgR1lmF5fAGwNrJZKJSGhiGDR9iYZLcZ4ff89X0eURZYcCIFMJ6r9Wqk2Ikf/REf3xM286KdqGbX+EhtdVRs7tr5MZASEDXNxh/HupccC1AaZGoqg7ECy0OIEhfKaC3Ibi1z+ogpIAAQEgAOH1BQAAAAAXqRQ1RebjO4MsRwUPJNPuuTycA5SLx4cBBBYAFIXRNTfy4mVAWjTbr6nj3aAfuCMIAAAA'
(get-txid:psbt:btc psbt)
(get-txid:^psbt:btc psbt)
:: gives [wid=%32 dat=0xfed6.cd1f.de4d.b4e1.3e7e.8003.17e3.7f9c.bd75.ec36.4389.670e.eff8.0da9.93c7.e560]
```
## Parse PSBT into Maps
```
=btc -build-file %/lib/btc/hoon
=psbt 'cHNidP8BAKACAAAAAqsJSaCMWvfEm4IS9Bfi8Vqz9cM9zxU4IagTn4d6W3vkAAAAAAD+////qwlJoIxa98SbghL0F+LxWrP1wz3PFTghqBOfh3pbe+QBAAAAAP7///8CYDvqCwAAAAAZdqkUdopAu9dAy+gdmI5x3ipNXHE5ax2IrI4kAAAAAAAAGXapFG9GILVT+glechue4O/p+gOcykWXiKwAAAAAAAEHakcwRAIgR1lmF5fAGwNrJZKJSGhiGDR9iYZLcZ4ff89X0eURZYcCIFMJ6r9Wqk2Ikf/REf3xM286KdqGbX+EhtdVRs7tr5MZASEDXNxh/HupccC1AaZGoqg7ECy0OIEhfKaC3Ibi1z+ogpIAAQEgAOH1BQAAAAAXqRQ1RebjO4MsRwUPJNPuuTycA5SLx4cBBBYAFIXRNTfy4mVAWjTbr6nj3aAfuCMIAAAA'
(parse:^psbt:btc psbt)
```
## Making Derivation Paths
Below is hex for `m/84'/0'/0'/0/0`
`*80 is the ending for hardened paths*
54.00.00.80 00.00.00.80 00.00.00.80 00.00.00.00 00.00.00.00.00`
## ColdCard PSBT Export Structure
If we don't have the master fingerprint, can probably fake it. It's not "load-bearing".
Maps:
1. 1 keyval
- Raw tx hex
2. 3 keyvals (Input)
- Witness TX: {8byte amount}|{1 byte scriptpubkeylen}|{22byte scriptpubkey)}
- Non Witness TX: 82 byte input tx
- Wallet Derivation path.
* Key: {0x6}|{33byte pubkey}
* Val: {4byte master xpub fingerprint}|{17byte HD path}
3. skips the main output for some reason, even though it's in raw tx hex
4. 1 keyval
- Change output Wallet derivation path
* Key: {0x2}|{33byte pubkey}
* Val: {4byte master xpub fingerprint}|{17byte HD path}

View File

@ -70,13 +70,20 @@
|%
++ parse
|= psbt-base64=cord
^- (list (list keyval:^psbt))
~[~]
:: TODO:
:: - get into a buffer
:: loop through using next-pair
:: every time there's a separator, close the prior on
:: +txid: extract txid from a valid PSBT
^- (list map:^psbt)
=/ todo=^buffer
%+ slag 5 (to-buffer psbt-base64)
=| acc=(list map:^psbt)
=| m=map:^psbt
|-
?~ todo (snoc acc m)
:: 0x0: map separator
?: =(0x0 i.todo)
$(acc (snoc acc m), m *map:^psbt, todo t.todo)
=+ [kv rest]=(next-keyval todo)
$(m (snoc m kv), todo rest)
:: +get-txid: extract txid from a valid PSBT
::
++ get-txid
|= psbt-base64=cord
^- txid
@ -100,14 +107,14 @@
|- ^- btc-byts
?~ b !!
?: =(0x0 i.b) !!
=+ np=(next-pair b)
?: =(0x0 dat.key.kv.np)
val.kv.np
$(b rest.np)
:: +next-pair: returns next key-val in a PSBT map
=+ nk=(next-keyval b)
?: =(0x0 dat.key.kv.nk)
val.kv.nk
$(b rest.nk)
:: +next-keyval: returns next key-val in a PSBT map
:: input buffer head must be a map key length
::
++ next-pair
++ next-keyval
|= b=^buffer
^- [kv=keyval:^psbt rest=^buffer]
=+ klen=(snag 0 b)

View File

@ -56,6 +56,7 @@
++ psbt
|%
+$ keyval [key=btc-byts val=btc-byts]
+$ map (list keyval)
--
++ ops
|%