Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add uint8array2dec
  • Loading branch information
wk3368 committed Oct 27, 2021
1 parent 152aa46 commit bdf7905
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/utils/helper.ts
Expand Up @@ -21,6 +21,14 @@ export function dec2uint8array(n: number): Uint8Array {
return bitmap
}

export function uint8array2dec(bitmap: Uint8Array): number {
const binArr = []
bitmap.forEach((n) => binArr.push(dec2bin(n).slice(-8)))
return bin2dec(binArr.join(''))
}



// index from left to right
export function setBit(n: number, idx: number): number {
if (idx > 31 || idx < 0) {
Expand Down
5 changes: 4 additions & 1 deletion src/utils/multi-sign.spec.ts
Expand Up @@ -15,7 +15,7 @@ import {
encodeScriptFunctionByResolve
} from "./tx";
import { createMultiEd25519KeyShard, signMultiEd25519KeyShard } from "./multi-sign";
import { dec2bin, bin2dec, setBit, isSetBit, dec2uint8array } from "./helper";
import { dec2bin, bin2dec, setBit, isSetBit, dec2uint8array, uint8array2dec } from "./helper";
import { showMultiEd25519Account } from "./account";
import * as starcoin_types from "../lib/runtime/starcoin_types";

Expand Down Expand Up @@ -186,6 +186,9 @@ test('bit operator', () => {
console.log({ test })
const bitmap = dec2uint8array(test)
console.log({ bitmap })
const test2 = uint8array2dec(bitmap)
console.log({ test2 })
expect(test).toEqual(test2);
const bin = dec2bin(test)
console.log({ bin })
const n = bin2dec(bin)
Expand Down

0 comments on commit bdf7905

Please sign in to comment.