Working with cells
Cells are the basic storage elements in the blockchain, used to represent and manipulate data in contracts. In this guide, we will explore the basic methods for working with cells provided by everscale-inpage-provider
.
INFO
Note that it is also possible to work with accounts, but we will cover that in the next section.
Here is the ABI definition we'll use in our examples:
const ABI = [
{ name: 'name', type: 'string' },
{ name: 'age', type: 'uint32' },
{ name: 'city', type: 'string' },
] as const;
Cell Data Packing
To pack data into a TVM cell, use the packIntoCell
method. It creates a base64
encoded BOC
(Bag of Cells) from the provided data
, structure
, and ABI version.
import { ProviderRpcClient } from 'everscale-inpage-provider';
const provider = new ProviderRpcClient();
// Make sure the provider is initialized.
await provider.ensureInitialized();
// Request permissions from the user to execute API
// methods using the provider.
await provider.requestPermissions({ permissions: ['basic'] });
// Define the data to pack.
const dataToPack = {
name: 'Alice',
age: 30,
city: 'New York',
};
// Pack the data into BOC.
const { boc, hash } = await provider.packIntoCell({
structure: ABI,
data: dataToPack,
});
console.log('Packed data:', boc);
console.log('Packed data hash:', hash);
Cell Data Unpacking
To unpack data from a TVM cell, use the unpackFromCell
method. It decodes the base64-encoded
string into an object according to the provided ABI.
// Define the data to unpack.
const bocToUnpack =
'te6ccgEBAwEAGQACCAAAAB4CAQAQTmV3IFlvcmsACkFsaWNl';
// Unpack the data from BOC.
const unpacked = await provider.unpackFromCell({
structure: ABI,
boc: bocToUnpack,
allowPartial: true,
});
console.log('Unpacked data:', unpacked.data);
BOC Hash Computation
Before diving into the specifics of how to compute the hash of a base64
encoded BOC (Bag of Cells) using the getBocHash
method provided by the everscale-inpage-provider
.
At the most fundamental level, everything in Blockchain is a cell. Each cell consists of up to 1023 data bits and up to 4 references to other cells. Cells are organized in a tree-like structure, with each cell potentially referencing up to four other cells. The hash of a cell is a kind of complex hash derived from the cell's representation in a specific format.
It's crucial to note that this is not simply a SHA-256 hash of the byte contents of the base64 string. Instead, it involves the structured data within the cell, its references, and specific aspects of its representation.
Get BOC Hash
The getBocHash
method computes the hash of a base64
encoded BOC and returns the hash as a hexadecimal string. This method requires basic permissions.
Here's an example of how to use the getBocHash
method:
// Define the base64 encoded BOC.
const boc = 'te6ccgEBAwEAGQACCAAAAB4CAQAQTmV3IFlvcmsACkFsaWNl';
// Compute the hash of the base64 encoded BOC.
const hash = await provider.getBocHash(boc);
console.log('BOC hash:', hash);
WARNING
In this case, getBocHash
takes the base64
representation of the cell (or BOC) and computes the hash using the specific hashing method suitable for this data structure.
Public Key Extraction
The extractPublicKey
method is useful when you need to obtain the public key associated with a contract's raw account state. This can be helpful, for example, when verifying the integrity of a transaction or checking the owner of a contract. Keep in mind that this method can only be used on contracts that have been deployed and have a pubkey
header.
exampleBOC
const exampleBOC = `te6ccgECJAEABVQAAq+ABZ3CHw48OB0mTpE8eqZinXiv5scdevb2gcI/pRjrelBFjS9+hkTHXAAAAGmBdY7yEh23xBWmo7FjLZOuSyzxWmQsy2Iuh1QTrvQw03v4IzLNJK91loBAAwEBgfvlEQAVS8EtkmmQXS+aqup1WIq0Oaj5Ef1aKt620rWnAAABh8qr9wqAABHVgAAAAAAAAAAAAAAAAAAB9AAAAABAAgAIdGVzdAQkiu1TIOMDIMD/4wIgwP7jAvILIQUEIwLc7UTQ10nDAfhmjQhgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAE+Gkh2zzTAAGOFIMI1xgg+CjIzs7J+QBY+EL5EPKo3tM/AfhDIbnytCD4I4ED6KiCCBt3QKC58rT4Y9MfAds88jwLBgN67UTQ10nDAfhmItDTA/pAMPhpqTgA+ER/b3GCCJiWgG9ybW9zcG90+GTcIccA4wIh1w0f8rwh4wMB2zzyPCAgBgIoIIIQQT7ay7vjAiCCEHEJa0q74wIOBwM8IIIQZwdgDrrjAiCCEG6/VtK64wIgghBxCWtKuuMCDAoIA3Iw+Eby4Ez4Qm7jANHbPCGOISPQ0wH6QDAxyM+HIM6CEPEJa0rPC4EBbyICyx/MyXD7AJEw4uMA8gAfCR0ABPhMAkgw+EJu4wD4RvJz03/U0fgAIfhrAYED6KkItR8BbwL4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j+GIACvhG8uBMAhD0pCD0vfLATiMiABRzb2wgMC42Ni4wAAA=`;
// Extract public key from boc
const publicKey = await provider.extractPublicKey(exampleBOC);
console.log('Public key:', publicKey);