const express = require('@runkit/tyvdh/express-endpoint/1.0.0')
const sendError = require('@runkit/tyvdh/send-error/1.0.1')
const { Server, Utils, Transaction, Networks } = require('stellar-sdk')
const cors = require('cors')
const { max, chain } = require('lodash')
const app = express(exports)
const server = new Server('https://horizon-testnet.stellar.org')
app.use(cors())
app.get('/', check)
async function check(req, res) {
try {
const publicKey = req.query.publicKey
await server
.loadAccount(publicKey)
.then(async (account) => {
if (account.signers.length <= 1)
throw `Multisig hasn't been successfully added yet. Carefully read the instructions and try again.`
const maxThreshold = max([
account.thresholds.low_threshold,
account.thresholds.med_threshold,
account.thresholds.high_threshold
])
const maxSigner = chain(account.signers)
.filter((signer) => signer.key !== publicKey)
.maxBy('weight')
.value().weight
if (maxSigner > maxThreshold)
throw `Signer weight isn't high enough. Carefully read the instructions and try again.`
const { records } = await account.transactions({order: 'desc', limit: 100})
const multisigned = chain(records)
.map((record) => {
if (record.source_account !== publicKey)
return
const transaction = new Transaction(record.envelope_xdr, Networks.TESTNET)
return !Utils.verifyTxSignedBy(transaction, publicKey)
})
.compact()
.value()
if (multisigned.length) {
if (res)
res.status(204).send()
else
console.log('OK')
}
else
throw `Account hasn't submitted a transaction signed by a multisigner. Carefully read the instructions and try again.`
})
}
catch(err) {
if (res)
sendError(err, res)
else
console.error(err)
}
}
check({query: {publicKey: 'GCHT3DCDBM3GQKN772H2JDT4EVL3SY4ZLBSAJDNO6GS4SY2V5BUXGRYN'}})