Skip to content

Commit

Permalink
v4 fix: dynex issues OK-27498 OK-27511 (#4522)
Browse files Browse the repository at this point in the history
* chore: add biginerger to handle dynex varint

* fix: dynex varint error

* fix: max send value display error
  • Loading branch information
weatherstar authored Apr 29, 2024
1 parent 8fc6e96 commit 9e1accb
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 11 deletions.
15 changes: 15 additions & 0 deletions packages/engine/@types/biginteger.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
declare module 'biginteger' {
export class BigInteger {
constructor(n: number | string | BigInteger, base?: number): BigInteger;

compare(n: number | string | BigInteger): number;

divide(n: number | string | BigInteger): BigInteger;

pow(n: number | string | BigInteger): BigInteger;

toJSValue(): number;

lowVal(): number;
}
}
1 change: 1 addition & 0 deletions packages/engine/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"axios-retry": "^3.4.0",
"bchaddrjs": "^0.5.2",
"bech32": "^2.0.0",
"biginteger": "^1.0.3",
"bignumber.js": "^9.0.1",
"bip32": "^4.0.0",
"bip39": "^3.1.0",
Expand Down
2 changes: 2 additions & 0 deletions packages/engine/src/vaults/impl/dynex/Vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ export default class Vault extends VaultBase {
.shiftedBy(network.decimals)
.toFixed(),
extraInfo: null,
utxoFrom: [],
utxoTo: [],
},
direction:
encodedTx.to === address
Expand Down
19 changes: 9 additions & 10 deletions packages/engine/src/vaults/impl/dynex/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable no-param-reassign */
/* eslint-disable no-bitwise */
import { keccak256 } from '@ethersproject/keccak256';
import { BigInteger } from 'biginteger';
import BigNumber from 'bignumber.js';

import type { IEncodedTxDynex, ISignTxParams } from './types';
Expand All @@ -11,16 +12,14 @@ const CRYPTONOTE_PUBLIC_INTEGRATED_ADDRESS_BASE58_PREFIX = 29;
const CRYPTONOTE_PUBLIC_SUBADDRESS_BASE58_PREFIX = 52;

export function encodeVarInt(number: number) {
const result = [];
do {
let byte = number & 0x7f;
number >>>= 7;
if (number !== 0) {
byte |= 0x80;
}
result.push(byte);
} while (number !== 0);
return result.map((byte) => byte.toString(16).padStart(2, '0')).join('');
let numberBI = new BigInteger(number);
let out = '';
while (numberBI.compare(0x80) >= 0) {
out += `0${((numberBI.lowVal() & 0x7f) | 0x80).toString(16)}`.slice(-2);
numberBI = numberBI.divide(new BigInteger(2).pow(7));
}
out += `0${numberBI.toJSValue().toString(16)}`.slice(-2);
return out;
}

export function integerToLittleEndianHex({
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/utils/hardware/OneKeyHardware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const getDeviceTypeByDeviceId = (deviceId?: string): IDeviceType =>
getDeviceTypeByDeviceIdUtil(deviceId);

export const isHwClassic = (deviceType: string | undefined): boolean =>
deviceType === 'classic' || deviceType === 'classic1s';
deviceType === 'classic';

export const getDeviceFirmwareVersion = (
features: IOneKeyDeviceFeatures | undefined,
Expand Down
15 changes: 15 additions & 0 deletions patches/biginteger+1.0.3.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
diff --git a/node_modules/biginteger/biginteger.js b/node_modules/biginteger/biginteger.js
index b881105..41967f5 100644
--- a/node_modules/biginteger/biginteger.js
+++ b/node_modules/biginteger/biginteger.js
@@ -1564,6 +1564,10 @@ BigInteger.prototype.toJSValue = function() {
return parseInt(this.toString(), 10);
};

+BigInteger.prototype.lowVal = function () {
+ return this._d[0] || 0;
+};
+
var MAX_EXP = BigInteger(0x7FFFFFFF);
// Constant: MAX_EXP
// The largest exponent allowed in <pow> and <exp10> (0x7FFFFFFF or 2147483647).
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7039,6 +7039,7 @@ __metadata:
axios-retry: ^3.4.0
bchaddrjs: ^0.5.2
bech32: ^2.0.0
biginteger: ^1.0.3
bignumber.js: ^9.0.1
bip32: ^4.0.0
bip39: ^3.1.0
Expand Down Expand Up @@ -13986,6 +13987,13 @@ __metadata:
languageName: node
linkType: hard

"biginteger@npm:^1.0.3":
version: 1.0.3
resolution: "biginteger@npm:1.0.3"
checksum: 325458d3801885eef4468ddb1d9b24fd4773d958fdd85bf75a7e71e856cea0009f7b319aec718ed5a64ce9e7f0aa4010b23b702e9576a840b8710e04e9550080
languageName: node
linkType: hard

"bignumber.js@npm:^9.0.0, bignumber.js@npm:^9.0.1, bignumber.js@npm:^9.0.2":
version: 9.0.2
resolution: "bignumber.js@npm:9.0.2"
Expand Down

0 comments on commit 9e1accb

Please sign in to comment.