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 31, 2024
1 parent e35e165 commit 1209bbb
Show file tree
Hide file tree
Showing 2 changed files with 84 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
67 changes: 67 additions & 0 deletions packages/shared/src/utils/stringUtils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-bitwise */
import safeStringify from 'fast-safe-stringify';

export function equalsIgnoreCase(
Expand Down Expand Up @@ -32,10 +33,76 @@ 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(buf: Buffer): boolean {
if (!buf) return false;

const len = buf.length;
let i = 0;

while (i < len) {
if ((buf[i] & 0x80) === 0x00) {
// 0xxxxxxx
// eslint-disable-next-line no-plusplus
i++;
} else if ((buf[i] & 0xe0) === 0xc0) {
// 110xxxxx 10xxxxxx
if (
i + 1 === len ||
(buf[i + 1] & 0xc0) !== 0x80 ||
(buf[i] & 0xfe) === 0xc0 // overlong
) {
return false;
}

i += 2;
} else if ((buf[i] & 0xf0) === 0xe0) {
// 1110xxxx 10xxxxxx 10xxxxxx
if (
i + 2 >= len ||
(buf[i + 1] & 0xc0) !== 0x80 ||
(buf[i + 2] & 0xc0) !== 0x80 ||
(buf[i] === 0xe0 && (buf[i + 1] & 0xe0) === 0x80) || // overlong
(buf[i] === 0xed && (buf[i + 1] & 0xe0) === 0xa0) // surrogate (U+D800 - U+DFFF)
) {
return false;
}

i += 3;
} else if ((buf[i] & 0xf8) === 0xf0) {
// 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
if (
i + 3 >= len ||
(buf[i + 1] & 0xc0) !== 0x80 ||
(buf[i + 2] & 0xc0) !== 0x80 ||
(buf[i + 3] & 0xc0) !== 0x80 ||
(buf[i] === 0xf0 && (buf[i + 1] & 0xf0) === 0x80) || // overlong
(buf[i] === 0xf4 && buf[i + 1] > 0x8f) ||
buf[i] > 0xf4 // > U+10FFFF
) {
return false;
}

i += 4;
} else {
return false;
}
}

return true;
}

export default {
stableStringify,
safeStringify,
randomString,
equalsIgnoreCase,
capitalizeWords,
isPrintableASCII,
isUTF8,
};

0 comments on commit 1209bbb

Please sign in to comment.