Skip to content

Commit

Permalink
fix: return address with BFC type (#386)
Browse files Browse the repository at this point in the history
* fix: return address with BFC type

* fix: return address with BFC type

* fix: return bfc address origin, buffer in signtransaction

* fix: remove tolowercase in benfen
  • Loading branch information
wabicai authored Dec 2, 2024
1 parent 93c33d5 commit a9109a1
Show file tree
Hide file tree
Showing 17 changed files with 69 additions and 47 deletions.
2 changes: 1 addition & 1 deletion packages/connect-examples/electron-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "hardware-example",
"productName": "HardwareExample",
"executableName": "onekey-hardware-example",
"version": "1.0.15-alpha.0",
"version": "1.0.15-alpha.1",
"author": "OneKey",
"description": "End-to-end encrypted workspaces for teams",
"main": "dist/index.js",
Expand Down
10 changes: 5 additions & 5 deletions packages/connect-examples/expo-example/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "expo-example",
"version": "1.0.15-alpha.0",
"version": "1.0.15-alpha.1",
"scripts": {
"start": "CONNECT_SRC=https://localhost:8087/ yarn expo start --dev-client",
"android": "yarn expo run:android",
Expand All @@ -19,10 +19,10 @@
"@noble/ed25519": "^2.1.0",
"@noble/hashes": "^1.3.3",
"@noble/secp256k1": "^1.7.1",
"@onekeyfe/hd-ble-sdk": "^1.0.15-alpha.0",
"@onekeyfe/hd-common-connect-sdk": "^1.0.15-alpha.0",
"@onekeyfe/hd-core": "^1.0.15-alpha.0",
"@onekeyfe/hd-web-sdk": "^1.0.15-alpha.0",
"@onekeyfe/hd-ble-sdk": "^1.0.15-alpha.1",
"@onekeyfe/hd-common-connect-sdk": "^1.0.15-alpha.1",
"@onekeyfe/hd-core": "^1.0.15-alpha.1",
"@onekeyfe/hd-web-sdk": "^1.0.15-alpha.1",
"@onekeyfe/react-native-ble-plx": "3.0.0",
"@polkadot/util-crypto": "13.1.1",
"@react-native-async-storage/async-storage": "1.21.0",
Expand Down
6 changes: 3 additions & 3 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@onekeyfe/hd-core",
"version": "1.0.15-alpha.0",
"version": "1.0.15-alpha.1",
"description": "> TODO: description",
"author": "OneKey",
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
Expand All @@ -25,8 +25,8 @@
"url": "https://github.com/OneKeyHQ/hardware-js-sdk/issues"
},
"dependencies": {
"@onekeyfe/hd-shared": "^1.0.15-alpha.0",
"@onekeyfe/hd-transport": "^1.0.15-alpha.0",
"@onekeyfe/hd-shared": "^1.0.15-alpha.1",
"@onekeyfe/hd-transport": "^1.0.15-alpha.1",
"axios": "^0.27.2",
"bignumber.js": "^9.0.2",
"bytebuffer": "^5.0.1",
Expand Down
9 changes: 5 additions & 4 deletions packages/core/src/api/benfen/BenfenGetAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { BaseMethod } from '../BaseMethod';
import { validateParams, validateResult } from '../helpers/paramsValidator';
import { serializedPath, validatePath } from '../helpers/pathUtils';
import { UI_REQUEST } from '../../constants/ui-request';
import { publicKeyToAddress } from './normalize';
import { hex2BfcAddress, publicKeyToAddress } from './normalize';
import { BenfenAddress, BenfenGetAddressParams } from '../../types';
import { supportBatchPublicKey } from '../../utils/deviceFeaturesUtils';

Expand Down Expand Up @@ -77,14 +77,15 @@ export default class BenfenGetAddress extends BaseMethod<HardwareBenfenGetAddres
'Address',
param
);
address = addressRes.message.address?.toLowerCase() ?? '';
address = addressRes.message.address;
} else {
address = publicKeyToAddress(publicKey);
}

const result = {
path: serializedPath(param.address_n),
address,
// 将 address 转为BFC格式
address: hex2BfcAddress(address),
pub: publicKey,
};

Expand All @@ -100,7 +101,7 @@ export default class BenfenGetAddress extends BaseMethod<HardwareBenfenGetAddres
const res = await this.device.commands.typedCall('BenfenGetAddress', 'Address', param);
const result = {
path: serializedPath(param.address_n),
address: res.message.address?.toLowerCase() ?? '',
address: hex2BfcAddress(res.message.address),
};
if (this.shouldConfirm) {
this.postPreviousAddressMessage(result);
Expand Down
7 changes: 5 additions & 2 deletions packages/core/src/api/benfen/BenfenSignTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,19 @@ export default class BenfenSignTransaction extends BaseMethod<BenfenSignTx> {
validateParams(this.payload, [
{ name: 'path', required: true },
{ name: 'rawTx', type: 'hexString', required: true },
{ name: 'coinType', type: 'hexString', required: true },
{ name: 'coinType', type: 'string', required: true },
]);

const { path, rawTx, coinType } = this.payload;
const addressN = validatePath(path, 3);
const coinTypeHex = coinType.startsWith('0x')
? coinType.slice(2)
: Buffer.from(coinType).toString('hex');

this.params = {
address_n: addressN,
raw_tx: formatAnyHex(rawTx),
coin_type: formatAnyHex(coinType),
coin_type: formatAnyHex(coinTypeHex),
};
}

Expand Down
18 changes: 18 additions & 0 deletions packages/core/src/api/benfen/normalize.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { bytesToHex, hexToBytes } from '@noble/hashes/utils';
import { blake2b } from '@noble/hashes/blake2b';
import { sha256 } from '@noble/hashes/sha256';

export const BENFEN_ADDRESS_LENGTH = 32;
export const PUBLIC_KEY_SIZE = 32;
Expand All @@ -25,3 +26,20 @@ export function publicKeyToAddress(publicKey: string) {
bytesToHex(blake2b(tmp, { dkLen: 32 })).slice(0, BENFEN_ADDRESS_LENGTH * 2)
);
}

export function hex2BfcAddress(hexAddress: string): string {
// 如果已经是BFC格式,直接返回
if (/^BFC/i.test(hexAddress)) {
return hexAddress;
}

// 移除0x前缀,补齐64位,转小写
const hex = hexAddress.replace(/^0x/, '').padStart(64, '0').toLowerCase();

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

// 返回BFC格式地址
return `BFC${hex}${checksumHex}`;
}
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
8 changes: 4 additions & 4 deletions packages/hd-ble-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@onekeyfe/hd-ble-sdk",
"version": "1.0.15-alpha.0",
"version": "1.0.15-alpha.1",
"author": "OneKey",
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
"license": "ISC",
Expand All @@ -20,8 +20,8 @@
"lint:fix": "eslint . --fix"
},
"dependencies": {
"@onekeyfe/hd-core": "^1.0.15-alpha.0",
"@onekeyfe/hd-shared": "^1.0.15-alpha.0",
"@onekeyfe/hd-transport-react-native": "^1.0.15-alpha.0"
"@onekeyfe/hd-core": "^1.0.15-alpha.1",
"@onekeyfe/hd-shared": "^1.0.15-alpha.1",
"@onekeyfe/hd-transport-react-native": "^1.0.15-alpha.1"
}
}
12 changes: 6 additions & 6 deletions packages/hd-common-connect-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@onekeyfe/hd-common-connect-sdk",
"version": "1.0.15-alpha.0",
"version": "1.0.15-alpha.1",
"author": "OneKey",
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
"license": "ISC",
Expand All @@ -20,10 +20,10 @@
"lint:fix": "eslint . --fix"
},
"dependencies": {
"@onekeyfe/hd-core": "^1.0.15-alpha.0",
"@onekeyfe/hd-shared": "^1.0.15-alpha.0",
"@onekeyfe/hd-transport-http": "^1.0.15-alpha.0",
"@onekeyfe/hd-transport-lowlevel": "^1.0.15-alpha.0",
"@onekeyfe/hd-transport-webusb": "^1.0.15-alpha.0"
"@onekeyfe/hd-core": "^1.0.15-alpha.1",
"@onekeyfe/hd-shared": "^1.0.15-alpha.1",
"@onekeyfe/hd-transport-http": "^1.0.15-alpha.1",
"@onekeyfe/hd-transport-lowlevel": "^1.0.15-alpha.1",
"@onekeyfe/hd-transport-webusb": "^1.0.15-alpha.1"
}
}
6 changes: 3 additions & 3 deletions packages/hd-transport-http/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@onekeyfe/hd-transport-http",
"version": "1.0.15-alpha.0",
"version": "1.0.15-alpha.1",
"description": "hardware http transport",
"author": "OneKey",
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
Expand All @@ -24,8 +24,8 @@
"url": "https://github.com/OneKeyHQ/hardware-js-sdk/issues"
},
"dependencies": {
"@onekeyfe/hd-shared": "^1.0.15-alpha.0",
"@onekeyfe/hd-transport": "^1.0.15-alpha.0",
"@onekeyfe/hd-shared": "^1.0.15-alpha.1",
"@onekeyfe/hd-transport": "^1.0.15-alpha.1",
"axios": "^0.27.2"
}
}
6 changes: 3 additions & 3 deletions packages/hd-transport-lowlevel/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@onekeyfe/hd-transport-lowlevel",
"version": "1.0.15-alpha.0",
"version": "1.0.15-alpha.1",
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
"license": "MIT",
"main": "dist/index.js",
Expand All @@ -19,7 +19,7 @@
"lint:fix": "eslint . --fix"
},
"dependencies": {
"@onekeyfe/hd-shared": "^1.0.15-alpha.0",
"@onekeyfe/hd-transport": "^1.0.15-alpha.0"
"@onekeyfe/hd-shared": "^1.0.15-alpha.1",
"@onekeyfe/hd-transport": "^1.0.15-alpha.1"
}
}
6 changes: 3 additions & 3 deletions packages/hd-transport-react-native/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@onekeyfe/hd-transport-react-native",
"version": "1.0.15-alpha.0",
"version": "1.0.15-alpha.1",
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
"license": "MIT",
"main": "dist/index.js",
Expand All @@ -19,8 +19,8 @@
"lint:fix": "eslint . --fix"
},
"dependencies": {
"@onekeyfe/hd-shared": "^1.0.15-alpha.0",
"@onekeyfe/hd-transport": "^1.0.15-alpha.0",
"@onekeyfe/hd-shared": "^1.0.15-alpha.1",
"@onekeyfe/hd-transport": "^1.0.15-alpha.1",
"@onekeyfe/react-native-ble-plx": "3.0.1",
"react-native-ble-manager": "^8.1.0"
}
Expand Down
6 changes: 3 additions & 3 deletions packages/hd-transport-webusb/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@onekeyfe/hd-transport-webusb",
"version": "1.0.15-alpha.0",
"version": "1.0.15-alpha.1",
"author": "OneKey",
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
"license": "MIT",
Expand All @@ -20,8 +20,8 @@
"lint:fix": "eslint . --fix"
},
"dependencies": {
"@onekeyfe/hd-shared": "^1.0.15-alpha.0",
"@onekeyfe/hd-transport": "^1.0.15-alpha.0"
"@onekeyfe/hd-shared": "^1.0.15-alpha.1",
"@onekeyfe/hd-transport": "^1.0.15-alpha.1"
},
"devDependencies": {
"@types/w3c-web-usb": "^1.0.6"
Expand Down
2 changes: 1 addition & 1 deletion packages/hd-transport/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@onekeyfe/hd-transport",
"version": "1.0.15-alpha.0",
"version": "1.0.15-alpha.1",
"description": "> TODO: description",
"author": "OneKey",
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
Expand Down
4 changes: 2 additions & 2 deletions packages/hd-transport/src/types/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export type BenfenGetAddress = {

// BenfenAddress
export type BenfenAddress = {
address?: string;
address: string;
};

// BenfenSignTx
Expand Down Expand Up @@ -3720,7 +3720,7 @@ export type SuiGetAddress = {

// SuiAddress
export type SuiAddress = {
address?: string;
address: string;
};

// SuiSignTx
Expand Down
10 changes: 5 additions & 5 deletions packages/hd-web-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@onekeyfe/hd-web-sdk",
"version": "1.0.15-alpha.0",
"version": "1.0.15-alpha.1",
"author": "OneKey",
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
"license": "ISC",
Expand All @@ -21,10 +21,10 @@
},
"dependencies": {
"@onekeyfe/cross-inpage-provider-core": "^0.0.17",
"@onekeyfe/hd-core": "^1.0.15-alpha.0",
"@onekeyfe/hd-shared": "^1.0.15-alpha.0",
"@onekeyfe/hd-transport-http": "^1.0.15-alpha.0",
"@onekeyfe/hd-transport-webusb": "^1.0.15-alpha.0"
"@onekeyfe/hd-core": "^1.0.15-alpha.1",
"@onekeyfe/hd-shared": "^1.0.15-alpha.1",
"@onekeyfe/hd-transport-http": "^1.0.15-alpha.1",
"@onekeyfe/hd-transport-webusb": "^1.0.15-alpha.1"
},
"devDependencies": {
"@babel/plugin-proposal-optional-chaining": "^7.17.12",
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@onekeyfe/hd-shared",
"version": "1.0.15-alpha.0",
"version": "1.0.15-alpha.1",
"description": "Hardware SDK's shared tool library",
"keywords": [
"Hardware-SDK",
Expand Down

0 comments on commit a9109a1

Please sign in to comment.