mirror of
https://github.com/ilyakooo0/urbit.git
synced 2025-01-05 22:03:50 +03:00
TX signing works, fixed network selection bug in PSBTs
This commit is contained in:
parent
5e421a23c2
commit
f8bfd5748b
7
DEMO.md
7
DEMO.md
@ -59,7 +59,7 @@ On both `~zod`/`dopzod`, choose depending on whether you're on test or main
|
|||||||
|
|
||||||
`~dopzod`
|
`~dopzod`
|
||||||
```
|
```
|
||||||
:btc-wallet-hook|action [%req-pay-address ~zod 3.000 feyb=[~ 30]]
|
:btc-wallet-hook|action [%req-pay-address ~zod 30.000 feyb=[~ 10]]
|
||||||
```
|
```
|
||||||
|
|
||||||
### Check State on ~zod/~dopzod
|
### Check State on ~zod/~dopzod
|
||||||
@ -83,6 +83,11 @@ Or can change amount:
|
|||||||
:btc-wallet-hook|action [%req-pay-address ~zod 3.000 feyb=[~ 100]]
|
:btc-wallet-hook|action [%req-pay-address ~zod 3.000 feyb=[~ 100]]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Broadcast the Signed TX
|
||||||
|
```
|
||||||
|
:btc-wallet-hook|action [%broadcast-tx tx]
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
## Scan a Real Xpub
|
## Scan a Real Xpub
|
||||||
```
|
```
|
||||||
|
@ -206,6 +206,9 @@
|
|||||||
::
|
::
|
||||||
++ send-status
|
++ send-status
|
||||||
|= =status ^- card
|
|= =status ^- card
|
||||||
|
%- ?: ?=(%new-block -.status)
|
||||||
|
~&(>> "%new-block: {<block.status>}" same)
|
||||||
|
same
|
||||||
[%give %fact ~[/clients] %btc-provider-status !>(status)]
|
[%give %fact ~[/clients] %btc-provider-status !>(status)]
|
||||||
::
|
::
|
||||||
++ send-update
|
++ send-update
|
||||||
@ -213,7 +216,7 @@
|
|||||||
^- card
|
^- card
|
||||||
=+ c=[%give %fact ~[/clients] %btc-provider-update !>(update)]
|
=+ c=[%give %fact ~[/clients] %btc-provider-update !>(update)]
|
||||||
?: ?=(%.y -.update)
|
?: ?=(%.y -.update)
|
||||||
~& >> "prov. update: {<p.update>}"
|
:: ~& >> "prov. update: {<p.update>}"
|
||||||
c
|
c
|
||||||
~& >> "prov. err: {<p.update>}"
|
~& >> "prov. err: {<p.update>}"
|
||||||
c
|
c
|
||||||
|
@ -200,7 +200,10 @@
|
|||||||
:_ state
|
:_ state
|
||||||
=+ pb=~(to-psbt txb:bwsl u.poym)
|
=+ pb=~(to-psbt txb:bwsl u.poym)
|
||||||
?~ pb ~
|
?~ pb ~
|
||||||
~& >> "PSBT: {<u.pb>}"
|
=+ vb=~(vbytes txb:bwsl u.poym)
|
||||||
|
=+ fee=~(fee txb:bwsl u.poym)
|
||||||
|
~& >> "{<vb>} vbytes, {<(div fee vb)>} sats/byte, {<fee>} sats fee"
|
||||||
|
%- (slog [%leaf "PSBT: {<u.pb>}"]~)
|
||||||
~[(send-update [%sign-tx u.poym])]
|
~[(send-update [%sign-tx u.poym])]
|
||||||
::
|
::
|
||||||
%close-pym
|
%close-pym
|
||||||
|
@ -68,9 +68,20 @@
|
|||||||
(roll (turn txos.t |=(=txo value.txo)) add)
|
(roll (turn txos.t |=(=txo value.txo)) add)
|
||||||
::
|
::
|
||||||
++ fee
|
++ fee
|
||||||
|
^- sats:btc
|
||||||
=/ [in=sats out=sats] value
|
=/ [in=sats out=sats] value
|
||||||
(sub in out)
|
(sub in out)
|
||||||
::
|
::
|
||||||
|
++ vbytes
|
||||||
|
^- vbytes:btc
|
||||||
|
%+ add overhead-weight:btc
|
||||||
|
%+ add
|
||||||
|
%+ roll
|
||||||
|
(turn txis.t |=(t=txi (input-weight:btc bipt.hdkey.t)))
|
||||||
|
add
|
||||||
|
%+ roll
|
||||||
|
(turn txos.t |=(t=txo (output-weight:btc (address-bipt:btc address.t))))
|
||||||
|
add
|
||||||
++ tx-data
|
++ tx-data
|
||||||
|^
|
|^
|
||||||
^- data:tx:btc
|
^- data:tx:btc
|
||||||
@ -101,6 +112,7 @@
|
|||||||
++ add-output
|
++ add-output
|
||||||
|= =txo
|
|= =txo
|
||||||
^- txbu
|
^- txbu
|
||||||
|
:: todo update vbytes
|
||||||
t(txos (snoc [txos.t] txo))
|
t(txos (snoc [txos.t] txo))
|
||||||
:: +to-psbt: returns a based 64 PSBT if
|
:: +to-psbt: returns a based 64 PSBT if
|
||||||
:: - all inputs have an associated rawtx
|
:: - all inputs have an associated rawtx
|
||||||
@ -135,7 +147,7 @@
|
|||||||
++ hdkey
|
++ hdkey
|
||||||
|= =idx:btc
|
|= =idx:btc
|
||||||
^- hdkey:btc
|
^- hdkey:btc
|
||||||
[fprint.w (~(pubkey wad w chyg) idx) bipt.w chyg idx]
|
[fprint.w (~(pubkey wad w chyg) idx) network.w bipt.w chyg idx]
|
||||||
::
|
::
|
||||||
++ mk-address
|
++ mk-address
|
||||||
|= =idx:btc
|
|= =idx:btc
|
||||||
@ -221,25 +233,7 @@
|
|||||||
++ dust-threshold
|
++ dust-threshold
|
||||||
|= output-bipt=bipt:btc
|
|= output-bipt=bipt:btc
|
||||||
^- vbytes
|
^- vbytes
|
||||||
(mul dust-sats (input-weight output-bipt))
|
(mul dust-sats (input-weight:btc output-bipt))
|
||||||
++ meta-weight 11
|
|
||||||
++ output-weight
|
|
||||||
|= b=bipt:btc
|
|
||||||
^- vbytes
|
|
||||||
?- b
|
|
||||||
%44 34
|
|
||||||
%49 32
|
|
||||||
%84 31
|
|
||||||
==
|
|
||||||
::
|
|
||||||
++ input-weight
|
|
||||||
|= b=bipt:btc
|
|
||||||
^- vbytes
|
|
||||||
?- b
|
|
||||||
%44 148
|
|
||||||
%49 91
|
|
||||||
%84 68
|
|
||||||
==
|
|
||||||
::
|
::
|
||||||
++ target-value
|
++ target-value
|
||||||
^- sats
|
^- sats
|
||||||
@ -248,24 +242,24 @@
|
|||||||
::
|
::
|
||||||
++ base-weight
|
++ base-weight
|
||||||
^- vbytes
|
^- vbytes
|
||||||
%+ add meta-weight
|
%+ add overhead-weight:btc
|
||||||
%+ roll
|
%+ roll
|
||||||
%+ turn txos
|
%+ turn txos
|
||||||
|=(=txo (output-weight (address-bipt:btc address.txo)))
|
|=(=txo (output-weight:btc (address-bipt:btc address.txo)))
|
||||||
add
|
add
|
||||||
::
|
::
|
||||||
++ total-vbytes
|
++ total-vbytes
|
||||||
|= selected=(list insel)
|
|= selected=(list insel)
|
||||||
^- vbytes
|
^- vbytes
|
||||||
%+ add base-weight
|
%+ add base-weight
|
||||||
(mul (input-weight bipt.w) (lent selected))
|
(mul (input-weight:btc bipt.w) (lent selected))
|
||||||
:: value of an input after fee
|
:: value of an input after fee
|
||||||
:: 0 if net is <= 0
|
:: 0 if net is <= 0
|
||||||
::
|
::
|
||||||
++ net-value
|
++ net-value
|
||||||
|= val=sats
|
|= val=sats
|
||||||
^- sats
|
^- sats
|
||||||
=/ cost (mul (input-weight bipt.w) feyb)
|
=/ cost (mul (input-weight:btc bipt.w) feyb)
|
||||||
?: (lte val cost) 0
|
?: (lte val cost) 0
|
||||||
(sub val cost)
|
(sub val cost)
|
||||||
::
|
::
|
||||||
@ -284,7 +278,7 @@
|
|||||||
?~ tb [~ ~]
|
?~ tb [~ ~]
|
||||||
=+ excess=~(fee txb u.tb) :: (inputs - outputs)
|
=+ excess=~(fee txb u.tb) :: (inputs - outputs)
|
||||||
=/ new-fee=sats :: cost of this tx + one more output
|
=/ new-fee=sats :: cost of this tx + one more output
|
||||||
(mul feyb (add (output-weight bipt.w) vbytes.u.tb))
|
(mul feyb (add (output-weight:btc bipt.w) vbytes.u.tb))
|
||||||
?. (gth excess new-fee)
|
?. (gth excess new-fee)
|
||||||
[tb ~]
|
[tb ~]
|
||||||
?. (gth (sub excess new-fee) (dust-threshold bipt.w))
|
?. (gth (sub excess new-fee) (dust-threshold bipt.w))
|
||||||
|
21
lib/btc.hoon
21
lib/btc.hoon
@ -20,6 +20,25 @@
|
|||||||
::
|
::
|
||||||
:- (div (lent (trip h)) 2)
|
:- (div (lent (trip h)) 2)
|
||||||
`@ux`(rash - hex)
|
`@ux`(rash - hex)
|
||||||
|
::
|
||||||
|
++ overhead-weight ^-(vbytes 11)
|
||||||
|
++ input-weight
|
||||||
|
|= =bipt
|
||||||
|
^- vbytes
|
||||||
|
?- bipt
|
||||||
|
%44 148
|
||||||
|
%49 91
|
||||||
|
%84 68
|
||||||
|
==
|
||||||
|
++ output-weight
|
||||||
|
|= =bipt
|
||||||
|
^- vbytes
|
||||||
|
?- bipt
|
||||||
|
%44 34
|
||||||
|
%49 32
|
||||||
|
%84 31
|
||||||
|
==
|
||||||
|
::
|
||||||
++ xpub-type
|
++ xpub-type
|
||||||
|= =xpub
|
|= =xpub
|
||||||
^- [=bipt =network]
|
^- [=bipt =network]
|
||||||
@ -310,7 +329,7 @@
|
|||||||
:~ fprint.h
|
:~ fprint.h
|
||||||
%- to-byts:buf
|
%- to-byts:buf
|
||||||
:~ `@ux`bipt.h 0x0 0x0 0x80
|
:~ `@ux`bipt.h 0x0 0x0 0x80
|
||||||
0x0 0x0 0x0 0x80
|
?:(?=(%main network.h) 0x0 0x1) 0x0 0x0 0x80
|
||||||
0x0 0x0 0x0 0x80
|
0x0 0x0 0x0 0x80
|
||||||
`@ux`chyg.h 0x0 0x0 0x0
|
`@ux`chyg.h 0x0 0x0 0x0
|
||||||
==
|
==
|
||||||
|
31
psbt_sign.js
31
psbt_sign.js
@ -1,20 +1,21 @@
|
|||||||
/*
|
|
||||||
Usage:
|
|
||||||
var p = require("./psbt_sign.js")
|
|
||||||
const psbt = PSBT_STRING_BASE64
|
|
||||||
const mnemonics = MNEMONICS_STRING
|
|
||||||
const [psbt, mnemonics] = []
|
|
||||||
p.run(mnemonics, psbt)
|
|
||||||
*/
|
|
||||||
|
|
||||||
const bitcoin = require('bitcoinjs-lib');
|
const bitcoin = require('bitcoinjs-lib');
|
||||||
const ecpair = bitcoin.ECPair;
|
const ecpair = bitcoin.ECPair;
|
||||||
const crypto = bitcoin.crypto;
|
const crypto = bitcoin.crypto;
|
||||||
const bscript = bitcoin.script;
|
const bscript = bitcoin.script;
|
||||||
const bip39 = require('bip39');
|
const bip39 = require('bip39');
|
||||||
const NETWORK = bitcoin.networks.bitcoin;
|
|
||||||
const LOCK_TIME = 0;
|
const LOCK_TIME = 0;
|
||||||
const SIGHASH_ALL = 1;
|
const SIGHASH_ALL = 1;
|
||||||
|
let NETWORK;
|
||||||
|
|
||||||
|
/*
|
||||||
|
Usage:
|
||||||
|
var p = require("./psbt_sign.js")
|
||||||
|
var psbt = PSBT_STRING_BASE64
|
||||||
|
const mnemonics = MNEMONICS_STRING
|
||||||
|
|
||||||
|
p.run("TESTNET", mnemonics, psbt)
|
||||||
|
p.run("MAIN", mnemonics, psbt)
|
||||||
|
*/
|
||||||
|
|
||||||
const isSegwitTx = (rawTx) => {
|
const isSegwitTx = (rawTx) => {
|
||||||
return rawTx.substring(8, 12) === '0001';
|
return rawTx.substring(8, 12) === '0001';
|
||||||
@ -22,7 +23,15 @@ const isSegwitTx = (rawTx) => {
|
|||||||
|
|
||||||
// const PATHS = ["m/84'/0'/0'/0/0", "m/84'/0'/0'/1/0"];
|
// const PATHS = ["m/84'/0'/0'/0/0", "m/84'/0'/0'/1/0"];
|
||||||
|
|
||||||
const run = async(mnemonics, psbtStr) => {
|
const run = async(network, mnemonics, psbtStr) => {
|
||||||
|
let NETWORK;
|
||||||
|
if (network === "MAIN") {
|
||||||
|
NETWORK = bitcoin.networks.bitcoin;
|
||||||
|
}
|
||||||
|
else if (network = "TESTNET") {
|
||||||
|
NETWORK = bitcoin.networks.testnet;
|
||||||
|
}
|
||||||
|
|
||||||
const psbt = bitcoin.Psbt.fromBase64(psbtStr);
|
const psbt = bitcoin.Psbt.fromBase64(psbtStr);
|
||||||
const is = psbt.data.inputs;
|
const is = psbt.data.inputs;
|
||||||
const seed = await bip39.mnemonicToSeed(mnemonics);
|
const seed = await bip39.mnemonicToSeed(mnemonics);
|
||||||
|
@ -74,11 +74,11 @@
|
|||||||
outputs=(list [=val:tx s=(unit ship)])
|
outputs=(list [=val:tx s=(unit ship)])
|
||||||
==
|
==
|
||||||
+$ history (map hexb hest)
|
+$ history (map hexb hest)
|
||||||
:: state/watch variables:
|
:: state/watch variables:
|
||||||
:: scanning addresses and monitoring generated addresses
|
:: scanning addresses and monitoring generated addresses
|
||||||
:: batch: indexes to scan for a given chyg
|
:: batch: indexes to scan for a given chyg
|
||||||
:: scans: all scans underway (batches)
|
:: scans: all scans underway (batches)
|
||||||
:: piym-watch: any address we've been told has an incoming payment promised
|
:: piym-watch: any address we've been told has an incoming payment promised
|
||||||
::
|
::
|
||||||
+$ batch [todo=(set idx) endpoint=idx has-used=?]
|
+$ batch [todo=(set idx) endpoint=idx has-used=?]
|
||||||
+$ scans (map [xpub chyg] batch)
|
+$ scans (map [xpub chyg] batch)
|
||||||
@ -87,7 +87,6 @@
|
|||||||
:: %address-info: give new data about an address.
|
:: %address-info: give new data about an address.
|
||||||
:: - used: address has been seen on the BTC blockchain?
|
:: - used: address has been seen on the BTC blockchain?
|
||||||
:: - block: the most recent block at the time of this information being retrieved
|
:: - block: the most recent block at the time of this information being retrieved
|
||||||
:: TODO: document
|
|
||||||
::
|
::
|
||||||
+$ action
|
+$ action
|
||||||
$% [%add-wallet =xpub =fprint scan-to=(unit scon) max-gap=(unit @ud) confs=(unit @ud)]
|
$% [%add-wallet =xpub =fprint scan-to=(unit scon) max-gap=(unit @ud) confs=(unit @ud)]
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
+$ bipt $?(%44 %49 %84)
|
+$ bipt $?(%44 %49 %84)
|
||||||
+$ chyg $?(%0 %1)
|
+$ chyg $?(%0 %1)
|
||||||
+$ idx @ud
|
+$ idx @ud
|
||||||
+$ hdkey [=fprint pubkey=hexb =bipt =chyg =idx]
|
+$ hdkey [=fprint pubkey=hexb =network =bipt =chyg =idx]
|
||||||
+$ sats @ud
|
+$ sats @ud
|
||||||
+$ vbytes @ud
|
+$ vbytes @ud
|
||||||
+$ buffer (list @ux)
|
+$ buffer (list @ux)
|
||||||
|
Loading…
Reference in New Issue
Block a user