Skip to main content
IntermediateEst8 minsdk-core15.4.1Reference

Save key-value data to an account

Saving a key-value pair to an account using the controller

You can find more information here regarding the account storage.

{
// create the entrypoint and the account controller
const entrypoint = new DevnetEntrypoint();
const controller = entrypoint.createAccountController();

const filePath = path.join("../src", "testdata", "testwallets", "alice.pem");
const alice = await Account.newFromPem(filePath);

// creating the key-value pairs we want to save
const keyValuePairs = new Map([[Buffer.from("key0"), Buffer.from("value0")]]);

// fetch the nonce of the network
alice.nonce = await entrypoint.recallAccountNonce(alice.address);

const transaction = await controller.createTransactionForSavingKeyValue(alice, alice.getNonceThenIncrement(), {
keyValuePairs: keyValuePairs,
});

// sending the transaction
const txHash = await entrypoint.sendTransaction(transaction);
}

Saving a key-value pair to an account using the factory

{
// create the entrypoint and the account factory
const entrypoint = new DevnetEntrypoint();
const factory = entrypoint.createAccountTransactionsFactory();

// create the account to guard
const filePath = path.join("../src", "testdata", "testwallets", "alice.pem");
const alice = await Account.newFromPem(filePath);

// creating the key-value pairs we want to save
const keyValuePairs = new Map([[Buffer.from("key0"), Buffer.from("value0")]]);

const transaction = await factory.createTransactionForSavingKeyValue(alice.address, {
keyValuePairs: keyValuePairs,
});

// fetch the nonce of the network
alice.nonce = await entrypoint.recallAccountNonce(alice.address);

// set the nonce
transaction.nonce = alice.getNonceThenIncrement();

// sign the transaction
transaction.signature = await alice.signTransaction(transaction);

// sending the transaction
const txHash = await entrypoint.sendTransaction(transaction);
}