@alephium/web3 0.39.2 → 0.39.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -21,6 +21,8 @@ export declare function stringToHex(str: string): string;
|
|
|
21
21
|
export declare function hexToString(str: string): string;
|
|
22
22
|
export declare function sleep(ms: number): Promise<void>;
|
|
23
23
|
export declare function isDevnet(networkId?: number): boolean;
|
|
24
|
+
export declare function targetToDifficulty(compactedTarget: HexString): bigint;
|
|
25
|
+
export declare function difficultyToTarget(diff: bigint): HexString;
|
|
24
26
|
type _Eq<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false;
|
|
25
27
|
export type Eq<X, Y> = _Eq<{
|
|
26
28
|
[P in keyof X]: X[P];
|
package/dist/src/utils/utils.js
CHANGED
|
@@ -20,7 +20,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
20
20
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
21
21
|
};
|
|
22
22
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
|
-
exports.assertType = exports.isDevnet = exports.sleep = exports.hexToString = exports.stringToHex = exports.blockChainIndex = exports.binToHex = exports.hexToBinUnsafe = exports.toNonNegativeBigInt = exports.isHexString = exports.signatureDecode = exports.encodeHexSignature = exports.encodeSignature = exports.networkIds = void 0;
|
|
23
|
+
exports.assertType = exports.difficultyToTarget = exports.targetToDifficulty = exports.isDevnet = exports.sleep = exports.hexToString = exports.stringToHex = exports.blockChainIndex = exports.binToHex = exports.hexToBinUnsafe = exports.toNonNegativeBigInt = exports.isHexString = exports.signatureDecode = exports.encodeHexSignature = exports.encodeSignature = exports.networkIds = void 0;
|
|
24
24
|
const elliptic_1 = require("elliptic");
|
|
25
25
|
const bn_js_1 = __importDefault(require("bn.js"));
|
|
26
26
|
const buffer_1 = require("buffer/");
|
|
@@ -110,6 +110,30 @@ function isDevnet(networkId) {
|
|
|
110
110
|
return networkId !== 0 && networkId !== 1;
|
|
111
111
|
}
|
|
112
112
|
exports.isDevnet = isDevnet;
|
|
113
|
+
function targetToDifficulty(compactedTarget) {
|
|
114
|
+
if (!isHexString(compactedTarget) || compactedTarget.length !== 8) {
|
|
115
|
+
throw Error(`Invalid target ${compactedTarget}, expected a hex string of length 8`);
|
|
116
|
+
}
|
|
117
|
+
const size = hexToBinUnsafe(compactedTarget.slice(0, 2))[0];
|
|
118
|
+
const mantissa = BigInt('0x' + compactedTarget.slice(2));
|
|
119
|
+
const maxBigInt = 1n << 256n;
|
|
120
|
+
const target = size <= 3 ? mantissa >> BigInt(8 * (3 - size)) : mantissa << BigInt(8 * (size - 3));
|
|
121
|
+
return maxBigInt / target;
|
|
122
|
+
}
|
|
123
|
+
exports.targetToDifficulty = targetToDifficulty;
|
|
124
|
+
function difficultyToTarget(diff) {
|
|
125
|
+
const maxBigInt = 1n << 256n;
|
|
126
|
+
const target = diff === 1n ? maxBigInt - 1n : maxBigInt / diff;
|
|
127
|
+
const size = Math.floor((target.toString(2).length + 7) / 8);
|
|
128
|
+
const mantissa = size <= 3
|
|
129
|
+
? BigInt.asIntN(32, target) << BigInt(8 * (3 - size))
|
|
130
|
+
: BigInt.asIntN(32, target >> BigInt(8 * (size - 3)));
|
|
131
|
+
const mantissaBytes = buffer_1.Buffer.alloc(4);
|
|
132
|
+
mantissaBytes.writeInt32BE(Number(mantissa), 0);
|
|
133
|
+
const bytes = new Uint8Array([size, ...mantissaBytes.slice(1)]);
|
|
134
|
+
return binToHex(bytes);
|
|
135
|
+
}
|
|
136
|
+
exports.difficultyToTarget = difficultyToTarget;
|
|
113
137
|
// eslint-disable-next-line @typescript-eslint/no-empty-function, @typescript-eslint/no-unused-vars
|
|
114
138
|
function assertType() { }
|
|
115
139
|
exports.assertType = assertType;
|
package/package.json
CHANGED
package/src/utils/utils.ts
CHANGED
|
@@ -112,6 +112,31 @@ export function isDevnet(networkId?: number): boolean {
|
|
|
112
112
|
return networkId !== 0 && networkId !== 1
|
|
113
113
|
}
|
|
114
114
|
|
|
115
|
+
export function targetToDifficulty(compactedTarget: HexString): bigint {
|
|
116
|
+
if (!isHexString(compactedTarget) || compactedTarget.length !== 8) {
|
|
117
|
+
throw Error(`Invalid target ${compactedTarget}, expected a hex string of length 8`)
|
|
118
|
+
}
|
|
119
|
+
const size = hexToBinUnsafe(compactedTarget.slice(0, 2))[0]
|
|
120
|
+
const mantissa = BigInt('0x' + compactedTarget.slice(2))
|
|
121
|
+
const maxBigInt = 1n << 256n
|
|
122
|
+
const target = size <= 3 ? mantissa >> BigInt(8 * (3 - size)) : mantissa << BigInt(8 * (size - 3))
|
|
123
|
+
return maxBigInt / target
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function difficultyToTarget(diff: bigint): HexString {
|
|
127
|
+
const maxBigInt = 1n << 256n
|
|
128
|
+
const target = diff === 1n ? maxBigInt - 1n : maxBigInt / diff
|
|
129
|
+
const size = Math.floor((target.toString(2).length + 7) / 8)
|
|
130
|
+
const mantissa =
|
|
131
|
+
size <= 3
|
|
132
|
+
? BigInt.asIntN(32, target) << BigInt(8 * (3 - size))
|
|
133
|
+
: BigInt.asIntN(32, target >> BigInt(8 * (size - 3)))
|
|
134
|
+
const mantissaBytes = Buffer.alloc(4)
|
|
135
|
+
mantissaBytes.writeInt32BE(Number(mantissa), 0)
|
|
136
|
+
const bytes = new Uint8Array([size, ...mantissaBytes.slice(1)])
|
|
137
|
+
return binToHex(bytes)
|
|
138
|
+
}
|
|
139
|
+
|
|
115
140
|
type _Eq<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false
|
|
116
141
|
export type Eq<X, Y> = _Eq<{ [P in keyof X]: X[P] }, { [P in keyof Y]: Y[P] }>
|
|
117
142
|
// eslint-disable-next-line @typescript-eslint/no-empty-function, @typescript-eslint/no-unused-vars
|