Skip to content

Commit

Permalink
Merge pull request #24 from starknet-id/feat/getstarkname_multicall
Browse files Browse the repository at this point in the history
feat: add getStarkNames
  • Loading branch information
Th0rgal authored Feb 27, 2024
2 parents 017a8a1 + d03f734 commit 75e434b
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 1,261 deletions.
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

0 comments on commit 75e434b

Please sign in to comment.