Starknet.js

Starknet js

Starknet.js offers integrated support for Starknet.id, enabling you to easily retrieve stark names and addresses.

🔗 View the source code on GitHub (opens in a new tab)

Installation

You can install Starknet.js using npm. You have two options:

# use the main branch
 
npm install starknet
 
# to use latest features (merges in develop branch)
 
npm install starknet@next

Usage

You can access the Starknet.id functions directly via the Provider class.

Functions

getStarkName

This function retrieves the stark name corresponding to a given address.

getStarkName(address, StarknetIdContract?): Promise<string>

Parameters

NameTypeDescription
addressBigNumberish (opens in a new tab)The address to query
StarknetIdContract?stringOptional naming contract address

Returns

Promise<string>

Defined in

src/provider/default.ts:217 (opens in a new tab)

Example usage

import { Provider, constants } from "starknet";
 
const provider = new Provider({
  sequencer: { network: constants.NetworkName.SN_MAIN },
});
 
const address = await provider.getAddressFromStarkName(address ?? "");

getAddressFromStarkName

This function retrieves the address corresponding to a given stark name.

getAddressFromStarkName(name, StarknetIdContract?): Promise<string>

Parameters

NameTypeDescription
namestringThe starkname to query
StarknetIdContract?stringOptional naming contract address

Returns

Promise<string>

Defined in

src/provider/default.ts:221 (opens in a new tab)

Example usage

import { Provider, constants } from "starknet";
 
const provider = new Provider({
  sequencer: { network: constants.NetworkName.SN_MAIN },
});
 
const address = await provider.getAddressFromStarkName("fricoben.stark");

Utils

The starknetId namespace in starknet.js provides utility functions for encoding and decoding stark names and addresses.

useDecoded

This function decodes a stark name from a given BigInt array.

useDecoded(encoded): string

Parameters

NameTypeDescription
encodedbigint[]BigInt array representing the domain

Returns

string : The decoded stark name

Defined in

src/utils/starknetId.ts:20 (opens in a new tab)

Example usage

import { starknetId } from "starknet";
 
const decodedDomain = starknetId.useDecoded([BigInt(1499554868251)]);
console.log(decodedDomain); // Output: fricoben.stark

useEncoded

Encodes a domain name into a BigInt. Note: Exclude the .stark suffix when passing the domain name.

useEncoded(decoded): bigint

Parameters

NameTypeDescription
decodedstringThe domain name to be encoded

Returns

bigint : The encoded BigInt value

Defined in

src/utils/starknetId.ts:61 (opens in a new tab)

Example usage

import { starknetId } from "starknet";
 
const encoded = starknetId.useEncoded("fricoben);
console.log(encoded); // Output: 1499554868251n

getStarknetIdContract

Retrieves the address of the Starknet.id naming contract based on the chain ID.

getStarknetIdContract(chainId): string

Parameters

NameTypeDescription
chainIdStarknetChainIdThe chain ID to query against

Returns

string : The address of the naming contract

**Defined in

src/utils/starknetId.ts:108 (opens in a new tab)

Example usage

import { starknetId, constants } from "starknet";
 
const namingContract = starknetId.getStarknetIdContract(
  constants.StarknetChainId.SN_MAIN
);
console.log(namingContract); // Output: 0x6ac597f8116f886fa1c97a23fa4e08299975ecaf6b598873ca6792b9bbfb678