How do you create a new TON wallet programmatically?
How do you create a new TON wallet programmatically?
In JavaScript, you can use the [`ton` package](https://github.com/ton-community/ton). I have adapted an example from a [community-led TON tutorial](https://ton-community.github.io/tutorials/01-wallet/). 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
```js
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();
```
In JavaScript, you can use the [`ton` package](https://github.com/ton-community/ton). I have adapted an example from a [community-led TON tutorial](https://ton-community.github.io/tutorials/01-wallet/). Please use at your own risk.
What it does is:
1. Generate multiple mnemonics
2. Fund the addresses of these mnemonics with a pre-funded wallet
3. Send a transaction out of these mnemonnics to help automatically deploy the contract
```js
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 ..."; // replace with 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();
```
How do you create a new TON wallet programmatically?
How do you create a new TON wallet programmatically?
In JavaScript, you can use the [`ton` package](https://github.com/ton-community/ton). I have adapted an example from a [community-led TON tutorial](https://ton-community.github.io/tutorials/01-wallet/). Please use at your own risk.
What it does is:
1. Generate multiple mnemonics
2. Fund the addresses of these mnemonics with a pre-funded wallet
3. Send a transaction out of these mnemonnics to help automatically deploy the contract
```js
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 ..."; // replace with 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();
```
In JavaScript, you can use the [`ton` package](https://github.com/ton-community/ton). They've provided this example as a way to generate a mnenomic and then deploy a wallet contract.
```js
import { TonClient, WalletContractV4, internal } from "ton";
import { mnemonicNew, mnemonicToPrivateKey } from "ton-crypto";
// Create Client
const client = new TonClient({
endpoint: 'https://toncenter.com/api/v2/jsonRPC',
});
// Generate new key
let mnemonics = await mnemonicNew();
let keyPair = await mnemonicToPrivateKey(mnemonics);
// Create wallet contract
let workchain = 0; // Usually you need a workchain 0
let wallet = WalletContractV4.create({ workchain, publicKey: keyPair.publicKey });
let contract = client.open(wallet);
```
How do you create a new TON wallet programmatically?
How do you create a new TON wallet programmatically?
In JavaScript, you can use the [`ton` package](https://github.com/ton-community/ton). They've provided this example as a way to generate a mnenomic and then deploy a wallet contract.
```js
import { TonClient, WalletContractV4, internal } from "ton";
import { mnemonicNew, mnemonicToPrivateKey } from "ton-crypto";
// Create Client
const client = new TonClient({
endpoint: 'https://toncenter.com/api/v2/jsonRPC',
});
// Generate new key
let mnemonics = await mnemonicNew();
let keyPair = await mnemonicToPrivateKey(mnemonics);
// Create wallet contract
let workchain = 0; // Usually you need a workchain 0
let wallet = WalletContractV4.create({ workchain, publicKey: keyPair.publicKey });
let contract = client.open(wallet);
```
In JavaScript, you can use the [`ton` package](https://github.com/ton-community/ton). They've provided this example as a way to generate a mnenomic and then deploy a wallet conract.
```js
import { TonClient, WalletContractV4, internal } from "ton";
import { mnemonicNew, mnemonicToPrivateKey } from "ton-crypto";
// Create Client
const client = new TonClient({
endpoint: 'https://toncenter.com/api/v2/jsonRPC',
});
// Generate new key
let mnemonics = await mnemonicNew();
let keyPair = await mnemonicToPrivateKey(mnemonics);
// Create wallet contract
let workchain = 0; // Usually you need a workchain 0
let wallet = WalletContractV4.create({ workchain, publicKey: keyPair.publicKey });
let contract = client.open(wallet);
```
How do you create a new TON wallet programmatically?
In JavaScript, you can use the [`ton` package](https://github.com/ton-community/ton). They've provided this example as a way to generate a mnenomic and then deploy a wallet conract.
```js
import { TonClient, WalletContractV4, internal } from "ton";
import { mnemonicNew, mnemonicToPrivateKey } from "ton-crypto";
// Create Client
const client = new TonClient({
endpoint: 'https://toncenter.com/api/v2/jsonRPC',
});
// Generate new key
let mnemonics = await mnemonicNew();
let keyPair = await mnemonicToPrivateKey(mnemonics);
// Create wallet contract
let workchain = 0; // Usually you need a workchain 0
let wallet = WalletContractV4.create({ workchain, publicKey: keyPair.publicKey });
let contract = client.open(wallet);
```