-
Notifications
You must be signed in to change notification settings - Fork 758
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #400 from 0xBenaparte/feat/add-starknet-id-getters
feat: add starknet.id getters
- Loading branch information
Showing
9 changed files
with
98,248 additions
and
3 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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,38 @@ | ||
import { StarknetChainId } from '../../src/constants'; | ||
import { getStarknetIdContract, useDecoded, useEncoded } from '../../src/utils/starknetId'; | ||
|
||
const characters = 'abcdefghijklmnopqrstuvwxyz0123456789-这来'; | ||
|
||
function generateString(length: number): string { | ||
let result = ''; | ||
const charactersLength = characters.length; | ||
for (let i = 0; i < length; i += 1) { | ||
result += characters.charAt(Math.floor(Math.random() * charactersLength)); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
describe('Should tets StarknetId utils', () => { | ||
test('Should test useEncoded and useDecoded hook with 100 random strings', () => { | ||
for (let index = 0; index < 100; index += 1) { | ||
const randomString = generateString(10); | ||
|
||
expect(useDecoded([useEncoded(randomString)])).toBe(randomString.concat('.stark')); | ||
} | ||
}); | ||
|
||
test('Should test getStarknetIdContract', () => { | ||
expect(getStarknetIdContract(StarknetChainId.TESTNET)).toBe( | ||
'0x05cf267a0af6101667013fc6bd3f6c11116a14cda9b8c4b1198520d59f900b17' | ||
); | ||
|
||
expect(() => { | ||
getStarknetIdContract(StarknetChainId.TESTNET2); | ||
}).toThrow(); | ||
|
||
expect(getStarknetIdContract(StarknetChainId.MAINNET)).toBe( | ||
'0x6ac597f8116f886fa1c97a23fa4e08299975ecaf6b598873ca6792b9bbfb678' | ||
); | ||
}); | ||
}); |
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,89 @@ | ||
/* eslint-disable no-param-reassign */ | ||
import BN from 'bn.js'; | ||
|
||
import { StarknetChainId } from '../constants'; | ||
|
||
const basicAlphabet = 'abcdefghijklmnopqrstuvwxyz0123456789-'; | ||
const basicSizePlusOne = new BN(basicAlphabet.length + 1); | ||
const bigAlphabet = '这来'; | ||
const basicAlphabetSize = new BN(basicAlphabet.length); | ||
const bigAlphabetSize = new BN(bigAlphabet.length); | ||
const bigAlphabetSizePlusOne = new BN(bigAlphabet.length + 1); | ||
|
||
export function useDecoded(encoded: BN[]): string { | ||
let decoded = ''; | ||
encoded.forEach((subdomain) => { | ||
while (!subdomain.isZero()) { | ||
const code = subdomain.mod(basicSizePlusOne).toNumber(); | ||
subdomain = subdomain.div(basicSizePlusOne); | ||
if (code === basicAlphabet.length) { | ||
const nextSubdomain = subdomain.div(bigAlphabetSizePlusOne); | ||
if (nextSubdomain.isZero()) { | ||
const code2 = subdomain.mod(bigAlphabetSizePlusOne).toNumber(); | ||
subdomain = nextSubdomain; | ||
if (code2 === 0) decoded += basicAlphabet[0]; | ||
else decoded += bigAlphabet[code2 - 1]; | ||
} else { | ||
const code2 = subdomain.mod(bigAlphabetSize).toNumber(); | ||
decoded += bigAlphabet[code2]; | ||
subdomain = subdomain.div(bigAlphabetSize); | ||
} | ||
} else decoded += basicAlphabet[code]; | ||
} | ||
decoded += '.'; | ||
}); | ||
|
||
return decoded.concat('stark'); | ||
} | ||
|
||
export function useEncoded(decoded: string): BN { | ||
let encoded = new BN(0); | ||
let multiplier = new BN(1); | ||
|
||
for (let i = 0; i < decoded.length; i += 1) { | ||
const char = decoded[i]; | ||
const index = basicAlphabet.indexOf(char); | ||
const bnIndex = new BN(basicAlphabet.indexOf(char)); | ||
|
||
if (index !== -1) { | ||
// add encoded + multiplier * index | ||
if (i === decoded.length - 1 && decoded[i] === basicAlphabet[0]) { | ||
encoded = encoded.add(multiplier.mul(basicAlphabetSize)); | ||
multiplier = multiplier.mul(basicSizePlusOne); | ||
// add 0 | ||
multiplier = multiplier.mul(basicSizePlusOne); | ||
} else { | ||
encoded = encoded.add(multiplier.mul(bnIndex)); | ||
multiplier = multiplier.mul(basicSizePlusOne); | ||
} | ||
} else if (bigAlphabet.indexOf(char) !== -1) { | ||
// add encoded + multiplier * (basicAlphabetSize) | ||
encoded = encoded.add(multiplier.mul(basicAlphabetSize)); | ||
multiplier = multiplier.mul(basicSizePlusOne); | ||
// add encoded + multiplier * index | ||
const newid = (i === decoded.length - 1 ? 1 : 0) + bigAlphabet.indexOf(char); | ||
encoded = encoded.add(multiplier.mul(new BN(newid))); | ||
multiplier = multiplier.mul(bigAlphabetSize); | ||
} | ||
} | ||
|
||
return encoded; | ||
} | ||
|
||
export function getStarknetIdContract(chainId: StarknetChainId): string { | ||
const starknetIdMainnetContract = | ||
'0x6ac597f8116f886fa1c97a23fa4e08299975ecaf6b598873ca6792b9bbfb678'; | ||
const starknetIdTestnetContract = | ||
'0x05cf267a0af6101667013fc6bd3f6c11116a14cda9b8c4b1198520d59f900b17'; | ||
|
||
switch (chainId) { | ||
case StarknetChainId.MAINNET: | ||
return starknetIdMainnetContract; | ||
|
||
case StarknetChainId.TESTNET: | ||
return starknetIdTestnetContract; | ||
|
||
default: | ||
throw new Error('Starknet.id is not yet deployed on this network'); | ||
} | ||
} |
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