We give a tutorial below for creating an Aptos wallet using our npm module:
Setup & Installation
Create a new directory and cd to it. Execute the following commands through the command line:
npm init -y
npm install --save @martiandao/aptos-web3.js
This adds the aptos-web3.js library in your node modules directory.
Add wallet.js code
Create wallet.js file in the working directory and paste the following code in it. The comments on the code are self-explanatory.
const aptosWeb3 = require(‘@martiandao/aptos-web3.js’);
async function main() {
// create a connection with Aptos RPC node
client = new aptosWeb3.RestClient(aptosWeb3.TESTNET_URL);
// create new wallet (10 test tokens airdropped by default)
console.log(“\n=== Wallet Creation ===”);
const wallet = await aptosWeb3.createWallet();
const address = wallet[‘address key’];
const signingKey = wallet[‘code’];
console.log(‘Address:’, address);
console.log(‘Secret recovery phrase:’, signingKey);
// airdrop test tokens
console.log(“\n=== Airdrop ===”);
await aptosWeb3.airdrop(wallet[‘code’],5000);
console.log(‘Balance:’, await aptosWeb3.getBalance(address));
// transfer tokens
console.log(“\n=== Transfer ===”);
const receiver_address = “d27307a2ccf76b4694c2b8f2ddf032fd487dbc82cb32e8b264026a9aee14df8d”;
await aptosWeb3.transfer(signingKey, receiver_address, 420);
console.log(‘Balance:’, await aptosWeb3.getBalance(address));
// get wallet transaction history
console.log(“\n=== Wallet history ===”);
const received_history = await aptosWeb3.getReceivedEvents(address);
const sent_history = await aptosWeb3.getSentEvents(address);
console.log(‘Received History:’, received_history);
console.log(‘Sent History:’, sent_history);
}
main()
Run the code
node wallet.js
Further blogs in this series would include a Token example and an NFT example. Stay tuned!