Skip to content
Docs

Platforms

Expo

Install, configure, and initialize @gray3/client and @gray3/keystore in an Expo app, step by step.

Requirements

Expo SDK 51 or later with Hermes (the default). Keys and the keystore live on the device; the app talks to your backend for balances and broadcasts.

1. Install

bash
npx expo install expo-sqlite react-native-get-random-values
npm install @gray3/client @gray3/keystore

@gray3/core is installed with them. Use npx expo install for the native modules so Expo picks versions that match your SDK.

2. Add the polyfills file

react-native-get-random-values must load before anything that imports @gray3/*.

js
// polyfills.js
import "react-native-get-random-values"; // crypto.getRandomValues — must be first
// Only if GrayClient.create reports that TextDecoder is missing:
// import "fast-text-encoding";

3. Load the polyfills from the entry file

With expo-router, create index.js and point main at it so the polyfills run before the router loads your screens.

js
// index.js
import "./polyfills";
import "expo-router/entry";
json
// package.json
{
  "main": "index.js"
}

Without expo-router, import ./polyfills on the first line of your existing entry file, before registerRootComponent.

4. Metro configuration (SDK 51–52 only)

Expo SDK 53 and later resolve package exports by default. On SDK 51 and 52 enable it, or Metro will not find @gray3/keystore/expo or the React Native build.

js
// metro.config.js
const { getDefaultConfig } = require("expo/metro-config");

const config = getDefaultConfig(__dirname);
config.resolver.unstable_enablePackageExports = true; // SDK 51–52 only
module.exports = config;

5. Open the keystore

Open the SQLite database with expo-sqlite and pass the handle to the keystore.

js
import * as SQLite from "expo-sqlite";
import { GrayKeystore } from "@gray3/keystore";

const storage = await GrayKeystore.create({
  sqlite: { expo: SQLite.openDatabaseSync("gray-wallets.db") },
  password: pin, // the user's PIN; also passed to wallet operations
});

Protecting the PIN

Password mode runs scrypt in JavaScript, which can take noticeable time on low-end phones. Measure unlock time on your slowest target device. As an alternative, use key mode: store a random 32-byte key in expo-secure-store (Keychain on iOS, Keystore on Android) and pass it as encryptionKey. Wallet operations then need no PIN, and you can gate access with biometrics.

js
// npx expo install expo-secure-store
import * as SecureStore from "expo-secure-store";
import * as SQLite from "expo-sqlite";
import { GrayKeystore, randomKey, toHex, fromHex } from "@gray3/keystore";

const KEY_NAME = "gray3-keystore-key";
let hex = await SecureStore.getItemAsync(KEY_NAME, { requireAuthentication: true });
if (!hex) {
  hex = toHex(randomKey()); // 32 random bytes
  await SecureStore.setItemAsync(KEY_NAME, hex, { requireAuthentication: true });
}

const storage = await GrayKeystore.create({
  sqlite: { expo: SQLite.openDatabaseSync("gray-wallets.db") },
  encryptionKey: fromHex(hex),
});
// After storage.lock(), reopen with: await storage.unlockWithKey(fromHex(hex));

password and encryptionKey are mutually exclusive. Choose one mode when the store is first created and keep it for the life of that database.

6. Create the client and a wallet

js
import { GrayClient } from "@gray3/client";

const client = await GrayClient.create({ storage });

// Create a new wallet (or import an existing phrase)
const wallet = await client.wallets.create({ label: "Main", password: pin });
// const wallet = await client.wallets.importMnemonic({ mnemonic, label: "Restored", password: pin });

// Addresses are derived on the device
const account = wallet.account(0);
const evmAddress = account.address("ethereum");
const solAddress = account.address("solana");

// Sign a message; the result names the signing standard
const { signature, address, standard } = await account.signMessage("Hello from Gray3", "ethereum");
// standard === "eip191"

By default the client enables every Gray3 mainnet. Pass chains: ["ethereum", "solana"] to GrayClient.create to limit the chains, which also speeds up the first start because Polkadot and Bittensor crypto is initialized only when those chains are enabled. Testnets must be listed explicitly.

7. Call your backend

js
// Your backend URL — it holds the secret key, the app never does
const API = "https://api.example.com";

const res = await fetch(`${API}/portfolio?chainId=ethereum&address=${evmAddress}`, {
  headers: { authorization: `Bearer ${userSessionToken}` }, // your own user auth
});
const body = await res.json();
if (body.error) throw new Error(`${body.error.code}: ${body.error.message}`);
const portfolio = body.data;

Gram (TON) signing

Gram addresses work out of the box. Signing is opt-in on React Native because @ton/core's React Native dependencies need native modules.

  1. Install the TON packages and a Buffer polyfill:
    bash
    npm install @ton/core @ton/ton @ton/crypto buffer
  2. Add a global Buffer to polyfills.js, after react-native-get-random-values:
    js
    // polyfills.js
    import "react-native-get-random-values";
    import { Buffer } from "buffer";
    globalThis.Buffer ??= Buffer; // must run before @ton/core is imported
  3. Point Metro at the browser build of @ton/crypto-primitives so it does not need extra native modules (alternatively, install expo-crypto and react-native-fast-pbkdf2):
    js
    // metro.config.js
    const path = require("path");
    const { getDefaultConfig } = require("expo/metro-config");
    
    const config = getDefaultConfig(__dirname);
    config.resolver.unstable_enablePackageExports = true; // SDK 51–52 only
    const tonPrimitives = require.resolve("@ton/crypto-primitives/dist/browser.js", {
      paths: [path.dirname(require.resolve("@ton/crypto/package.json"))],
    });
    config.resolver.resolveRequest = (context, moduleName, platform) =>
      moduleName === "@ton/crypto-primitives"
        ? { type: "sourceFile", filePath: tonPrimitives }
        : context.resolveRequest(context, moduleName, platform);
    module.exports = config;
  4. Register the modules once at app start:
    js
    import * as core from "@ton/core";
    import * as ton from "@ton/ton";
    import { configureTon } from "@gray3/client";
    
    configureTon({ core, ton });

Limits on React Native

  • Monero: keys and addresses work. Wallet sync and transfers are not available, because they need WebAssembly and Web Workers, which Hermes does not provide.
  • Kaspa: addresses work; signing is available on Node.js only.
  • Polkadot and Bittensor: use an asm.js build, so the first start is slower. If you enable them after start with client.chains.add, call await ensurePolkadotCrypto() (from @gray3/client) before deriving.