-
Notifications
You must be signed in to change notification settings - Fork 28
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
11 changed files
with
275 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { toJson } from './utils/serializer'; | ||
import { getAddrFromStarkNameUtil } from '../src/utils/starknetUtils'; | ||
import { ApiParams, GetAddrFromStarkNameRequestParam } from './types/snapApi'; | ||
import { getNetworkFromChainId } from './utils/snapUtils'; | ||
import { logger } from './utils/logger'; | ||
|
||
export async function getAddrFromStarkName(params: ApiParams) { | ||
try { | ||
const { state, requestParams } = params; | ||
const requestParamsObj = requestParams as GetAddrFromStarkNameRequestParam; | ||
|
||
if (!requestParamsObj.starkName) { | ||
throw new Error( | ||
`The given stark name need to be non-empty string, got: ${toJson( | ||
requestParamsObj, | ||
)}`, | ||
); | ||
} | ||
|
||
const starkName = requestParamsObj.starkName; | ||
|
||
const network = getNetworkFromChainId(state, requestParamsObj.chainId); | ||
|
||
const resp = await getAddrFromStarkNameUtil(network, starkName); | ||
logger.log(`getAddrFromStarkName: addr:\n${toJson(resp)}`); | ||
|
||
return resp; | ||
} catch (err) { | ||
logger.error(`Problem found: ${err}`); | ||
throw err; | ||
} | ||
} |
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
100 changes: 100 additions & 0 deletions
100
packages/starknet-snap/test/src/getAddrFromStarkName.test.ts
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,100 @@ | ||
import chai, { expect } from 'chai'; | ||
import sinon from 'sinon'; | ||
import sinonChai from 'sinon-chai'; | ||
import { WalletMock } from '../wallet.mock.test'; | ||
import * as utils from '../../src/utils/starknetUtils'; | ||
import { SnapState } from '../../src/types/snapState'; | ||
import { STARKNET_SEPOLIA_TESTNET_NETWORK } from '../../src/utils/constants'; | ||
import { Mutex } from 'async-mutex'; | ||
import { | ||
ApiParams, | ||
GetAddrFromStarkNameRequestParam, | ||
} from '../../src/types/snapApi'; | ||
import { getAddrFromStarkName } from '../../src/getAddrFromStarkName'; | ||
|
||
chai.use(sinonChai); | ||
const sandbox = sinon.createSandbox(); | ||
|
||
describe('Test function: getAddrFromStarkName', function () { | ||
const walletStub = new WalletMock(); | ||
const state: SnapState = { | ||
accContracts: [], | ||
erc20Tokens: [], | ||
networks: [STARKNET_SEPOLIA_TESTNET_NETWORK], | ||
transactions: [], | ||
}; | ||
const apiParams: ApiParams = { | ||
state, | ||
requestParams: {}, | ||
wallet: walletStub, | ||
saveMutex: new Mutex(), | ||
}; | ||
|
||
afterEach(function () { | ||
walletStub.reset(); | ||
sandbox.restore(); | ||
}); | ||
|
||
it('should retrieve the address from stark name successfully', async function () { | ||
sandbox.stub(utils, 'getAddrFromStarkNameUtil').callsFake(async () => { | ||
return '0x01c744953f1d671673f46a9179a58a7e58d9299499b1e076cdb908e7abffe69f'; | ||
}); | ||
const requestObject: GetAddrFromStarkNameRequestParam = { | ||
starkName: 'testName.stark', | ||
}; | ||
apiParams.requestParams = requestObject; | ||
const result = await getAddrFromStarkName(apiParams); | ||
expect(result).to.be.eq( | ||
'0x01c744953f1d671673f46a9179a58a7e58d9299499b1e076cdb908e7abffe69f', | ||
); | ||
}); | ||
|
||
it('should throw error if getAddrFromStarkNameUtil failed', async function () { | ||
sandbox.stub(utils, 'getAddrFromStarkNameUtil').throws(new Error()); | ||
const requestObject: GetAddrFromStarkNameRequestParam = { | ||
starkName: 'testName.stark', | ||
}; | ||
apiParams.requestParams = requestObject; | ||
|
||
let result; | ||
try { | ||
await getAddrFromStarkName(apiParams); | ||
} catch (err) { | ||
result = err; | ||
} finally { | ||
expect(result).to.be.an('Error'); | ||
} | ||
}); | ||
|
||
it('should throw error if the stark name is empty', async function () { | ||
const requestObject: GetAddrFromStarkNameRequestParam = { | ||
starkName: '', | ||
}; | ||
apiParams.requestParams = requestObject; | ||
|
||
let result; | ||
try { | ||
await getAddrFromStarkName(apiParams); | ||
} catch (err) { | ||
result = err; | ||
} finally { | ||
expect(result).to.be.an('Error'); | ||
} | ||
}); | ||
|
||
it('should throw error if the user address is invalid', async function () { | ||
const requestObject: GetAddrFromStarkNameRequestParam = { | ||
starkName: 'invalidName', | ||
}; | ||
apiParams.requestParams = requestObject; | ||
|
||
let result; | ||
try { | ||
await getAddrFromStarkName(apiParams); | ||
} catch (err) { | ||
result = err; | ||
} finally { | ||
expect(result).to.be.an('Error'); | ||
} | ||
}); | ||
}); |
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
Oops, something went wrong.