#!/usr/bin/env -S fift -s "TonUtil.fif" include "lib.fif" include { ."usage: " @' $0 type ." [ ...] " cr ."Merges multisig .boc files and writes result to .boc" cr 1 halt } : usage $# 3 < ' usage if $# 1 - const input-len 1 { dup $() +".boc" swap 1+ } input-len times drop input-len tuple const input-files $# $() +".boc" const output-file ."Inspecting and merging " input-len . ."files..." cr input-files explode 1- swap load-boc inspect cr message-hash const previous-hash wallet-addr 2const previous-address // multiSigWrapper$0 signatures:(HashmapE 4 Signature) message:(WrappedMessage X) = MultiSigWrapper X; message-contents 1 u@+ nip // $0 was already verified dict@+ // Read signatures s>c const wrapped-message // Store rest of message in const swap // put length on top, signatures right underneath { swap // Get filename, signatures right underneath load-boc inspect cr message-hash previous-hash <> abort"Hash mismatch!" // I suppose it's ok if there are multiple wallet instances with the same messages on different workchains/at different addresses, we're all agreeing to sign the same message anyway // wallet-address previous-address rot <> abort"Wallet address mismatch!" <> abort"Wallet address mismatch (different workchain)!" message-contents 1 u@+ nip // $0 was already verified dict@ // Read signatures 4 { drop s, -1 } dictmerge } swap times // Signatures are the only element on the stack ."Final message signed by the following keys: " dup 4 { drop . ."- " -1 } dictforeach cr drop // multiSigWrapper$0 signatures:(HashmapE 4 Signature) message:(WrappedMessage X) = MultiSigWrapper X; // Renerate external message 2 boc+>B dup Bx. cr output-file tuck B>file ."Saved new multisigned message to file " type cr