NEWTON
Asked
3 months ago
7
views
0
How do I create my own account abstraction using starknetjs?
Newton
asked
3 months ago
0
Accepted answer
You are not limited to OZ or AX contracts; you can create your own contract for your wallet. It's the concept of Account Abstraction. You can customize entirely the wallet; for example :
The only limitation is your imagination...Here is an example of a customized wallet, including super administrator management, on a local starknet-devnet (launch starknet-devnet --seed 0
before using this script) :
import { Account, ec, json, stark, Provider, hash } from "starknet"; import axios from "axios";
and:
// connect provider const provider = new Provider({ sequencer: { network: "http://127.0.0.1:5050" } }); // initialize existing predeployed account 0 of Devnet const privateKey0 = "0xe3e70682c2094cac629f6fbed82c07cd"; const starkKeyPair0 = ec.getKeyPair(privateKey0); const accountAddress0 = "0x7e00d496e324876bbc8531f2d9a82bf154d1a04a50218ee74cdd372f75a551a"; const account0 = new Account(provider, accountAddress0, starkKeyPair0); // new account abstraction : // Generate public and private key pair. const AAprivateKey = stark.randomAddress(); console.log('New account :\nprivateKey=', AAprivateKey); const AAstarkKeyPair = ec.getKeyPair(AAprivateKey); const AAstarkKeyPub = ec.getStarkKey(AAstarkKeyPair); console.log('publicKey=', AAstarkKeyPub); // declare the contract const compiledAAaccount = json.parse(fs.readFileSync("./compiled_contracts/myAccountAbstraction.json").toString("ascii") const AAaccountClashHass = "0x5139780c7ec8246e21a22e49f4fa0ce430237df4a4b241214a3a5a5c120120d"; const { transaction_hash: declTH, class_hash: decCH } = await account0.declare({ classHash: AAaccountClashHass, contract: compiledAAaccount }); console.log('Customized account class hash =', decCH); await provider.waitForTransaction(declTH); // Calculate future address of the account const AAaccountConstructorCallData = stark.compileCalldata({ super_admin_address: account0.address, publicKey: AAstarkKeyPub }); const AAcontractAddress = hash.calculateContractAddressFromHash( AAstarkKeyPub, AAaccountClashHass, AAaccountConstructorCallData, 0 ); console.log('Precalculated account address=', AAcontractAddress); // fund account address before account creation const { data: answer } = await axios.post('http://127.0.0.1:5050/mint', { "address": AAcontractAddress, "amount": 50_000_000_000_000_000_000, "lite": true }, { headers: { "Content-Type": "application/json" } }); console.log('Answer mint =', answer); // deploy account const AAaccount = new Account(provider, AAcontractAddress, AAstarkKeyPair); const { transaction_hash, contract_address } = await AAaccount.deployAccount({ classHash: AAaccountClashHass, constructorCalldata: AAaccountConstructorCallData, addressSalt: AAstarkKeyPub }); await provider.waitForTransaction(transaction_hash); console.log('✅ New customized account created.\n address =', contract_address);
Newton
answered
3 months ago
How do I create account using starknetjs?
How do I create AX (ArgentX) contract using starknetjs?
How do I create OZ (Open Zeppelin) contract using starknetjs?
What are steps to create a new contract using starknetjs?
How do I connect my DAPP to Starknet mainnet using starknetjs?
How do I connect my DAPP to Starknet testnet using starknetjs?
How do I connect my DAPP to a Starknet node using starknetjs?
How can I read contract memory with starknetjs?
when starknet token?
Implementing the connect wallet. Can you help about connectors?
What are multi-calls in Account Abstraction? What will it bring to the UX?
Is the Starknet Name Service working or not?
ApeWorX: How can I test on multi-chain with both Starknet and Ethereum?
What is SHARP in Cairo Language?