shrub/base/ape/bit/core.hook
2015-05-28 16:07:03 -07:00

228 lines
6.7 KiB
Plaintext

::
::
::::
::
/? 314
/- *talk, *bit-api
/+ talk, sole, http, mean
!:
:::: sivtyv-barnel
:: be sure to have oauth2-code markk
|%
++ axle
$: cred=(unit ,[app-secret=@t client-id=@t])
oauth-code=(unit ,@t)
auth=(unit ,[access=@t refresh=@t])
contacts=(map ,@t ,@t)
outgoing=(map ship ,@t) incoming=(map ,@t ship)
==
++ move ,[bone note]
++ hapt ,[ship path]
++ note
$% [%send wire [ship term] %poke mesa]
[%them wire (unit hiss)]
[%hiss wire mark bit-any]
==
++ gift
$% [%nice ~]
[%mean ares]
==
++ sign
$% [%thou p=httr]
[%unto %coup p=(unit tang)]
==
++ mesa
$% [%bit-addr-request ~]
[%bit-addr-made @t]
[%talk-command command]
==
--
::
!:
|_ [hid=hide vat=axle]
:: ++ prep ,_`.
++ return [*(list move) .]
++ redirect-uri [hostname `/~~/home/pub/bit/fab ~]
++ hostname ^- hart
?+ (clan our.hid) !!
%czar [| ?+(our.hid !! %~zod `8.443, %~fyr `8.444) `/localhost]
%duke [| ~ `/org/urbit/(crip +:<our.hid>)]
==
++ auth-url
%+ weld "https://www.coinbase.com/oauth/authorize"
%- tail:earn :~
'response_type'^%code
'client_id'^client-id:(need cred.vat)
'redirect_uri'^(crip (earn redirect-uri))
'scope'^'user balance buy sell send transactions addresses'
'meta[send_limit_amount]'^'1'
'meta[send_limit_curency]'^'BTC'
'meta[send_limit_period]'^'day'
==
++ old-api
|= [ost=bone pour-path=wire end-point=path req=$|(%get [%post p=json])]
^- move
%^ httpreq ost pour-path
[/com/coinbase/api v1/end-point req ~ ['access_token' access:(need auth.vat)]~]
::
++ api-call
|= [ost=bone res=mark req=bit-api-call] ^+ return
~| %missing-access-token
[[ost %hiss / res bit-api-call/[access:(need auth.vat) req]]~ +>.$]
::
++ print
|= [ost=bone msg=tape] ^+ return
[[(publish ost our.hid [%lin & (crip msg)]~)]~ +>.$]
::
++ callback-uri [hostname [`%json /~/to/[app.hid]/json] ~[anon/'' wire/'/']]
++ poke-auth
|= [[ost=bone you=ship] arg=[secr=cord id=cord]]
=. cred.vat `arg
=+ prl=`purl`(need (epur (crip auth-url)))
:_ +>.$
:- %^ publish ost you
:~ [%lin & 'secret and client id saved successfully']
[%lin & 'please click on the url below to continue authentication']
[%url prl]
==
~
::
++ sigh-tang |=([^ err=tang] (mean err))
++ send-friend :: inter-app interface
|= [ost=bone way=wire his=ship mez=$%([%bit-addr-request ~] [%bit-addr-made @t])]
^- move
[ost %send way [his app.hid] %poke mez]
::
++ poke-bit-send :: send to address
|=([[ost=bone @] req=bit-send] (api-call ost %bit-transaction send/req))
::
++ sigh-bit-transaction
|= [[ost=bone ^] trid=@t]
(print ost "transaction success {(trip trid)}")
::
++ poke-bit-ship-send :: send to ship
|= [[ost=bone @] to=ship amount=@t]
=. outgoing.vat (~(put by outgoing.vat) to amount)
:_ +>.$
:_ ~
(send-friend ost /sent to %bit-addr-request ~)
++ poke-bit-addr-request :: give your address to receive bit
|= [[ost=bone his=ship] req=~] :: gen new address with callback
:_ +>.$
:_ ~
%^ old-api ost /addr-request/(scot %p his)
:+ /addresses %post
%+ joba %address
%- jobe :~
label/(jape "address for {<his>}")
'callback_url'^(jape (earn callback-uri))
==
::
++ pour-addr-request :: send new address to sender
|= [ost=bone pax=path rez=json]
=+ adr=(need ((ot address/so ~):jo rez))
=+ his=(slav %p +<.pax)
~! incoming.vat
=. incoming.vat
(~(put by incoming.vat) adr his)
:_ +>.$
:_ ~
(send-friend ost [%message ~] his %bit-addr-made adr)
::
++ poke-bit-addr-made :: receive address for sending
|= [[ost=bone his=ship] addr=@t]
=+ amount=(~(got by outgoing.vat) his) :: get amount to send
(api-call ost %bit-transaction txt-send/[addr amount])
::
++ poke-oauth2-code
|= [[ost=bone you=ship] code=cord]
=. oauth-code.vat
[~ code]
?< ?=(~ cred.vat)
=+ req=[code [client-id app-secret]:u.cred.vat redirect-uri]
[[ost %hiss / %oauth-tokens bit-get-token/req]~ +>.$]
::
++ sigh-oauth-tokens
|= [then toke=[access=@t refresh=@t]]
=. auth.vat `toke
[[(publish ost our.hid [%lin & 'authenticated.']~) ~] +>.$]
::
++ poke-buy
|= [[ost=bone you=ship] arg=[amount=@t currency=@t]]
?< =(amount.arg ~)
(api-call ost %json %buy arg)
::
++ poke-sell
|= [[ost=bone you=ship] arg=[amount=@t currency=@t]]
?< =(amount.arg ~)
(api-call ost %json %sell arg)
::
++ sigh-json |=([^ a=json] ~&(a return))
++ poke-list |=([[ost=bone @] ~] (api-call ost %bit-accounts %list ~))
++ sigh-bit-accounts |=([^ acc=bit-accounts] ~&(accounts=acc return))
++ poke-send-raw
|= $: [ost=bone you=ship]
arg=[to=@t amone=(unit ,@t) cur=(unit ,@t) amtwo=(unit ,@t) nt=(unit ,@t)]
==
=+ adr=?~((~(get by contacts.vat) to.arg) to ~&('contact-exists' (~(get by contacts.vat) to.arg)))
=+ ^= info
?~ cur.arg
[to/s/to.arg amount/s/(need amone.arg) ~]
?~ nt.arg
[to/s/to.arg 'amount_currency_iso'^s/(need cur.arg) 'amount_string'^s/(need amtwo.arg) ~]
[to/s/to.arg amount/s/(need amtwo.arg) 'amount_string'^s/(need amtwo.arg) ~]
=+ ^= pst
(jobe transaction/(jobe info) ~)
[[(old-api ost /send /transactions/'send_money' %post pst) ~] +>.$]
::
++ poke-json
|= [[ost=bone you=ship] arg=json]
~& arg
:_ +>.$
=+ [adr amo]=(need %.(arg (ot address/so amount/no ~):jo))
=+ frm=(~(get by incoming.vat) adr)
:_ ~
~! frm
(publish ost our.hid [%lin & (crip "just received {(trip amo)} BTC from {<frm>}")]~)
::(old-api ost /get-id /transactions/[transaction-id] %get)
++ pour-get-id :: display transaction info
|= [ost=bone pax=path rez=json]
:_ +>.$
:_ ~
!!
::
++ thou
|= [[ost=bone @ way=wire] res=httr]
?+ -.way !!
%message `+>.$
?(%addr-request %get-id)
~| 'must receive a 200'
~| res
?> =(200 p.res)
%- ?-(-.way %addr-request pour-addr-request, %get-id pour-get-id)
[ost way (rash q:(need r.res) apex:poja)]
%get-id
~& [`@tas`-.way res]
:_ +>.$
~
==
::
++ publish
|= [ost=bone you=ship act=(list speech)]
^- move
=+ ^= thotz
%+ turn act
|= sp=speech ^- thought
=+ ^= tail
:- ^- audience
:+ :- `partner`[%& our.hid ?+((clan our.hid) !! %czar %court, %duke %porch)]
^- (pair envelope delivery)
[`envelope`[& ~] %pending]
~
~
`statement`[lat.hid ~ sp]
:- `@`(sham eny.hid tail)
tail
[ost %send /auth [our.hid %talk] %poke [%talk-command %publish thotz]]
--