Skip to content

Commit

Permalink
Add wallet.js
Browse files Browse the repository at this point in the history
  • Loading branch information
giansalex committed May 4, 2022
1 parent f456635 commit 8474fd2
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 87 deletions.
99 changes: 12 additions & 87 deletions gnoland/website/static/js/realm_help.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,17 @@ function main() {
var el = u(e.currentTarget);
var sender = el.data("sender");
var msg = el.data("msg");
sendTx(sender, JSON.parse(msg));
if (!window.keplr) {
alert("Please install keplr extension.");
return;
}

walletFn.keplr.signAndBroadcast(sender, JSON.parse(msg)).then(function (result) {
alert("Tx: " + result.txHash);
}).catch(function (err) {
console.log(err);
alert("Error: " + err);
});
});
};

Expand Down Expand Up @@ -107,95 +117,10 @@ function updateCommand(x) {

// set keplr params
var keplrExec = x.find(".keplr_exec");
const msg = createMsg(myAddr, realmPath, funcName, vals);
const msg = walletFn.createVmCallMsg(myAddr, realmPath, funcName, vals);
keplrExec.data({msg: JSON.stringify(msg), sender: myAddr});
}

function createMsg(sender, pkgPath, funcName, args) {
return {
type: "/vm.m_call",
value: {
caller: sender,
send: "",
pkg_path: pkgPath,
func: funcName,
args: args,
}
};
}

function createSignDoc(account, msg, chainId, gas) {
return {
msgs: [msg],
fee: { amount: [{
amount: "1",
denom: "gnot"
}], gas: gas },
chain_id: chainId,
memo: "",
account_number: account.account_number,
sequence: account.sequence,
};
}

function sendTx(sender, msg) {
// validate params

// get account info.
var account = {account_number: 0, sequence: 0};
const signDoc = createSignDoc(account, msg, "testchain", "2000000");
const chainId = "testchain";
window.keplr.enable(chainId).then(function () {
return keplr.signAmino(chainId, sender, signDoc, {
preferNoSetFee: true, // 1gnot fixed fee
});
}).then(function (signature) {
const tx = makeStdTx(signature.signed, signature.signature);
return broadcastTx(tx);
}).then(function (result) {
console.log(result.txHash);
}).catch(function (err) {
console.log(err);
});
}

function makeStdTx(content, signature) {
const feeAmount = content.fee.amount;

return {
msg: content.msgs.map(function (msg) {
return {
"@type": msg.type,
...msg.value
};
}),
fee: {
gas_wanted: content.fee.gas,
gas_fee: feeAmount.length ? `${content.fee.amount[0].amount}${content.fee.amount[0].denom}`: "",
},
signatures: [{
pub_key: {
"@type": signature.pub_key.type,
value: signature.pub_key.value,
},
signature: signature.signature,
}],
memo: content.memo,
};
}

function broadcastTx(tx) {
return fetch("/txs", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(tx)
}).then(function (response) {
return response.json();
});
}

// Jae: why isn't this a library somewhere?
function shq(s) {
var s2 = String(s).replace(/\t/g, '\\t');
Expand Down
107 changes: 107 additions & 0 deletions gnoland/website/static/js/wallet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
var remote = "http://gno.land:36657",
chainId = "testchain",
walletFn = {keplr: {}};

walletFn.createVmCallMsg = function (sender, pkgPath, funcName, args) {
return {
type: "/vm.m_call",
value: {
caller: sender,
send: "",
pkg_path: pkgPath,
func: funcName,
args: args,
}
};
};

walletFn.createSignDoc = function(account, msg, chainId, gas) {
return {
msgs: [msg],
fee: { amount: [{
amount: "1",
denom: "gnot"
}], gas: gas },
chain_id: chainId,
memo: "",
account_number: account.account_number,
sequence: account.sequence,
};
};

walletFn.keplr.signAndBroadcast = function(sender, msg) {
return window.keplr.enable(chainId).then(function () {
return walletFn.getAccount(sender);
})
.then(function(account) {
const signDoc = walletFn.createSignDoc(account, msg, chainId, "2000000");
return window.keplr.signAmino(chainId, sender, signDoc, {
// use app fee (1gnot fixed fee)
preferNoSetFee: true,
});
})
.then(function (signature) {
const tx = walletFn.makeStdTx(signature.signed, signature.signature);
return walletFn.broadcastTx(tx);
});
};

walletFn.makeStdTx = function(content, signature) {
const feeAmount = content.fee.amount;

return {
msg: content.msgs.map(function (msg) {
return {
"@type": msg.type,
...msg.value
};
}),
fee: {
gas_wanted: content.fee.gas,
gas_fee: feeAmount.length ? `${content.fee.amount[0].amount}${content.fee.amount[0].denom}`: "",
},
signatures: [{
pub_key: {
"@type": signature.pub_key.type,
value: signature.pub_key.value,
},
signature: signature.signature,
}],
memo: content.memo,
};
};

walletFn.getAccount = function(address) {
return walletFn.abciQuery(`auth/accounts/${address}`)
.then(function (data) {
const response = data.result.response.ResponseBase;
if (response.Error) {
throw new Error(response.Log);
}

const account = JSON.parse(atob(response.Data));
if (!account) {
throw new Error("Account not found");
}

return account.BaseAccount;
});
};

walletFn.abciQuery = function(path) {
return fetch(`${remote}/abci_query?path="${path}"`).then(function(response) {
return response.json();
});
};

walletFn.broadcastTx = function(tx) {
return fetch("/txs", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(tx)
}).then(function (response) {
return response.json();
});
};
1 change: 1 addition & 0 deletions gnoland/website/views/realm_help.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<link rel="stylesheet" href="/static/css/app.css"/>
<script src="/static/js/umbrella.min.js"></script>
<script src="/static/js/marked.min.js"></script>
<script src="/static/js/wallet.js"></script>
<script src="/static/js/realm_help.js"></script>
</head>
<body onload="main()">
Expand Down

0 comments on commit 8474fd2

Please sign in to comment.