Skip to content

Commit

Permalink
optimize: sol sign message format
Browse files Browse the repository at this point in the history
  • Loading branch information
ByteZhang1024 committed Dec 30, 2024
1 parent e35e165 commit 9621821
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/kit-bg/src/vaults/impls/sol/KeyringHardware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { ETranslations } from '@onekeyhq/shared/src/locale';
import { appLocale } from '@onekeyhq/shared/src/locale/appLocale';
import accountUtils from '@onekeyhq/shared/src/utils/accountUtils';
import { checkIsDefined } from '@onekeyhq/shared/src/utils/assertUtils';
import stringUtils from '@onekeyhq/shared/src/utils/stringUtils';

import { KeyringHardwareBase } from '../../base/KeyringHardwareBase';

Expand Down Expand Up @@ -203,6 +204,19 @@ export class KeyringHardware extends KeyringHardwareBase {
);
}

guessMessageFormat(message: Buffer) {
if (Object.prototype.toString.call(message) !== '[object Uint8Array]') {
return undefined;
}
if (stringUtils.isPrintableASCII(message)) {
return 0;
}
if (stringUtils.isUTF8(message)) {
return 1;
}
return undefined;
}

override async signMessage(
params: ISignMessageParams,
): Promise<ISignedMessagePro> {
Expand All @@ -221,6 +235,9 @@ export class KeyringHardware extends KeyringHardwareBase {
...params.deviceParams?.deviceCommonParams,
path: dbAccount.path,
messageHex: Buffer.from(payload.message).toString('hex'),
messageFormat: this.guessMessageFormat(
Buffer.from(payload.message),
),
},
);

Expand Down
1 change: 1 addition & 0 deletions packages/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"webextension-polyfill": "^0.8.0"
},
"devDependencies": {
"@types/utf-8-validate": "^5.0.2",
"folderslint": "^1.2.0",
"rimraf": "^5.0.5"
}
Expand Down
14 changes: 14 additions & 0 deletions packages/shared/src/utils/stringUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import safeStringify from 'fast-safe-stringify';
import isValidUTF8 from 'utf-8-validate';

export function equalsIgnoreCase(
a: string | undefined | null,
Expand Down Expand Up @@ -32,10 +33,23 @@ export function capitalizeWords(str: string): string {
return str.replace(/\b\w/g, (match) => match.toUpperCase());
}

export function isPrintableASCII(buffer: Buffer): boolean {
return (
buffer && buffer.every((element) => element >= 0x20 && element <= 0x7e)
);
}

export function isUTF8(buffer: Buffer): boolean {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call
return buffer && isValidUTF8(buffer);
}

export default {
stableStringify,
safeStringify,
randomString,
equalsIgnoreCase,
capitalizeWords,
isPrintableASCII,
isUTF8,
};
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7081,6 +7081,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@onekeyhq/shared@workspace:packages/shared"
dependencies:
"@types/utf-8-validate": "npm:^5.0.2"
"@types/webextension-polyfill": "npm:^0.8.2"
async-mutex: "npm:^0.3.2"
fast-safe-stringify: "npm:^2.1.1"
Expand Down Expand Up @@ -14558,6 +14559,15 @@ __metadata:
languageName: node
linkType: hard

"@types/utf-8-validate@npm:^5.0.2":
version: 5.0.2
resolution: "@types/utf-8-validate@npm:5.0.2"
dependencies:
"@types/node": "npm:*"
checksum: 10/0853fe2dc4c2ae4c86c161e9c195d248cf76730497cc9156c7dadba3e6e7dcd9d233ff2c0b2683e5616871b927a6f86dd826192a6927446c74d6c031605ebcb2
languageName: node
linkType: hard

"@types/verror@npm:^1.10.3":
version: 1.10.9
resolution: "@types/verror@npm:1.10.9"
Expand Down

0 comments on commit 9621821

Please sign in to comment.