-
Notifications
You must be signed in to change notification settings - Fork 775
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
109 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters