Skip to content

Commit

Permalink
feat: move starkname to provider
Browse files Browse the repository at this point in the history
  • Loading branch information
janek26 committed Feb 15, 2023
1 parent a12edc8 commit 8e88ed0
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 51 deletions.
2 changes: 1 addition & 1 deletion __tests__/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ describe('deploy and test Wallet', () => {
const address = await account.getAddressFromStarkName('ben.stark', namingAddress);
expect(hexToDecimalString(address as string)).toEqual(hexToDecimalString(account.address));

const name = await account.getStarkName(namingAddress);
const name = await account.getStarkName(undefined, namingAddress);
expect(name).toEqual('ben.stark');
});
});
Expand Down
4 changes: 4 additions & 0 deletions __tests__/utils/starknetId.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ describe('Should tets StarknetId utils', () => {
}
});

test('Should test useEncoded and useDecoded hook with an empty string', () => {
expect(useDecoded([])).toBe('');
});

test('Should test useDecoded and useEncoded hook with an encoded number', () => {
for (let index = 0; index < 2500; index += 1) {
const decoded = useDecoded([new BN(index)]);
Expand Down
58 changes: 8 additions & 50 deletions src/account/default.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { BN } from 'bn.js';

import { UDC, ZERO } from '../constants';
import { ProviderInterface, ProviderOptions } from '../provider';
import { Provider } from '../provider/default';
Expand Down Expand Up @@ -39,10 +37,9 @@ import {
pedersen,
transactionVersion,
} from '../utils/hash';
import { BigNumberish, hexToDecimalString, toBN, toCairoBool } from '../utils/number';
import { BigNumberish, toBN, toCairoBool } from '../utils/number';
import { parseContract } from '../utils/provider';
import { compileCalldata, estimatedFeeToMaxFee, randomAddress } from '../utils/stark';
import { getStarknetIdContract, useDecoded, useEncoded } from '../utils/starknetId';
import { fromCallsToExecuteCalldata } from '../utils/transaction';
import { TypedData, getMessageHash } from '../utils/typedData';
import { AccountInterface } from './interface';
Expand All @@ -67,52 +64,6 @@ export class Account extends Provider implements AccountInterface {
return super.getNonceForAddress(this.address, blockIdentifier);
}

public async getStarkName(StarknetIdContract?: string): Promise<string | Error> {
const chainId = await this.getChainId();
const contract = StarknetIdContract ?? getStarknetIdContract(chainId);

try {
const hexDomain = await this.callContract({
contractAddress: contract,
entrypoint: 'address_to_domain',
calldata: compileCalldata({
address: this.address,
}),
});
const decimalDomain = hexDomain.result
.map((element) => new BN(hexToDecimalString(element)))
.slice(1);

const stringDomain = useDecoded(decimalDomain);

return stringDomain;
} catch {
return Error('Could not get stark name');
}
}

public async getAddressFromStarkName(
name: string,
StarknetIdContract?: string
): Promise<string | Error> {
const chainId = await this.getChainId();
const contract = StarknetIdContract ?? getStarknetIdContract(chainId);

try {
const addressData = await this.callContract({
contractAddress: contract,
entrypoint: 'domain_to_address',
calldata: compileCalldata({
domain: [useEncoded(name.replace('.stark', '')).toString(10)],
}),
});

return addressData.result[0];
} catch {
return Error('Could not get address from stark name');
}
}

public async estimateFee(
calls: AllowArray<Call>,
estimateFeeDetails?: EstimateFeeDetails | undefined
Expand Down Expand Up @@ -672,4 +623,11 @@ export class Account extends Provider implements AccountInterface {
},
};
}

public override async getStarkName(
address: BigNumberish = this.address, // default to the wallet address
StarknetIdContract?: string
): Promise<string> {
return super.getStarkName(address, StarknetIdContract);
}
}
9 changes: 9 additions & 0 deletions src/provider/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { BigNumberish } from '../utils/number';
import { ProviderInterface } from './interface';
import { RpcProvider, RpcProviderOptions } from './rpc';
import { SequencerProvider, SequencerProviderOptions } from './sequencer';
import { getAddressFromStarkName, getStarkName } from './starknetId';
import { BlockIdentifier } from './utils';

export interface ProviderOptions {
Expand Down Expand Up @@ -207,4 +208,12 @@ export class Provider implements ProviderInterface {
public async getStateUpdate(blockIdentifier?: BlockIdentifier): Promise<StateUpdateResponse> {
return this.provider.getStateUpdate(blockIdentifier);
}

public async getStarkName(address: BigNumberish, StarknetIdContract?: string): Promise<string> {
return getStarkName(this, address, StarknetIdContract);
}

public async getAddressFromStarkName(name: string, StarknetIdContract?: string): Promise<string> {
return getAddressFromStarkName(this, name, StarknetIdContract);
}
}
9 changes: 9 additions & 0 deletions src/provider/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { parseCalldata, wait } from '../utils/provider';
import { RPCResponseParser } from '../utils/responseParser/rpc';
import { LibraryError } from './errors';
import { ProviderInterface } from './interface';
import { getAddressFromStarkName, getStarkName } from './starknetId';
import { Block, BlockIdentifier } from './utils';

export type RpcProviderOptions = {
Expand Down Expand Up @@ -500,4 +501,12 @@ export class RpcProvider implements ProviderInterface {
): Promise<TransactionSimulationResponse> {
throw new Error('RPC does not implement simulateTransaction function');
}

public async getStarkName(address: BigNumberish, StarknetIdContract?: string): Promise<string> {
return getStarkName(this, address, StarknetIdContract);
}

public async getAddressFromStarkName(name: string, StarknetIdContract?: string): Promise<string> {
return getAddressFromStarkName(this, name, StarknetIdContract);
}
}
9 changes: 9 additions & 0 deletions src/provider/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import { randomAddress } from '../utils/stark';
import { buildUrl } from '../utils/url';
import { GatewayError, HttpError, LibraryError } from './errors';
import { ProviderInterface } from './interface';
import { getAddressFromStarkName, getStarkName } from './starknetId';
import { Block, BlockIdentifier } from './utils';

type NetworkName = 'mainnet-alpha' | 'goerli-alpha' | 'goerli-alpha-2';
Expand Down Expand Up @@ -605,4 +606,12 @@ export class SequencerProvider implements ProviderInterface {
const args = new Block(blockIdentifier).sequencerIdentifier;
return this.fetchEndpoint('get_block_traces', { ...args });
}

public async getStarkName(address: BigNumberish, StarknetIdContract?: string): Promise<string> {
return getStarkName(this, address, StarknetIdContract);
}

public async getAddressFromStarkName(name: string, StarknetIdContract?: string): Promise<string> {
return getAddressFromStarkName(this, name, StarknetIdContract);
}
}
64 changes: 64 additions & 0 deletions src/provider/starknetId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { BN } from 'bn.js';

import { BigNumberish, hexToDecimalString, toBN, toHex } from '../utils/number';
import { compileCalldata } from '../utils/stark';
import { getStarknetIdContract, useDecoded, useEncoded } from '../utils/starknetId';
import { ProviderInterface } from './interface';

export async function getStarkName(
provider: ProviderInterface,
address: BigNumberish,
StarknetIdContract?: string
): Promise<string> {
const chainId = await provider.getChainId();
const contract = StarknetIdContract ?? getStarknetIdContract(chainId);

try {
const hexDomain = await provider.callContract({
contractAddress: contract,
entrypoint: 'address_to_domain',
calldata: compileCalldata({
address: toHex(toBN(address)),
}),
});
const decimalDomain = hexDomain.result
.map((element) => new BN(hexToDecimalString(element)))
.slice(1);

const stringDomain = useDecoded(decimalDomain);

if (!stringDomain) {
throw Error('Starkname not found');
}

return stringDomain;
} catch (e) {
if (e instanceof Error && e.message === 'Starkname not found') {
throw e;
}
throw Error('Could not get stark name');
}
}

export async function getAddressFromStarkName(
provider: ProviderInterface,
name: string,
StarknetIdContract?: string
): Promise<string> {
const chainId = await provider.getChainId();
const contract = StarknetIdContract ?? getStarknetIdContract(chainId);

try {
const addressData = await provider.callContract({
contractAddress: contract,
entrypoint: 'domain_to_address',
calldata: compileCalldata({
domain: [useEncoded(name.replace('.stark', '')).toString(10)],
}),
});

return addressData.result[0];
} catch {
throw Error('Could not get address from stark name');
}
}
5 changes: 5 additions & 0 deletions src/utils/starknetId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ export function useDecoded(encoded: BN[]): string {
: bigAlphabet[bigAlphabet.length - 1].repeat((k - 1) / 2 + 1));
decoded += '.';
});

if (!decoded) {
return decoded;
}

return decoded.concat('stark');
}

Expand Down

0 comments on commit 8e88ed0

Please sign in to comment.