From 8c13a23b5fcd91e675a7cf361cb57b20039bffa5 Mon Sep 17 00:00:00 2001 From: timlucmiptev Date: Tue, 8 Dec 2020 11:56:32 +0200 Subject: [PATCH] working %create-raw-tx --- PROVIDER.md | 9 ++++++ app/btc-provider.hoon | 8 ++++++ app/btc-wallet-hook.hoon | 2 +- lib/btc-provider.hoon | 62 ++++++++++++++++++++++++++++++++++++++-- sur/btc-provider.hoon | 5 ++++ 5 files changed, 82 insertions(+), 4 deletions(-) diff --git a/PROVIDER.md b/PROVIDER.md index e3c1171a01..8187848b53 100644 --- a/PROVIDER.md +++ b/PROVIDER.md @@ -20,3 +20,12 @@ :btc-provider|action ['reqid' %tx-info [%32 0x9ece.9c56.9ab3.27db.ada5.a51e.2653.7b3f.7e99.5579.c18a.af4f.8620.304b.ce53.16f1]] :: 4 inputs, one output ``` + +## Generate a Raw TX from Inputs/Outputs +``` +:btc-provider|command [%set-credentials api-url='http://localhost:50002'] +=inputs ~[[[%32 0xe86.8771.b3bf.f789.525e.21ba.c735.e280.70d1.eff0.fb4d.f59a.dc98.e914.8e2f.85d4] 0]] +=outputs ~[[[%bech32 'bc1q0ydcskwye4rqky4qankhl4kegajl8nh50plmx0'] 12.500.000]] + +:btc-provider|action ['rawtx0' %create-raw-tx inputs outputs] +``` diff --git a/app/btc-provider.hoon b/app/btc-provider.hoon index 3dfe57df84..53f6b47df2 100644 --- a/app/btc-provider.hoon +++ b/app/btc-provider.hoon @@ -114,6 +114,9 @@ :: %raw-tx [%get-raw-tx txid.body.act] + :: + %create-raw-tx + [%create-raw-tx inputs.body.act outputs.body.act] :: %ping [%get-block-and-fee ~] @@ -173,6 +176,11 @@ ?> ?=([%get-raw-tx *] resp) :_ state ~[(send-update [%.y (get-req-id wire) %raw-tx +.resp])] + :: + [%create-raw-tx @ *] + ?> ?=([%get-raw-tx *] resp) + :_ state + ~[(send-update [%.y (get-req-id wire) %raw-tx +.resp])] :: [%ping @ *] ?> ?=([%get-block-and-fee *] resp) diff --git a/app/btc-wallet-hook.hoon b/app/btc-wallet-hook.hoon index ab6f221813..344d00d720 100644 --- a/app/btc-wallet-hook.hoon +++ b/app/btc-wallet-hook.hoon @@ -55,7 +55,7 @@ def ~(. (default-agent this %|) bowl) hc ~(. +> bowl) :: -++ on-initb +++ on-init ^- (quip card _this) ~& > '%btc-wallet-hook initialized' :_ this(fam-limit.state fam-limit:defaults) diff --git a/lib/btc-provider.hoon b/lib/btc-provider.hoon index b4c5ff5034..f2d3297aa6 100644 --- a/lib/btc-provider.hoon +++ b/lib/btc-provider.hoon @@ -22,6 +22,10 @@ == ~|("legacy addresses not yet supported" !!) [%bech32 addrc] +:: +++ txid-to-cord + |= =txid ^- cord + (en:base16:mimes:html txid) :: +from-epoch: time since Jan 1, 1970 in seconds. :: ++ from-epoch @@ -59,12 +63,52 @@ ^- request:http [%'GET' url ~ ~] :: +++ post-request + |= [url=@t body=json] + ^- request:http + :* %'POST' + url + ~[['Content-Type' 'application/json']] + =, html + %- some + %- as-octt:mimes + (en-json body) + == +:: ++ gen-request |= [=host-info ract=action:rpc] ^- request:http %+ rpc-action-to-http api-url.host-info ract :: +++ action-to-json + |= ract=action:rpc + |^ ^- json + =, enjs:format + ?+ -.ract ~|("Unsupported action for POST" !!) + %create-raw-tx + %- pairs + :~ [%inputs [%a (turn inputs.ract input)]] + [%outputs [%a (turn outputs.ract output)]] + == + == + :: + ++ input + |= [=txid pos=@ud] + ^- json + =, enjs:format + %- pairs + :~ [%txid [%s (txid-to-cord txid)]] + [%vout (numb pos)] + == + ++ output + |= [=address value=sats] + =, enjs:format + ^- json + %- frond + [(address-to-cord address) (numb value)] + -- +:: ++ to-response |= result:rpc ^- result @@ -85,6 +129,9 @@ :: %get-raw-tx [id.res (raw-tx res.res)] + :: + %create-raw-tx + [%get-raw-tx (raw-tx res.res)] :: %get-block-count [id.res (ni:dejs:format res.res)] @@ -136,24 +183,33 @@ ++ rpc-action-to-http |= [endpoint=@t ract=action:rpc] |^ ^- request:http - %- get-request ?- -.ract %get-address-info + %- get-request %+ mk-url '/addresses/info/' (address-to-cord address.ract) :: %get-tx-vals + %- get-request %+ mk-url '/gettxvals/' - (en:base16:mimes:html txid.ract) + (txid-to-cord txid.ract) :: %get-raw-tx + %- get-request %+ mk-url '/getrawtx/' - (en:base16:mimes:html txid.ract) + (txid-to-cord txid.ract) + :: + %create-raw-tx + %+ post-request + (mk-url '/createrawtx' '') + (action-to-json ract) :: %get-block-count + %- get-request (mk-url '/getblockcount' '') :: %get-block-and-fee + %- get-request (mk-url '/getblockandfee' '') == ++ mk-url diff --git a/sur/btc-provider.hoon b/sur/btc-provider.hoon index c023370191..bb812ceddb 100644 --- a/sur/btc-provider.hoon +++ b/sur/btc-provider.hoon @@ -11,9 +11,12 @@ $% [%address-info =address] [%tx-info =txid] [%raw-tx =txid] + [%create-raw-tx inputs=(list [=txid pos=@ud]) outputs=(list [=address value=sats])] [%ping ~] == +$ result [=req-id body=result-body] +:: %raw-tx: result type for both %raw-tx and %create-raw-tx actions +:: +$ result-body $% [%address-info utxos=(set utxo) used=? block=@ud] [%tx-info =info:tx] @@ -39,6 +42,7 @@ $% [%get-address-info =address] [%get-tx-vals =txid] [%get-raw-tx =txid] + [%create-raw-tx inputs=(list [=txid pos=@ud]) outputs=(list [=address value=sats])] [%get-block-count ~] [%get-block-and-fee ~] == @@ -47,6 +51,7 @@ $% [%get-address-info utxos=(set utxo) used=? block=@ud] [%get-tx-vals =info:tx] [%get-raw-tx =txid =rawtx] + [%create-raw-tx =rawtx] [%get-block-count block=@ud] [%get-block-and-fee block=@ud fee=sats]