Examples: query, "exact match", wildcard*, wild?ard, wild*rd
Fuzzy search: cake~ (finds cakes, bake)
Term boost: "red velvet"^4, chocolate^2
Field grouping: tags:(+work -"fun-stuff")
Escape special characters +-&|!(){}[]^"~*?:\ - e.g. \+ \* \!
Range search: properties.timestamp:[1587729413488 TO *] (inclusive), properties.title:{A TO Z}(excluding A and Z)
Combinations: chocolate AND vanilla, chocolate OR vanilla, (chocolate OR vanilla) NOT "vanilla pudding"
Field search: properties.title:"The Title" AND text
Answered
How do you create a new TON wallet programmatically?

Is there an API that can create a new TON wallet using code?

Creating and deploying are two different steps. So how I can create many uninitialized wallet addresses and then later deploy and initialize them one by one?


This question was imported from Telegram Chat: https://t.me/tondev/41699

Votes Newest

Answers 2


In JavaScript, you can use the ton package. I have adapted an example from a community-led TON tutorial. Please use at your own risk.

What it does is:

  1. Generate multiple mnemonics
  2. Find the addresses of the generated mnemonics
  3. Fund the addresses of these mnemonics with a pre-funded wallet
  4. Send a transaction out of these mnemonnics to automatically deploy the contract
import { getHttpEndpoint } from "@orbs-network/ton-access";
import { mnemonicToWalletKey, mnemonicNew } from "ton-crypto";
import { TonClient, WalletContractV4, internal } from "ton";

async function main() {
    // open wallet v4 (set your correct wallet version here)
    const mnemonic = "unfold sugar water ..."; // insert a mnemonic for a wallet that has funds
    const key = await mnemonicToWalletKey(mnemonic.split(" "));
    const fundingWallet = WalletContractV4.create({ publicKey: key.publicKey, workchain: 0 });

    for(let i = 0; i < 5; i++) {
        let m = await mnemonicNew();
        console.log(m);
        await initializeWallet(fundingWallet, m);
    }
}

async function initializeWallet(fundingWallet: WalletContractV4, mnemonic: string[]) {
  // open wallet v4 (notice the correct wallet version here)
  const key = await mnemonicToWalletKey(mnemonic);
  const generatedWallet = WalletContractV4.create({ publicKey: key.publicKey, workchain: 0 });

  // initialize ton rpc client on testnet
  const endpoint = await getHttpEndpoint({ network: "testnet" });
  const client = new TonClient({ endpoint });

  // send 0.1 TON from funding wallet to new wallet
  let walletContract = client.open(generatedWallet);
  let seqno = await walletContract.getSeqno();
  await walletContract.sendTransfer({
    secretKey: key.secretKey,
    seqno: seqno,
    messages: [
      internal({
        to: generatedWallet.address, 
        value: "0.09", // 0.001 TON
        bounce: false,
      })
    ]
  });

  // send 0.9 back TON to funding wallet
  walletContract = client.open(generatedWallet);
  seqno = await walletContract.getSeqno();
  await walletContract.sendTransfer({
    secretKey: key.secretKey,
    seqno: seqno,
    messages: [
      internal({
        to: fundingWallet.address, 
        value: "0.09", // 0.001 TON
        bounce: false
      })
    ]
  });

  await waitForTransaction(seqno, walletContract);
}

async function waitForTransaction(seqno: number, walletContract: any) {
    // wait until confirmed
    let currentSeqno = seqno;
    while (currentSeqno == seqno) {
    console.log("waiting for transaction to confirm...");
    await sleep(1500);
        currentSeqno = await walletContract.getSeqno();
    }
    console.log("transaction confirmed!");
}

function sleep(ms: number) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

main();
2
2
Posted one year ago
Edited one year ago
Jeremy
404 × 5 Administrator
  
  

nice nice

Howard   one year ago Report
  
  

Tell me please. How I can get transaction hash after sendTransfer ?

No Name   9 months ago Report
  
  

Won't work.

let walletContract = client.open(generatedWallet);
  let seqno = await walletContract.getSeqno();
  await walletContract.sendTransfer({
    secretKey: key.secretKey,
    seqno: seqno,
    messages: [
      internal({
        to: generatedWallet.address, 
        value: "0.09", // 0.001 TON
        bounce: false,
      })
    ]
  });

// This will try to send frome generatedWallet to generatedWallet, you need to open fundingwallet instead and use its key.

No Name   4 months ago Report

You can use the TonWeb javascript SDK.

const tonweb = new TonWeb();

const wallet = tonweb.wallet.create({publicKey});

const address = await wallet.getAddress();

const nonBounceableAddress = address.toString(true, true, false);

const seqno = await wallet.methods.seqno().call();

await wallet.deploy(secretKey).send(); // deploy wallet to blockchain

const fee = await wallet.methods.transfer({
    secretKey,
    toAddress: 'EQDjVXa_oltdBP64Nc__p397xLCvGm2IcZ1ba7anSW0NAkeP',
    amount: TonWeb.utils.toNano(0.01), // 0.01 TON
    seqno: seqno,
    payload: 'Hello',
    sendMode: 3,
}).estimateFee();

const Cell = TonWeb.boc.Cell;
const cell = new Cell();
cell.bits.writeUint(0, 32);
cell.bits.writeAddress(address);
cell.bits.writeGrams(1);
console.log(cell.print()); // print cell data like Fift
const bocBytes = cell.toBoc();

const history = await tonweb.getTransactions(address);

const balance = await tonweb.getBalance(address);

tonweb.sendBoc(bocBytes);
26K Views
2 Answers
2 years ago
one year ago
Tags