diff --git a/components/forms/MultisigForm.js b/components/forms/MultisigForm.js index 1b64ef6..d8dabc8 100644 --- a/components/forms/MultisigForm.js +++ b/components/forms/MultisigForm.js @@ -84,10 +84,10 @@ class MultiSigForm extends React.Component { compressedPubkeys, this.state.threshold ); + this.props.router.push(`/multi/${multisigAddress}`); } catch (error) { - console.log(error); + console.log("Failed to creat multisig: ", error); } - this.props.router.push(`/multi/${multisigAddress}`); }; render() { diff --git a/components/forms/TransactionSigning.js b/components/forms/TransactionSigning.js index 73cefb3..ef5c31c 100644 --- a/components/forms/TransactionSigning.js +++ b/components/forms/TransactionSigning.js @@ -68,6 +68,8 @@ export default class TransactionSigning extends React.Component { this.props.tx.memo, signerData ); + // save body bytes to the tx + // save/create the signature in the db console.log(bodyBytes, signatures); } catch (error) { console.log(error); diff --git a/lib/graphqlHelpers.js b/lib/graphqlHelpers.js index 89457ce..2b34d4a 100644 --- a/lib/graphqlHelpers.js +++ b/lib/graphqlHelpers.js @@ -1,10 +1,37 @@ import axios from "axios"; // Graphql base request for Faunadb -const graphqlReq = axios; -graphqlReq.defaults.baseURL = "https://graphql.fauna.com/graphql"; -graphqlReq.defaults.headers.common = { - Authorization: `Bearer ${process.env.FAUNADB_SECRET}`, +const graphqlReq = axios.create({ + baseURL: "https://graphql.fauna.com/graphql", + headers: { + Authorization: `Bearer ${process.env.FAUNADB_SECRET}`, + }, +}); + +/** + * Creates multisig record in faunadb + * + * @param {object} multisig an object with address (string) and pubkey JSON + * @return Returns async function that makes a request to the faunadb graphql endpoint + */ +const createMultisig = async (multisig) => { + console.log(multisig); + return graphqlReq({ + method: "POST", + data: { + query: ` + mutation { + createMultisig(data: { + address: "${multisig.address}" + pubkeyJSON: ${JSON.stringify(multisig.pubkeyJSON)} + }) { + _id + address + } + } + `, + }, + }); }; /** @@ -82,13 +109,16 @@ const findTransactionByID = async (id) => { * @param {object} transaction The base transaction * @return Returns async function that makes a request to the faunadb graphql endpoint */ -const createSignature = async (transaction) => { +const createSignature = async (signature, transactionId) => { return graphqlReq({ method: "POST", data: { query: ` mutation { - createTransaction(data: {dataJSON: ${JSON.stringify(transaction)}}) { + createSignature(data: { + transaction: {connect: ${transactionId}}, + dataJSON: + }) { _id } } @@ -97,4 +127,4 @@ const createSignature = async (transaction) => { }); }; -export { getMultisig, createTransaction, findTransactionByID }; +export { createMultisig, getMultisig, createTransaction, findTransactionByID }; diff --git a/lib/multisigHelpers.js b/lib/multisigHelpers.js index 6ae983c..d709ce1 100644 --- a/lib/multisigHelpers.js +++ b/lib/multisigHelpers.js @@ -16,23 +16,27 @@ const createMultisigFromCompressedSecp256k1Pubkeys = async ( compressedPubkeys, threshold ) => { - let pubkeys = compressedPubkeys.map((compressedPubkey) => { - return { - type: "tendermint/PubKeySecp256k1", - value: compressedPubkey, + try { + let pubkeys = compressedPubkeys.map((compressedPubkey) => { + return { + type: "tendermint/PubKeySecp256k1", + value: compressedPubkey, + }; + }); + const multisigPubkey = createMultisigThresholdPubkey(pubkeys, threshold); + const multisigAddress = pubkeyToAddress(multisigPubkey, "cosmos"); + + // save multisig to fauna + const multisig = { + address: multisigAddress, + pubkeyJSON: JSON.stringify(multisigPubkey), }; - }); - - const multisigPubkey = createMultisigThresholdPubkey(pubkeys, threshold); - const multisigAddress = pubkeyToAddress(multisigPubkey, "cosmos"); - - // save multisig to fauna - const multisig = { - address: multisigAddress, - pubkeyJSON: JSON.stringify(multisigPubkey), - }; - await axios.post("/api/multisig", multisig); - return multisigAddress; + const res = await axios.post("/api/multisig", multisig); + console.log(res.data); + return res.data.address; + } catch (error) { + throw error; + } }; /** diff --git a/pages/api/multisig/index.js b/pages/api/multisig/index.js index 3d738f8..c89bee1 100644 --- a/pages/api/multisig/index.js +++ b/pages/api/multisig/index.js @@ -1,24 +1,15 @@ -import faunadb from "faunadb"; +import { createMultisig } from "../../../lib/graphqlHelpers"; export default async function (req, res) { return new Promise(async (resolve) => { switch (req.method) { case "POST": - const q = faunadb.query; - const client = new faunadb.Client({ - secret: process.env.FAUNADB_SECRET, - }); try { const data = req.body; console.log("Function `createMultisig` invoked", data); - const multisig = { - data: data, - }; - const faunaRes = await client.query( - q.Create(q.Collection("Multisig"), multisig) - ); - console.log("success", faunaRes); - res.status(200).send(faunaRes); + const saveRes = await createMultisig(data); + console.log("success", saveRes.data); + res.status(200).send(saveRes.data.data.createMultisig); return resolve(); } catch (err) { console.log(err);