-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Remove CoreMask class * update version * update docs * version * format
- Loading branch information
Showing
6 changed files
with
110 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,67 @@ | ||
export const VOID_MASK = '0x00000000000000000000'; // hex encoded 80 bit bitmap. | ||
export const COMPLETE_MASK = '0xffffffffffffffffffff'; // hex encoded 80 bit bitmap. | ||
|
||
export class CoreMask { | ||
private mask: string; | ||
|
||
toRawHex(): string { | ||
return this.mask; | ||
} | ||
|
||
constructor(mask: string) { | ||
// 10 hex characters plus the '0x' at the start. | ||
if (isHex(mask) && mask.length === 22) { | ||
this.mask = mask; | ||
} else { | ||
throw new Error('Invalid mask'); | ||
} | ||
} | ||
export function completeMask(): string { | ||
return COMPLETE_MASK; | ||
} | ||
|
||
static completeMask(): CoreMask { | ||
return new CoreMask(COMPLETE_MASK); | ||
} | ||
export function voidMask(): string { | ||
return VOID_MASK; | ||
} | ||
|
||
static voidMask(): CoreMask { | ||
return new CoreMask(VOID_MASK); | ||
export function maskFromChunk(from: number, to: number): string { | ||
if (from < 0 || to >= 80 || from > to) { | ||
throw new Error('Invalid bit range'); | ||
} | ||
|
||
static fromChunk(from: number, to: number): CoreMask { | ||
if (from < 0 || to >= 80 || from > to) { | ||
throw new Error('Invalid bit range'); | ||
} | ||
|
||
let mask = 0n; | ||
for (let i = from; i < to; i++) { | ||
mask |= 1n << BigInt(79 - i); | ||
} | ||
|
||
return new CoreMask('0x' + mask.toString(16).padStart(20, '0')); | ||
let mask = 0n; | ||
for (let i = from; i < to; i++) { | ||
mask |= 1n << BigInt(79 - i); | ||
} | ||
|
||
static fromBin(bin: string): CoreMask { | ||
let hexMask = ''; | ||
for (let i = 0; i < bin.length; i += 4) { | ||
const v = parseInt(bin.slice(i, i + 4), 2); | ||
hexMask += v.toString(16); | ||
} | ||
return new CoreMask(`0x${hexMask}`); | ||
} | ||
return '0x' + mask.toString(16).padStart(20, '0'); | ||
} | ||
|
||
public countZeros(): number { | ||
let count = 0; | ||
for (let i = 2; i < this.mask.length; ++i) { | ||
let v = parseInt(this.mask.slice(i, i + 1), 16); | ||
for (let j = 0; j < 4; ++j) { | ||
if ((v & 1) === 0) ++count; | ||
v >>= 1; | ||
} | ||
export function countMaskZeros(mask: string): number { | ||
let count = 0; | ||
for (let i = 2; i < mask.length; ++i) { | ||
let v = parseInt(mask.slice(i, i + 1), 16); | ||
for (let j = 0; j < 4; ++j) { | ||
if ((v & 1) === 0) ++count; | ||
v >>= 1; | ||
} | ||
return count; | ||
} | ||
return count; | ||
} | ||
|
||
public countOnes(): number { | ||
let count = 0; | ||
for (let i = 2; i < this.mask.length; ++i) { | ||
let v = parseInt(this.mask.slice(i, i + 1), 16); | ||
while (v > 0) { | ||
if (v & 1) ++count; | ||
v >>= 1; | ||
} | ||
export function countMaskOnes(mask: string): number { | ||
let count = 0; | ||
for (let i = 2; i < mask.length; ++i) { | ||
let v = parseInt(mask.slice(i, i + 1), 16); | ||
while (v > 0) { | ||
if (v & 1) ++count; | ||
v >>= 1; | ||
} | ||
return count; | ||
} | ||
return count; | ||
} | ||
|
||
public toBin(): string { | ||
let bin = ''; | ||
for (let i = 2; i < this.mask.length; ++i) { | ||
const v = parseInt(this.mask.slice(i, i + 1), 16); | ||
for (let j = 3; j >= 0; --j) { | ||
bin += v & (1 << j) ? '1' : '0'; | ||
} | ||
export function maskToBin(mask: string): string { | ||
let bin = ''; | ||
for (let i = 2; i < mask.length; ++i) { | ||
const v = parseInt(mask.slice(i, i + 1), 16); | ||
for (let j = 3; j >= 0; --j) { | ||
bin += v & (1 << j) ? '1' : '0'; | ||
} | ||
return bin; | ||
} | ||
return bin; | ||
} | ||
|
||
function isHex(str: string) { | ||
const regex = /^0x[0-9a-fA-F]+$/; | ||
return regex.test(str); | ||
export function maskFromBin(bin: string): string { | ||
let hexMask = ''; | ||
for (let i = 0; i < bin.length; i += 4) { | ||
const v = parseInt(bin.slice(i, i + 4), 2); | ||
hexMask += v.toString(16); | ||
} | ||
return `0x${hexMask}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,25 @@ | ||
import { BN } from '@polkadot/util'; | ||
import { RawRegionId } from './types'; | ||
import { RegionId } from './region'; | ||
|
||
export * from './coreMask'; | ||
export * from './region'; | ||
export * from './types'; | ||
|
||
/** | ||
* Encodes the `regionId` into a BigNumber (BN) format. This is used for interacting | ||
* with the xc-regions contract or when conducting cross-chain transfers, where | ||
* `regionId` needs to be represented as a u128. | ||
* | ||
* @param api The API object used to encode the regionId fields. | ||
* @returns The encoded regionId as a BigNumber (BN) | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export function getEncodedRegionId(regionId: RegionId, api: any): RawRegionId { | ||
const encodedBegin = api.createType('u32', regionId.begin).toHex().substring(2); | ||
const encodedCore = api.createType('u16', regionId.core).toHex().substring(2); | ||
|
||
const rawRegionId = encodedBegin + encodedCore + regionId.mask.substring(2); | ||
|
||
return new BN(rawRegionId, 16); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters