silencebringer69 --> Hi! I am trying to create a subaccount and than deploy contract on it. I am facing issues while deploying contract. Kindly review the code and error shared as images. Kindly guide what am I doing wrong? Any help will be appreciated.
Benji | NEAR --> Hey so you should do something like this:
const keyStore = new keyStores.InMemoryKeyStore();
const ACCOUNT_ID = "benjiman.testnet"; // NEAR account tied to the keyPair
const NETWORK_ID = "testnet";
keyStore.setKey(NETWORK_ID, ACCOUNT_ID, KeyPair.fromString("YOUR_PRIVATE_KEY"));
const config = {
networkId: "testnet",
keyStore, // optional if not signing transactions
nodeUrl: "https://rpc.testnet.near.org",
walletUrl: "https://wallet.testnet.near.org",
helperUrl: "https://helper.testnet.near.org",
explorerUrl: "https://explorer.testnet.near.org",
};
const near = await connect(config);
let keyPair = await KeyPair.fromRandom('ed25519');
let pubKey = keyPair.getPublicKey();
console.log('pubKey: ', pubKey);
const account = await near.account("benjiman.testnet");
await account.createAccount(
"in-memory-testing.benjiman.testnet", // new account name
pubKey, // public key for new account
parseNearAmount("1") // initial balance for new account in yoctoNEAR
);
Benji | NEAR --> Basically what’s happening is you’re not properly creating the account and then you try to deploy the contract to the account that wasn’t actually created and it’s yelling at you saying you don’t have the keys to that account.
Benji | NEAR --> In my code snippet, I’m first logging into my account “benjiman.testnet” by setting the private key in the keystore. I then connect to near using the config and keystore and then generate a new keypair for my new account that I’m gonna create. I then go ahead and create the subaccount using my original account benjiman.testnet
Benji | NEAR --> here is the transaction: https://explorer.testnet.near.org/transactions/9tzNYeZ4gAynrQRsronFvpUPDsQm4SHGDX3XUmQoxXd5
silencebringer69 --> Thanks for the answer. From where can I get the private key?
Benji | NEAR --> You can read it in from a file or get it from browser local storage (which is not recommended as you need a FAK to create an account)
Benji | NEAR --> your keys will be stored in the ~/.near-credentials
folder usually. If you have your account in the wallet but not locally, use the NEAR-CLI command near login
to populate a json file in your near credentials folder with your keys.
silencebringer69 --> Do we have to repeat the same exercise while deploying for the client? Meaning the client will have to share his private key after logging in using NEAR cli.
Benji | NEAR --> from the client, you can login using the wallet object
Benji | NEAR --> https://docs.near.org/docs/api/naj-quick-reference#wallet
Benji | NEAR --> instantiate a wallet connection and then login with a button of some sorts. This will create a function call access key and then you can try and create a subaccount. Since this requires a full access key to do, you will be redirected to the NEAR wallet to authorize the transaction.
Benji | NEAR --> I’m not sure how you’ll handle the deployment of the contract since you’ll have a redirect, however.