arbitraryPartialTxs :: Gen ([Tx], [(ScriptOutput, OutPoint, Int, Int)])
arbitraryPartialTxs = do
    tx <- arbitraryEmptyTx
    res <-
        forM (map prevOutput $ txIn tx) $ \op -> do
            (so, rdmM, prvs, m, n) <- arbitraryData
            txs <- mapM (singleSig so rdmM tx op) prvs
            return (txs, (so, op, m, n))
    return (concatMap fst res, map snd res)
  where
    singleSig so rdmM tx op prv = do
        sh <- arbitraryValidSigHash
        let sigi = SigInput so op sh rdmM
        return . fromRight (error "Colud not decode transaction") $
            signTx tx [sigi] [prv]
    arbitraryData = do
        (m, n) <- arbitraryMSParam
        nPrv <- choose (m, n)
        keys <- vectorOf n arbitraryPubKey
        perm <- choose (0, length keys - 1)
        let pubKeys = map snd keys
            prvKeys = take nPrv $ permutations (map fst keys) !! perm
        let so = PayMulSig pubKeys m
        elements
            [ (so, Nothing, prvKeys, m, n)
            , ( PayScriptHash $ getAddrHash $ p2shAddr so
              , Just so
              , prvKeys
              , m
              , n)
            ]
arbitraryPartialTxs :: Gen ([Tx], [(ScriptOutput, OutPoint, Int, Int)])
arbitraryPartialTxs = do
    tx <- arbitraryEmptyTx
    res <-
        forM (map prevOutput $ txIn tx) $ \op -> do
            (so, rdmM, prvs, m, n) <- arbitraryData
            txs <- mapM (singleSig so rdmM tx op) prvs
            return (txs, (so, op, m, n))
    return (concatMap fst res, map snd res)
  where
    singleSig so rdmM tx op prv = do
        sh <- arbitraryValidSigHash
        let sigi = SigInput so op sh rdmM
        return . fromRight (error "Colud not decode transaction") $
            signTx tx [sigi] [prv]
    arbitraryData = do
        (m, n) <- arbitraryMSParam
        nPrv <- choose (m, n)
        keys <- vectorOf n arbitraryPubKey
        perm <- choose (0, length keys - 1)
        let pubKeys = map snd keys
            prvKeys = take nPrv $ permutations (map fst keys) !! perm
        let so = PayMulSig pubKeys m
        elements
            [ (so, Nothing, prvKeys, m, n)
            , ( PayScriptHash $ getAddrHash $ p2shAddr so
              , Just so
              , prvKeys
              , m
              , n)
            ]
arbitraryPartialTxs = do
    tx <- arbitraryEmptyTx
    res <-
        forM (map prevOutput $ txIn tx) $ \op -> do
            (so, rdmM, prvs, m, n) <- arbitraryData
            txs <- mapM (singleSig so rdmM tx op) prvs
            return (txs, (so, op, m, n))
    return (concatMap fst res, map snd res)
  where
    singleSig so rdmM tx op prv = do
        sh <- arbitraryValidSigHash
        let sigi = SigInput so op sh rdmM
        return . fromRight (error "Colud not decode transaction") $
            signTx tx [sigi] [prv]
    arbitraryData = do
        (m, n) <- arbitraryMSParam
        nPrv <- choose (m, n)
        keys <- vectorOf n arbitraryPubKey
        perm <- choose (0, length keys - 1)
        let pubKeys = map snd keys
            prvKeys = take nPrv $ permutations (map fst keys) !! perm
        let so = PayMulSig pubKeys m
        elements
            [ (so, Nothing, prvKeys, m, n)
            , ( PayScriptHash $ getAddrHash $ p2shAddr so
              , Just so
              , prvKeys
              , m
              , n)
            ]