Skip to content

Commit

Permalink
chore: optimize normalize
Browse files Browse the repository at this point in the history
  • Loading branch information
wabicai committed Dec 2, 2024
1 parent fae349c commit bf77053
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
8 changes: 7 additions & 1 deletion packages/core/src/api/benfen/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ export function publicKeyToAddress(publicKey: string) {
);
}

/**
* 将十六进制地址转换为 BFC 格式地址
* @param hexAddress - 输入的十六进制地址(可以带有0x前缀)
* @returns BFC格式的地址,格式为:BFC + 64位地址 + 4位校验和
* @throws {Error} 当输入地址格式无效时
*/
export function hex2BfcAddress(hexAddress: string): string {
// 如果已经是BFC格式,直接返回
if (/^BFC/i.test(hexAddress)) {
Expand All @@ -37,7 +43,7 @@ export function hex2BfcAddress(hexAddress: string): string {
const hex = hexAddress.replace(/^0x/, '').padStart(64, '0').toLowerCase();

// 使用SHA-256计算校验和
const hash = sha256(new TextEncoder().encode(hex));
const hash = sha256(hexToBytes(hex));
const checksumHex = bytesToHex(hash).slice(0, 4);

// 返回BFC格式地址
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/api/sui/SuiGetAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export default class SuiGetAddress extends BaseMethod<HardwareSuiGetAddress[]> {
'SuiAddress',
param
);
address = addressRes.message.address?.toLowerCase();
address = addressRes.message.address?.toLowerCase() ?? '';
} else {
address = publicKeyToAddress(publicKey);
}
Expand Down

0 comments on commit bf77053

Please sign in to comment.