Skip to content

Commit

Permalink
feat(grpc,node-wasm): add javascript bindings for tx client (#510)
Browse files Browse the repository at this point in the history
Signed-off-by: Maciej Zwoliński <[email protected]>
Co-authored-by: Mikołaj Florkiewicz <[email protected]>
Co-authored-by: Yiannis Marangos <[email protected]>
  • Loading branch information
3 people authored Jan 27, 2025
1 parent 0b12468 commit a2213af
Show file tree
Hide file tree
Showing 19 changed files with 1,042 additions and 75 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 52 additions & 1 deletion cli/js/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,52 @@
Error.stackTraceLimit = 99; // rust stack traces can get pretty big, increase the default

import { NodeConfig, spawnNode } from "lumina-node";
import { AppVersion, Blob, Namespace, NodeConfig, TxClient, protoEncodeSignDoc, spawnNode } from "lumina-node";
import { secp256k1 } from "@noble/curves/secp256k1";
import { Registry } from "@cosmjs/proto-signing";

// Expose classes on window so they can be used from the console
window.AppVersion = AppVersion;
window.Blob = Blob;
window.Namespace = Namespace;

// cat ci/credentials/bridge-0.address
window.bridge0Address = "celestia1t52q7uqgnjfzdh3wx5m5phvma3umrq8k6tq2p9";

async function createTxClient() {
// cat ci/credentials/bridge-0.plaintext-key
const privKey = "393fdb5def075819de55756b45c9e2c8531a8c78dd6eede483d3440e9457d839";
const pubKey = secp256k1.getPublicKey(privKey);

const signer = (signDoc) => {
const bytes = protoEncodeSignDoc(signDoc);
const sig = secp256k1.sign(bytes, privKey, { prehash: true });
return sig.toCompactRawBytes();
};

const txClient = await new TxClient(
"http://127.0.0.1:18080",
window.bridge0Address,
pubKey,
signer
);
return txClient;
}

async function submitBankMsgSend(address, amount) {
const registry = new Registry();
const sendMsg = {
typeUrl: "/cosmos.bank.v1beta1.MsgSend",
value: {
fromAddress: window.bridge0Address,
toAddress: address,
amount: [{ denom: "utia", amount: amount.toString() }],
},
};
const sendMsgAny = registry.encodeAsAny(sendMsg);
const txInfo = await window.txClient.submitMessage(sendMsgAny);

return txInfo;
}

async function showStats(node) {
if (!node || !await node.isRunning()) {
Expand Down Expand Up @@ -81,6 +127,7 @@ function stopped(document) {

async function main(document, window) {
window.node = await spawnNode();
window.txClient = await createTxClient();

window.events = await window.node.eventsChannel();
window.events.onmessage = (event) => {
Expand Down Expand Up @@ -114,6 +161,10 @@ async function main(document, window) {
await started(document, window);
}
});

// test submitting transfer
const txInfo = await submitBankMsgSend(window.bridge0Address, 10000);
console.log("Submitting bank MsgSend successful", txInfo);
}

await main(document, window);
Loading

0 comments on commit a2213af

Please sign in to comment.