Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add getStarkNames #24

Merged
merged 2 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 0 additions & 24 deletions example/.gitignore

This file was deleted.

12 changes: 0 additions & 12 deletions example/index.html

This file was deleted.

1,145 changes: 0 additions & 1,145 deletions example/package-lock.json

This file was deleted.

24 changes: 0 additions & 24 deletions example/package.json

This file was deleted.

8 changes: 0 additions & 8 deletions example/src/App.tsx

This file was deleted.

9 changes: 0 additions & 9 deletions example/src/main.tsx

This file was deleted.

21 changes: 0 additions & 21 deletions example/tsconfig.json

This file was deleted.

9 changes: 0 additions & 9 deletions example/tsconfig.node.json

This file was deleted.

7 changes: 0 additions & 7 deletions example/vite.config.ts

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "starknetid.js",
"version": "3.0.1",
"version": "3.0.2",
"description": "JavaScript library for Starknet ID",
"private": false,
"license": "MIT",
Expand Down
31 changes: 31 additions & 0 deletions packages/core/__test__/default.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { StarknetIdNavigator } from "../src";
import {
compiledIdentitySierra,
compiledIdentitySierraCasm,
compiledMulticallSierra,
compiledMulticallSierraCasm,
compiledNamingSierra,
compiledNamingSierraCasm,
compiledPricingSierra,
Expand All @@ -20,6 +22,7 @@ describe("test starknetid.js sdk", () => {
"0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7";
let NamingContract: string;
let IdentityContract: string;
let MulticallContract: string;

beforeAll(async () => {
expect(account).toBeInstanceOf(Account);
Expand Down Expand Up @@ -62,6 +65,16 @@ describe("test starknetid.js sdk", () => {
);
NamingContract = namingResponse.deploy.contract_address;

// Deploy multicall contract
const multicallResponse = await account.declareAndDeploy(
{
contract: compiledMulticallSierra,
casm: compiledMulticallSierraCasm,
},
{ maxFee: 1e18 },
);
MulticallContract = multicallResponse.deploy.contract_address;

const { transaction_hash } = await account.execute(
[
{
Expand Down Expand Up @@ -129,6 +142,24 @@ describe("test starknetid.js sdk", () => {
expect(name).toBe("ben.stark");
});

test("getStarkNames should work", async () => {
const starknetIdNavigator = new StarknetIdNavigator(
provider,
constants.StarknetChainId.SN_GOERLI,
{
naming: NamingContract,
identity: IdentityContract,
},
);
expect(starknetIdNavigator).toBeInstanceOf(StarknetIdNavigator);
const addresses = [account.address, account.address, account.address];
const names = await starknetIdNavigator.getStarkNames(
addresses,
MulticallContract,
);
expect(names).toEqual(["ben.stark", "ben.stark", "ben.stark"]);
});

test("getStarknetId should return id 1 for ben.stark", async () => {
const starknetIdNavigator = new StarknetIdNavigator(
provider,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "starknetid.js",
"version": "3.0.1",
"version": "3.0.2",
"keywords": [
"starknet",
"starknetid",
Expand Down
54 changes: 54 additions & 0 deletions packages/core/src/starknetIdNavigator/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
cairo,
Contract,
RawArgs,
RawArgsArray,
} from "starknet";
import {
decodeDomain,
Expand Down Expand Up @@ -95,6 +96,59 @@ export class StarknetIdNavigator implements StarknetIdNavigatorInterface {
}
}

public async getStarkNames(
addresses: string[],
multicallContract?: string,
): Promise<string[]> {
const namingContract =
this.StarknetIdContract.naming ?? getNamingContract(this.chainId);
const multicallAddress =
multicallContract ?? getMulticallContract(this.chainId);

// We need our contract to know the abi,
// otherwise we have to hardcode all the values for each enums
const { abi: multicallAbi } = await this.provider.getClassAt(
multicallAddress,
);
const contract = new Contract(
multicallAbi,
multicallAddress,
this.provider,
);

try {
// build calldata for all addresses
let calldata: RawArgsArray = [];
addresses.forEach((address) => {
calldata.push({
execution: this.staticExecution(),
to: this.hardcoded(namingContract),
selector: this.hardcoded(
hash.getSelectorFromName("address_to_domain"),
),
calldata: [this.hardcoded(address)],
});
});

const data = await contract.call("aggregate", [calldata]);

let result: string[] = [];
if (Array.isArray(data)) {
data.forEach((hexDomain: any) => {
const decimalDomain = hexDomain
.map((element: bigint) => BigInt(element))
.slice(1);
const stringDomain = decodeDomain(decimalDomain);
result.push(stringDomain);
});
}

return result;
} catch (e) {
throw new Error("Could not get stark names");
}
}

public async getStarknetId(domain: string): Promise<string> {
const contract =
this.StarknetIdContract.naming ?? getNamingContract(this.chainId);
Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/starknetIdNavigator/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ export abstract class StarknetIdNavigatorInterface {
*/
public abstract getStarkName(address: string): Promise<string>;

/**
* Get Starknames from an array of address
*
* @param addresses
* @param multicallContract (optional, will use the default one if not provided)
* @returns starknames
*/
public abstract getStarkNames(
addresses: string[],
multicallContract?: string,
): Promise<string[]>;

/**
* Get Starknet id from domain
*
Expand Down
Loading