Nym Smart Contracts
After having installed your client from the Contract Clients
(opens in a new tab) to query any of the Nym smart contracts, you can import the packages and execute some methods, signing them with a mnemonic:
import { contracts } from '@nymproject/contract-clients';
import { SigningCosmWasmClient } from "@cosmjs/cosmwasm-stargate";
import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";
async function main() {
// Generate a signer from a mnemonic
const signer = await DirectSecp256k1HdWallet.fromMnemonic("...");
const accounts = await signer.getAccounts();
// Make a signing client for the Nym Mixnet contract on mainnet
const cosmWasmSigningClient = await SigningCosmWasmClient.connectWithSigner("https://rpc.nymtech.net:443", signer);
const client = new contracts.Mixnet.MixnetClient(cosmWasmSigningClient, accounts[0].address, 'n17srjznxl9dvzdkpwpw24gg668wc73val88a6m5ajg6ankwvz9wtst0cznr');
// Delegate 1 NYM to mixnode with id 100
const result = await client.delegateToMixnode({ mixId: 100 }, 'auto', undefined, [{ amount: `${1_000_000}`, denom: 'unym' }]);
console.log(`Tx Hash = ${result.transactionHash}`);
};
Mixnet Client
After instantiating the Mixnet Client
(opens in a new tab), you can use it and send messages to yourself and output them in the console by following these steps:
import { createNymMixnetClient } from '@nymproject/sdk';
const main = async () => {
const nym = await createNymMixnetClient();
const nymApiUrl = 'https://validator.nymtech.net/api';
// Show message payload content when received
nym.events.subscribeToTextMessageReceivedEvent((e) => {
console.log('Got a message: ', e.args.payload);
});
// Start the client and connect to a gateway
await nym.client.start({
clientId: 'My awesome client',
nymApiUrl,
});
// Stop the client connection
const stop = async () => {
await nym?.client.stop();
};
// Send a message to yourself
const payload = 'Hello mixnet';
const recipient = nym.client.selfAddress();
nym.client.send({ payload, recipient });
};
MixFetch
Use the mixFetch
(opens in a new tab) package as a drop-in replacement for fetch
to send HTTP requests over the Nym Mixnet:
import { mixFetch } from '@nymproject/mix-fetch';
// HTTP GET
const response = await mixFetch('https://nymtech.net');
const html = await response.text();
// HTTP POST
const apiResponse = await mixFetch('https://api.example.com', {
method: 'POST',
body: JSON.stringify({ foo: 'bar' }),
headers: { [`Content-Type`]: 'application/json', Authorization: `Bearer ${AUTH_TOKEN}` }
});
Check the standard allowed list (opens in a new tab) to see
if the host you want to mixFetch
from is allowed.