1
0
mirror of https://github.com/danog/toncontest.git synced 2024-11-26 20:15:01 +01:00
toncontest/lightning/lib.fif
2019-10-15 19:13:27 +02:00

199 lines
4.4 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"TonUtil.fif" include
// b body -- b'
{ tuck <s 2dup s-fits? not rot over 1 i, -rot
{ drop swap ref, } { s, nip } cond
} : maybe-ref,
// filename -- c
{ dup ."Loading order from file " type ."..." cr
file>B B>boc
} : load-boc
// filename -- uint256
{ dup ."Loading public key from file " type ."..." cr
file>B dup Blen 32 <> abort"Public key must be exactly 32 bytes long"
256 B>u@
} : load-pubkey
' constant : const
' 2constant : 2const
// D n -- uint
{
0 -rot
{ drop swap 1+ swap -1 } dictmap drop
} : dictlen
// u D n -- s ? or ?
{
{ swap 2 pick = { nip 0 } { drop -1 } cond } dictforeach
dup ' nip if
not
} : dict[]
{ hole dup 1 ' @ does create 1 ' ! does create } : variable-set
{ hole hole 2dup 2 { @ swap @ swap } does create 2 { rot swap ! ! } does create } : 2variable-set
2variable-set wallet-addr wallet-addr!
variable-set delta delta!
variable-set message-contents message-contents!
variable-set sig-count sig-count!
variable-set sigA sigA!
variable-set sigB sigB!
variable-set final final!
// udata BSig ukey ?
{
256 u>B rot 256 u>B -rot ed25519_chksign
} : ed25519_chksignuu
// Inspect state
//
// state$_ signatures:MaybeTwoSignatures seqno:uint32 delta:Grams final:Bool {turnB:(seqno % 2)} = State;
//
// s --
{
// twoSignatures$_ a:^Signature b:^Signature = MaybeTwoSignatures;
// oneSignatureA$0 a:^Signature = MaybeTwoSignatures;
// oneSignatureB$1 b:^Signature = MaybeTwoSignatures;
dup srefs
2 = {
."Signature A: "
64 B@+ swap dup Bx. cr
sigA!
."Signature B: "
64 B@+ swap dup Bx. cr
sigB!
2 sig-count!
} {
1 sig-count!
1 u@+ swap
{
."Signature A: empty" cr
null sigA!
."Signature B: "
64 B@+ swap dup Bx. cr
sigB!
}
{
."Signature A: "
64 B@+ swap dup Bx. cr
sigA!
."Signature B: empty" cr
null sigB!
}
cond
} cond
32 u@+ swap
."Seqno: " . cr
Gram@+ swap
dup ."Delta grams: " . cr
delta!
1 u@+ swap
dup ."Final? " { ."Yes" } { ."No" } cond
final!
drop
} : inspect-state
// Inspects contents of cell provided on top of the stack
// c --
//
// defines/updates the following vars
//
// wallet-addr = u u
// message-hash = B
// message-contents = s
{
// Assuming the same external structure created by create.fif (simple external message)
//
// addr_none$00 = MsgAddressExt;
// addr_extern$01 len:(## 9) external_address:(bits len)
// = MsgAddressExt;
// anycast_info$_ depth:(#<= 30) { depth >= 1 }
// rewrite_pfx:(bits depth) = Anycast;
// addr_std$10 anycast:(Maybe Anycast)
// workchain_id:int8 address:bits256 = MsgAddressInt;
// addr_var$11 anycast:(Maybe Anycast) addr_len:(## 9)
// workchain_id:int32 address:(bits addr_len) = MsgAddressInt;
//
// ext_in_msg_info$10 src:MsgAddressExt dest:MsgAddressInt
// import_fee:Grams = CommonMsgInfo;
//
//
// message$_ {X:Type} info:CommonMsgInfo
// init:(Maybe (Either StateInit ^StateInit))
// body:(Either X ^X) = Message X;
//
// <b b{1000100} s, wallet-addr addr, 0 Gram, b{00} s,
// swap <s s, b>
// (external message)
//
// message$_ {X:Type} info:CommonMsgInfo
// init:(Maybe (Either StateInit ^StateInit))
// body:(Either X ^X) = Message X;
<s dup csr. cr
// External message
// ext_in_msg_info$10 src:MsgAddressExt (addr_none$00) dest:MsgAddressInt (addr_std$10 anycast 0) ... import_fee:Grams = CommonMsgInfo;
// 10 00 10 0
// 1000100 => 68
7 u@+ swap 68 <> { ."There seems to be an invalid header" cr } if
8 i@+
256 u@+ -rot
2dup wallet-addr!
."Wallet address: " .addr cr
Gram@+ nip // Ignore grams
1 u@+ swap // init:(Maybe
abort"This seems to be an init message"
// body:(Either X ^X)
1 u@+ swap
{ ref@ <s } if // Load ref
dup message-contents!
inspect-state
} : inspect
// storage$_ keyA:PubKey keyB:PubKey amountA:Grams amountB:Grams addresses:^SimpleAddrPair state:State = Storage X;
// c --
{
<s
256 u@+ swap
."Key A: " x. cr
256 u@+ swap
."Key B: " x. cr
Gram@+ swap
."Gram amount A: " x. cr
Gram@+ swap
."Gram amount B: " x. cr
ref@+
8 u@+
256 u@+
0
."Address A" .addr
8 u@+
256 u@+
0
."Address B" .addr
drop
."State:"
inspect-state
} : inspect-storage