const account = Keypair.generate()
console.log(JSON.stringify(account.publicKey.toBase58()))
// output: "gVazpxjimX3EP4mto53pEi4YSE36KP2nDwyEvLcKjmR"
const mnemonic =
'pill tomorrow foster begin walnut borrow virtual kick shift mutual shoe scatter'
const seed = bip39.mnemonicToSeedSync(mnemonic, '') // (mnemonic, password)
// BIP39
const keypair = Keypair.fromSeed(seed.slice(0, 32))
// BIP44
for (let i = 0; i < 10; i++) {
const path = `m/44'/501'/${i}'/0'`
const keypair = Keypair.fromSeed(derivePath(path, seed.toString('hex')).key)
console.log(`${path} => ${keypair.publicKey.toBase58()}`)
}
// Check if a given public key has an associated private key
const key = new PublicKey('5oNDL3swdJJF1g9DzJiZ4ynHXgszjAEpUkxVYejchzrY')
console.log(PublicKey.isOnCurve(key.toBytes()))
const message = "The quick brown fox jumps over the lazy dog";
const messageBytes = decodeUTF8(message);
const signature = nacl.sign.detached(messageBytes, keypair.secretKey);
const result = nacl.sign.detached.verify(
messageBytes,
signature,
keypair.publicKey.toBytes()
);
console.log(result);
-------------------------------------------------------------------------------
{
let recoverTx = Transaction.populate(Message.from(realDataNeedToSign));
recoverTx.addSignature(feePayer.publicKey, Buffer.from(feePayerSignature));
recoverTx.addSignature(alice.publicKey, Buffer.from(aliceSignature));
console.log(
`txhash: ${await connection.sendRawTransaction(recoverTx.serialize())}`
);
}
-------------------------------------------------------------------------------
{
let recoverTx = Transaction.populate(Message.from(realDataNeedToSign), [
bs58.encode(feePayerSignature),
bs58.encode(aliceSignature),
]);
console.log(
`txhash: ${await connection.sendRawTransaction(recoverTx.serialize())}`
);
}
const transferTransaction = new Transaction().add(
SystemProgram.transfer({
fromPubkey: fromKeypair.publicKey,
toPubkey: toKeypair.publicKey,
lamports: lamportsToSend,
}),
)
await transferTransaction.add(
new TransactionInstruction({
keys: [
{ pubkey: fromKeypair.publicKey, isSigner: true, isWritable: true },
],
data: Buffer.from('Data to send in transaction', 'utf-8'),
programId: new PublicKey('MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr'),
}),
)
await sendAndConfirmTransaction(connection, transferTransaction, [fromKeypair])
getEstimatedFee
const recentBlockhash = await connection.getLatestBlockhash()
const transaction = new Transaction({
recentBlockhash: recentBlockhash.blockhash,
}).add(
SystemProgram.transfer({
fromPubkey: payer.publicKey,
toPubkey: payee.publicKey,
lamports: 10,
}),
)
const fees = await transaction.getEstimatedFee(connection)
console.log(`Estimated SOL transfer cost: ${fees} lamports`)
// Estimated SOL transfer cost: 5000 lamports
// getFeeForMessage
const message = new Message(messageParams)
const fees = await connection.getFeeForMessage(message)
console.log(`Estimated SOL transfer cost: ${fees.value} lamports`)
// Estimated SOL transfer cost: 5000 lamports
// Send SOL
const transferTransaction = new Transaction().add(
SystemProgram.transfer({
fromPubkey: fromKeypair.publicKey,
toPubkey: toKeypair.publicKey,
lamports: lamportsToSend,
}),
)
await sendAndConfirmTransaction(connection, transferTransaction, [fromKeypair])
// Send SPL Token
// Add token transfer instructions to transaction
const transaction = new web3.Transaction().add(
splToken.Token.createTransferInstruction(
splToken.TOKEN_PROGRAM_ID,
fromTokenAccount.address,
toTokenAccount.address,
fromWallet.publicKey,
[],
1,
),
)
// Sign transaction, broadcast, and confirm
await web3.sendAndConfirmTransaction(connection, transaction, [fromWallet])
I using rust, anchor to develop smart contract. All I have to do is initializing some struct, and add methods in main program module. Itβs quite easy to understand, and I think itβs simillar with creating and using gRPC.
Screenshot of Demo project (thx to https://buildspace.so)