Skip to content

Commit

Permalink
✨ feat: add sdk rpc methods (#265)
Browse files Browse the repository at this point in the history
  • Loading branch information
rustin01 authored Dec 22, 2024
1 parent 9215d0d commit 6452694
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 32 deletions.
2 changes: 1 addition & 1 deletion packages/coin-dfinity/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default defineConfig({
},
commonjsOptions: {
transformMixedEsModules: true,
include: [/node_modules/]
include: [/node_modules/, /crypto-lib/]
},
rollupOptions: {
plugins: [
Expand Down
2 changes: 1 addition & 1 deletion packages/coin-ethereum/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default defineConfig({
},
commonjsOptions: {
transformMixedEsModules: true,
include: [/node_modules/]
include: [/node_modules/, /crypto-lib/]
},
rollupOptions: {
plugins: [
Expand Down
2 changes: 1 addition & 1 deletion packages/coin-solana/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default defineConfig({
},
commonjsOptions: {
transformMixedEsModules: true,
include: [/node_modules/]
include: [/node_modules/, /crypto-lib/]
},
rollupOptions: {
plugins: [
Expand Down
2 changes: 1 addition & 1 deletion packages/coin-ton/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default defineConfig({
},
commonjsOptions: {
transformMixedEsModules: true,
include: [/node_modules/]
include: [/node_modules/, /crypto-lib/]
},
rollupOptions: {
plugins: [
Expand Down
2 changes: 1 addition & 1 deletion packages/coin-tron/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default defineConfig({
},
commonjsOptions: {
transformMixedEsModules: true,
include: [/node_modules/]
include: [/node_modules/, /crypto-lib/]
},
rollupOptions: {
plugins: [
Expand Down
2 changes: 2 additions & 0 deletions packages/sdk/src/lib/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export enum WalletExposeRPCMethod {
CONNECT = 'connect',
GET_BALANCE = 'getBalance',
TRANSFER = 'transfer',
GET_ESTIMATED_FEE = 'getEstimatedFee',
TONCONNECT_GET_STATE_INIT = 'tonConnectGetStateInit',
TONCONNECT_TRANSFER = 'tonConnectTransfer',
TONCONNECT_SIGN_DATA = 'tonConnectSignData',
Expand All @@ -23,6 +24,7 @@ export enum WalletExposeRPCMethod {
SWITCH_CHAIN = 'switchChain',
SET_BACKGROUND_EMBED = 'setBackgroundEmbed',
SHOW_RESET_PASSWORD = 'showResetPassword',
VERIFY_PASSWORD = 'verifyPassword',
}

// TODO: grow this list
Expand Down
28 changes: 1 addition & 27 deletions packages/sdk/src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,7 @@ export {
HibitIdErrorCode,
} from './enums';
export { BridgePromise } from './types';
export type {
HibitEnv,
Language,
FixDevMode,
HibitIdWalletOptions,
ConnectRequest,
ConnectedRequest,
GetAccountRequest,
GetAccountResponse,
GetChainInfoResponse,
SignMessageRequest,
SignMessageResponse,
GetBalanceRequest,
GetBalanceResponse,
TransferRequest,
TransferResponse,
TonConnectGetStateInitResponse,
TonConnectSignDataRequest,
TonConnectSignDataResponse,
TonConnectTransferRequest,
TonConnectTransferResponse,
SwitchChainRequest,
ChainChangedRequest,
AccountsChangedRequest,
LoginChangedRequest,
SetBackgroundEmbedRequest,
} from './types';
export type * from './types';
export type {
TonConnectTransactionPayload,
TonConnectTransactionPayloadMessage,
Expand Down
12 changes: 12 additions & 0 deletions packages/sdk/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,18 @@ export type TransferResponse = RpcBaseResponse<{
txHash: string
}>

export type GetEstimatedFeeRequest = TransferRequest

export type GetEstimatedFeeResponse = RpcBaseResponse<{
fee: string // in minimal unit (like wei for eth)
}>

export interface VerifyPasswordRequest {
password: string
}

export type VerifyPasswordResponse = RpcBaseResponse<null>

export type TonConnectGetStateInitResponse = RpcBaseResponse<{
stateInitBase64: string
}>
Expand Down
42 changes: 42 additions & 0 deletions packages/sdk/src/lib/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
GetBalanceRequest,
GetBalanceResponse,
GetChainInfoResponse,
GetEstimatedFeeRequest,
GetEstimatedFeeResponse,
HibitIdError,
HibitIdEventHandlerMap,
HibitIdWalletOptions,
Expand All @@ -28,6 +30,8 @@ import {
TonConnectTransferResponse,
TransferRequest,
TransferResponse,
VerifyPasswordRequest,
VerifyPasswordResponse,
} from './types';
import {
SdkExposeRPCMethod,
Expand Down Expand Up @@ -230,6 +234,25 @@ export class HibitIdWallet {
}
};

public getEstimatedFee = async (option: GetEstimatedFeeRequest): Promise<string> => {
const request: GetEstimatedFeeRequest = option || {};
console.debug('[sdk call getEstimatedFee]', { request });
await this._iframeReadyPromise.promise;
this.assertConnected();
try {
const res = await this._rpc?.call<GetEstimatedFeeResponse>(
WalletExposeRPCMethod.GET_ESTIMATED_FEE,
request
);
if (!res?.success) {
throw new Error(res?.errMsg);
}
return res.data.fee ?? null;
} catch (e: any) {
throw new Error(`Get estimated fee failed: ${this.getRpcErrorMessage(e)}`);
}
}

public tonConnectGetStateInit = async (): Promise<string> => {
console.debug('[sdk call TonConnectGetStateInit]');
await this._iframeReadyPromise.promise;
Expand Down Expand Up @@ -366,6 +389,25 @@ export class HibitIdWallet {
this.showIframe();
}

public verifyPassword = async (option: VerifyPasswordRequest): Promise<boolean> => {
const request: VerifyPasswordRequest = option || {};
console.debug('[sdk call verifyPassword]');
await this._iframeReadyPromise.promise;
this.assertConnected();
try {
const res = await this._rpc?.call<VerifyPasswordResponse>(
WalletExposeRPCMethod.VERIFY_PASSWORD,
request
);
if (!res?.success) {
throw new Error(res?.errMsg);
}
return true;
} catch (e: any) {
throw new Error(`Verify password failed: ${this.getRpcErrorMessage(e)}`);
}
}

public addEventListener = <K extends keyof HibitIdEventHandlerMap>(
event: K,
handler: HibitIdEventHandlerMap[K]
Expand Down
12 changes: 12 additions & 0 deletions packages/sdk/src/test/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ const App: FC = () => {
reset password
</button>
</div>
<div>
<button className="btn btn-sm" onClick={async () => {
try {
await wallet?.verifyPassword({ password: '123456' })
alert('password verified')
} catch (e: any) {
alert(e.message || e)
}
}}>
verify password
</button>
</div>
<div>
<button className="btn btn-sm" onClick={async () => {
await wallet?.setBackgroundEmbed(!isBackgroundEmbed)
Expand Down
4 changes: 4 additions & 0 deletions packages/sdk/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export default defineConfig({
// the proper extensions will be added
fileName: 'hibit-id-wallet-sdk',
},
commonjsOptions: {
transformMixedEsModules: true,
include: [/node_modules/, /crypto-lib/]
},
rollupOptions: {
// make sure to externalize deps that shouldn't be bundled
// into your library
Expand Down

0 comments on commit 6452694

Please sign in to comment.