-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* feat: transfer to a stark name * fix: lint errors * test: update getAddrFromStarkName network * fix: update to new RPC structure * chore: lint * chore: lint * fix: racing condition in SendModal * fix: move validate logic out of addressInput & update requests made to snap * fix: simplify name check in frontend * fix: resolved addr is validated by superstruct * chore: add snap permission boundary and update UI text * chore: update RpcController import location --------- Co-authored-by: Iris <[email protected]>
- Loading branch information
1 parent
1e80070
commit c9e2c64
Showing
15 changed files
with
344 additions
and
11 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
58 changes: 58 additions & 0 deletions
58
packages/starknet-snap/src/rpcs/get-addr-from-starkname.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,58 @@ | ||
import { constants } from 'starknet'; | ||
|
||
import { InvalidRequestParamsError } from '../utils/exceptions'; | ||
import * as starknetUtils from '../utils/starknetUtils'; | ||
import { | ||
getAddrFromStarkName, | ||
type GetAddrFromStarkNameParams, | ||
} from './get-addr-from-starkname'; | ||
|
||
jest.mock('../utils/snap'); | ||
jest.mock('../utils/logger'); | ||
|
||
const prepareMockGetAddrFromStarkName = ({ | ||
chainId, | ||
starkName, | ||
}: { | ||
chainId: constants.StarknetChainId; | ||
starkName: string; | ||
}) => { | ||
const request = { | ||
chainId, | ||
starkName, | ||
} as unknown as GetAddrFromStarkNameParams; | ||
|
||
const getAddrFromStarkNameSpy = jest.spyOn( | ||
starknetUtils, | ||
'getAddrFromStarkNameUtil', | ||
); | ||
getAddrFromStarkNameSpy.mockResolvedValue( | ||
'0x01c744953f1d671673f46a9179a58a7e58d9299499b1e076cdb908e7abffe69f', | ||
); | ||
|
||
return { | ||
request, | ||
}; | ||
}; | ||
|
||
describe('getAddrFromStarkName', () => { | ||
it('get address from stark name correctly', async () => { | ||
const chainId = constants.StarknetChainId.SN_SEPOLIA; | ||
const { request } = prepareMockGetAddrFromStarkName({ | ||
chainId, | ||
starkName: 'testname.stark', | ||
}); | ||
|
||
const result = await getAddrFromStarkName.execute(request); | ||
|
||
expect(result).toBe( | ||
'0x01c744953f1d671673f46a9179a58a7e58d9299499b1e076cdb908e7abffe69f', | ||
); | ||
}); | ||
|
||
it('throws `InvalidRequestParamsError` when request parameter is not correct', async () => { | ||
await expect( | ||
getAddrFromStarkName.execute({} as unknown as GetAddrFromStarkNameParams), | ||
).rejects.toThrow(InvalidRequestParamsError); | ||
}); | ||
}); |
87 changes: 87 additions & 0 deletions
87
packages/starknet-snap/src/rpcs/get-addr-from-starkname.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,87 @@ | ||
import type { Infer } from 'superstruct'; | ||
import { assign, object } from 'superstruct'; | ||
|
||
import { NetworkStateManager } from '../state/network-state-manager'; | ||
import type { Network } from '../types/snapState'; | ||
import { AddressStruct, BaseRequestStruct, StarkNameStruct } from '../utils'; | ||
import { InvalidNetworkError } from '../utils/exceptions'; | ||
import { getAddrFromStarkNameUtil } from '../utils/starknetUtils'; | ||
import { RpcController } from './abstract/base-rpc-controller'; | ||
|
||
export const GetAddrFromStarkNameRequestStruct = assign( | ||
object({ | ||
starkName: StarkNameStruct, | ||
}), | ||
BaseRequestStruct, | ||
); | ||
|
||
export const GetAddrFromStarkNameResponseStruct = AddressStruct; | ||
|
||
export type GetAddrFromStarkNameParams = Infer< | ||
typeof GetAddrFromStarkNameRequestStruct | ||
>; | ||
|
||
export type GetAddrFromStarkNameResponse = Infer< | ||
typeof GetAddrFromStarkNameResponseStruct | ||
>; | ||
|
||
/** | ||
* The RPC handler to get a StarkName by a Starknet address. | ||
*/ | ||
export class GetAddrFromStarkNameRpc extends RpcController< | ||
GetAddrFromStarkNameParams, | ||
GetAddrFromStarkNameResponse | ||
> { | ||
protected requestStruct = GetAddrFromStarkNameRequestStruct; | ||
|
||
protected responseStruct = GetAddrFromStarkNameResponseStruct; | ||
|
||
protected readonly networkStateMgr: NetworkStateManager; | ||
|
||
constructor() { | ||
super(); | ||
this.networkStateMgr = new NetworkStateManager(); | ||
} | ||
|
||
/** | ||
* Execute the get address from stark name request handler. | ||
* | ||
* @param params - The parameters of the request. | ||
* @param params.starkName - The stark name of the user. | ||
* @param params.chainId - The chain id of the network. | ||
* @returns A promise that resolves to an address. | ||
* @throws {Error} If the network with the chain id is not supported. | ||
*/ | ||
async execute( | ||
params: GetAddrFromStarkNameParams, | ||
): Promise<GetAddrFromStarkNameResponse> { | ||
return super.execute(params); | ||
} | ||
|
||
protected async getNetworkFromChainId(chainId: string): Promise<Network> { | ||
const network = await this.networkStateMgr.getNetwork({ | ||
chainId, | ||
}); | ||
|
||
// It should be never happen, as the chainId should be validated by the superstruct | ||
if (!network) { | ||
throw new InvalidNetworkError() as unknown as Error; | ||
} | ||
|
||
return network; | ||
} | ||
|
||
protected async handleRequest( | ||
params: GetAddrFromStarkNameParams, | ||
): Promise<GetAddrFromStarkNameResponse> { | ||
const { chainId, starkName } = params; | ||
|
||
const network = await this.getNetworkFromChainId(chainId); | ||
|
||
const address = await getAddrFromStarkNameUtil(network, starkName); | ||
|
||
return address; | ||
} | ||
} | ||
|
||
export const getAddrFromStarkName = new GetAddrFromStarkNameRpc(); |
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
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.