Skip to content

Commit

Permalink
fix: fixed naming convention
Browse files Browse the repository at this point in the history
  • Loading branch information
hoffmannjan committed Jun 25, 2024
1 parent 3ce1cc7 commit f9734b1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 52 deletions.
28 changes: 14 additions & 14 deletions src/lib/CLValue/Key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ import {
KeyHashAddr,
HashParser,
KeyTransferAddr,
DeployHash,
EraInfo,
Balance,
KeyDeployHash,
KeyEraInfo,
KeyBalance,
KeyBid,
Withdraw,
KeyWithdraw,
KeyDictionary,
KeySystemEntityRegistry,
KeyEraSummary,
Expand All @@ -46,7 +46,7 @@ import {
UNBOND_PREFIX,
CHAINSPEC_REGISTRY_PREFIX,
CHECKSUM_REGISTRY_PREFIX,
BidAddr,
KeyBidAddr,
BidAddrParser
// BID_ADDR_PREFIX,
// PACKAGE_PREFIX,
Expand Down Expand Up @@ -101,7 +101,7 @@ export class CLKeyBytesParser extends CLValueBytesParsers {
case KeyTag.Balance:
return Ok(concat([Uint8Array.from([KeyTag.Balance]), value.data.data]));
case KeyTag.BidAddr:
const bidAddrBytes = BidAddrParser.toBytes(value.data as BidAddr);
const bidAddrBytes = BidAddrParser.toBytes(value.data as KeyBidAddr);
return Ok(concat([Uint8Array.from([KeyTag.BidAddr]), bidAddrBytes]));
case KeyTag.Bid:
return Ok(concat([Uint8Array.from([KeyTag.Bid]), value.data.data]));
Expand Down Expand Up @@ -211,23 +211,23 @@ export class CLKeyBytesParser extends CLValueBytesParsers {
KEY_DEFAULT_BYTE_LENGTH,
contentBytes
);
const deploy = new DeployHash(deployBytes);
const deploy = new KeyDeployHash(deployBytes);
return resultHelper(Ok(new CLKey(deploy)), remainder);
}
case KeyTag.EraInfo: {
const [eraBytes, remainder] = splitAt(
64,
contentBytes
);
const era = new EraInfo(eraBytes);
const era = new KeyEraInfo(eraBytes);
return resultHelper(Ok(new CLKey(era)), remainder);
}
case KeyTag.Balance: {
const [balanceBytes, remainder] = splitAt(
KEY_DEFAULT_BYTE_LENGTH,
contentBytes
);
const balance= new Balance(balanceBytes);
const balance= new KeyBalance(balanceBytes);
return resultHelper(Ok(new CLKey(balance)), remainder);
}
case KeyTag.BidAddr: {
Expand All @@ -252,7 +252,7 @@ export class CLKeyBytesParser extends CLValueBytesParsers {
KEY_DEFAULT_BYTE_LENGTH,
contentBytes
);
const withdraw = new Withdraw(withdrawBytes);
const withdraw = new KeyWithdraw(withdrawBytes);
return resultHelper(Ok(new CLKey(withdraw)), remainder);
}
case KeyTag.Dictionary: {
Expand Down Expand Up @@ -357,16 +357,16 @@ export class CLKey extends CLValue {
case TRANSFER_PREFIX:
return new CLKey(KeyTransferAddr.fromFormattedString(input));
case DEPLOY_HASH_PREFIX:
return new CLKey(DeployHash.fromFormattedString(input));
return new CLKey(KeyDeployHash.fromFormattedString(input));
case ERA_INFO_PREFIX:
return new CLKey(EraInfo.fromFormattedString(input));
return new CLKey(KeyEraInfo.fromFormattedString(input));
case BALANCE_PREFIX:
return new CLKey(Balance.fromFormattedString(input));
return new CLKey(KeyBalance.fromFormattedString(input));
// note: BID_ADDR must come before BID as their heads overlap (bid- / bid-addr-)
case BID_PREFIX:
return new CLKey(KeyBid.fromFormattedString(input));
case WITHDRAW_PREFIX:
return new CLKey(Withdraw.fromFormattedString(input));
return new CLKey(KeyWithdraw.fromFormattedString(input));
case DICTIONARY_PREFIX:
return new CLKey(KeyDictionary.fromFormattedString(input));
case UNBOND_PREFIX:
Expand Down
76 changes: 38 additions & 38 deletions src/lib/CLValue/KeyVariants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export class KeyTransferAddr implements CLKeyVariant {
}
}

export class DeployHash implements CLKeyVariant {
export class KeyDeployHash implements CLKeyVariant {
keyVariant = KeyTag.DeployInfo;
prefix = DEPLOY_HASH_PREFIX;

Expand All @@ -146,19 +146,19 @@ export class DeployHash implements CLKeyVariant {
return `${DEPLOY_HASH_PREFIX}-${this.toString()}`;
}

static fromFormattedString(input: string): DeployHash {
static fromFormattedString(input: string): KeyDeployHash {
if (!input.startsWith(`${DEPLOY_HASH_PREFIX}-`)) {
throw new Error(`Prefix is not ${DEPLOY_HASH_PREFIX}`);
}

const hashStr = input.substring(`${DEPLOY_HASH_PREFIX}-`.length + 1);
const hashBytes = decodeBase16(hashStr);

return new DeployHash(hashBytes);
return new KeyDeployHash(hashBytes);
}
}

export class EraInfo implements CLKeyVariant {
export class KeyEraInfo implements CLKeyVariant {
keyVariant = KeyTag.EraInfo;
prefix = ERA_INFO_PREFIX;
data: BigNumber;
Expand All @@ -179,19 +179,19 @@ export class EraInfo implements CLKeyVariant {
return `${ERA_INFO_PREFIX}-${this.toString()}`;
}

static fromFormattedString(input: string): EraInfo {
static fromFormattedString(input: string): KeyEraInfo {
if (!input.startsWith(`${ERA_INFO_PREFIX}-`)) {
throw new Error(`Prefix is not ${ERA_INFO_PREFIX}`);
}

const hashStr = input.substring(`${ERA_INFO_PREFIX}-`.length + 1);
const hashBytes = decodeBase16(hashStr);

return new EraInfo(hashBytes);
return new KeyEraInfo(hashBytes);
}
}

export class Balance implements CLKeyVariant {
export class KeyBalance implements CLKeyVariant {
keyVariant = KeyTag.Balance;
prefix = BALANCE_PREFIX;

Expand All @@ -209,15 +209,15 @@ export class Balance implements CLKeyVariant {
return `${BALANCE_PREFIX}-${this.toString()}`;
}

static fromFormattedString(input: string): DeployHash {
static fromFormattedString(input: string): KeyBalance {
if (!input.startsWith(`${BALANCE_PREFIX}-`)) {
throw new Error(`Prefix is not ${BALANCE_PREFIX}`);
}

const hashStr = input.substring(`${BALANCE_PREFIX}-`.length + 1);
const hashBytes = decodeBase16(hashStr);

return new Balance(hashBytes);
return new KeyBalance(hashBytes);
}
}

Expand Down Expand Up @@ -251,7 +251,7 @@ export class KeyBid implements CLKeyVariant {
}
}

export class Withdraw implements CLKeyVariant {
export class KeyWithdraw implements CLKeyVariant {
keyVariant = KeyTag.Withdraw;
prefix = WITHDRAW_PREFIX;

Expand All @@ -269,15 +269,15 @@ export class Withdraw implements CLKeyVariant {
return `${WITHDRAW_PREFIX}-${this.toString()}`;
}

static fromFormattedString(input: string): DeployHash {
static fromFormattedString(input: string): KeyWithdraw {
if (!input.startsWith(`${WITHDRAW_PREFIX}-`)) {
throw new Error(`Prefix is not ${WITHDRAW_PREFIX}`);
}

const hashStr = input.substring(`${WITHDRAW_PREFIX}-`.length + 1);
const hashBytes = decodeBase16(hashStr);

return new Withdraw(hashBytes);
return new KeyWithdraw(hashBytes);
}
}

Expand All @@ -299,7 +299,7 @@ export class KeyDictionary implements CLKeyVariant {
return `${DICTIONARY_PREFIX}-${this.toString()}`;
}

static fromFormattedString(input: string): DeployHash {
static fromFormattedString(input: string): KeyDictionary {
if (!input.startsWith(`${DICTIONARY_PREFIX}-`)) {
throw new Error(`Prefix is not ${DICTIONARY_PREFIX}`);
}
Expand Down Expand Up @@ -329,7 +329,7 @@ export class KeySystemEntityRegistry implements CLKeyVariant {
return `${SYSTEM_ENTITY_REGISTRY_PREFIX}-${this.toString()}`;
}

static fromFormattedString(input: string): DeployHash {
static fromFormattedString(input: string): KeySystemEntityRegistry {
if (!input.startsWith(`${SYSTEM_ENTITY_REGISTRY_PREFIX}-`)) {
throw new Error(`Prefix is not ${SYSTEM_ENTITY_REGISTRY_PREFIX}`);
}
Expand Down Expand Up @@ -361,7 +361,7 @@ export class KeyEraSummary implements CLKeyVariant {
return `${ERA_SUMMARY_PREFIX}-${this.toString()}`;
}

static fromFormattedString(input: string): DeployHash {
static fromFormattedString(input: string): KeyEraSummary {
if (!input.startsWith(`${ERA_SUMMARY_PREFIX}-`)) {
throw new Error(`Prefix is not ${ERA_SUMMARY_PREFIX}`);
}
Expand Down Expand Up @@ -391,7 +391,7 @@ export class KeyUnbond implements CLKeyVariant {
return `${UNBOND_PREFIX}-${this.toString()}`;
}

static fromFormattedString(input: string): DeployHash {
static fromFormattedString(input: string): KeyUnbond {
if (!input.startsWith(`${UNBOND_PREFIX}-`)) {
throw new Error(`Prefix is not ${UNBOND_PREFIX}`);
}
Expand Down Expand Up @@ -421,7 +421,7 @@ export class KeyChainspecRegistry implements CLKeyVariant {
return `${CHAINSPEC_REGISTRY_PREFIX}-${this.toString()}`;
}

static fromFormattedString(input: string): DeployHash {
static fromFormattedString(input: string): KeyChainspecRegistry {
if (!input.startsWith(`${CHAINSPEC_REGISTRY_PREFIX}-`)) {
throw new Error(`Prefix is not ${CHAINSPEC_REGISTRY_PREFIX}`);
}
Expand Down Expand Up @@ -451,7 +451,7 @@ export class KeyChecksumRegistry implements CLKeyVariant {
return `${CHECKSUM_REGISTRY_PREFIX}-${this.toString()}`;
}

static fromFormattedString(input: string): DeployHash {
static fromFormattedString(input: string): KeyChecksumRegistry {
if (!input.startsWith(`${CHECKSUM_REGISTRY_PREFIX}-`)) {
throw new Error(`Prefix is not ${CHECKSUM_REGISTRY_PREFIX}`);
}
Expand Down Expand Up @@ -496,7 +496,7 @@ interface BidAddrData {
export const BidAddrParser = {
fromBytesWithRemainder(
bytes: Uint8Array
): ResultAndRemainder<BidAddr, CLErrorCodes> {
): ResultAndRemainder<KeyBidAddr, CLErrorCodes> {
const tag = bytes[0];
const rem = bytes.subarray(1);

Expand All @@ -507,20 +507,20 @@ export const BidAddrParser = {
const { result, remainder } = accHashParser.fromBytesWithRemainder(rem);

if (result.ok) {
const bidAddr = BidAddr.legacy(result.val);
const bidAddr = KeyBidAddr.legacy(result.val);
return resultHelper(Ok(bidAddr), remainder);
} else {
return resultHelper<BidAddr, CLErrorCodes>(Err(result.val));
return resultHelper<KeyBidAddr, CLErrorCodes>(Err(result.val));
}
}
case BidAddrTag.Validator: {
const { result, remainder } = accHashParser.fromBytesWithRemainder(rem);

if (result.ok) {
const bidAddr = BidAddr.validator(result.val);
const bidAddr = KeyBidAddr.validator(result.val);
return resultHelper(Ok(bidAddr), remainder);
} else {
return resultHelper<BidAddr, CLErrorCodes>(Err(result.val));
return resultHelper<KeyBidAddr, CLErrorCodes>(Err(result.val));
}
}
case BidAddrTag.Delegator: {
Expand All @@ -536,16 +536,16 @@ export const BidAddrParser = {
} = accHashParser.fromBytesWithRemainder(delegatorRem!);

if (delegatorRes.ok) {
const bidAddr = BidAddr.delegator(
const bidAddr = KeyBidAddr.delegator(
validatorRes.val,
delegatorRes.val
);
return resultHelper(Ok(bidAddr), remainder);
} else {
return resultHelper<BidAddr, CLErrorCodes>(Err(delegatorRes.val));
return resultHelper<KeyBidAddr, CLErrorCodes>(Err(delegatorRes.val));
}
} else {
return resultHelper<BidAddr, CLErrorCodes>(Err(validatorRes.val));
return resultHelper<KeyBidAddr, CLErrorCodes>(Err(validatorRes.val));
}
}
case BidAddrTag.Credit: {
Expand All @@ -557,17 +557,17 @@ export const BidAddrParser = {
if (validatorRes.ok) {
const u64Bytes = Uint8Array.from(remainder!.subarray(0, 8));
const eraId = BigNumber.from(u64Bytes.slice().reverse());
const bidAddr = BidAddr.credit(validatorRes.val, eraId);
const bidAddr = KeyBidAddr.credit(validatorRes.val, eraId);
return resultHelper(Ok(bidAddr), remainder);
} else {
return resultHelper<BidAddr, CLErrorCodes>(Err(validatorRes.val));
return resultHelper<KeyBidAddr, CLErrorCodes>(Err(validatorRes.val));
}
}
default:
throw new Error('Unsupported tag while deserializing!');
}
},
toBytes(value: BidAddr): Uint8Array {
toBytes(value: KeyBidAddr): Uint8Array {
const tag = value.tag();

switch (tag) {
Expand All @@ -594,7 +594,7 @@ export const BidAddrParser = {
}
};

export class BidAddr implements CLKeyVariant {
export class KeyBidAddr implements CLKeyVariant {
keyVariant = KeyTag.BidAddr;
prefix = BID_ADDR_PREFIX;

Expand All @@ -607,32 +607,32 @@ export class BidAddr implements CLKeyVariant {
}

static validator(validator: CLAccountHash) {
return new BidAddr({ Validator: validator });
return new KeyBidAddr({ Validator: validator });
}

static delegator(validator: CLAccountHash, delegator: CLAccountHash) {
return new BidAddr({ Delegator: { validator, delegator } });
return new KeyBidAddr({ Delegator: { validator, delegator } });
}

static legacy(validator: CLAccountHash) {
return new BidAddr({ Unified: validator });
return new KeyBidAddr({ Unified: validator });
}

static fromPublicKeys(validator: CLPublicKey, delegator?: CLPublicKey) {
if (delegator) {
return new BidAddr({
return new KeyBidAddr({
Delegator: {
validator: validator.toAccountHashType(),
delegator: delegator.toAccountHashType()
}
});
}

return new BidAddr({ Validator: validator.toAccountHashType() });
return new KeyBidAddr({ Validator: validator.toAccountHashType() });
}

static credit(validator: CLAccountHash, eraId: BigNumberish) {
return new BidAddr({
return new KeyBidAddr({
Credit: {
validator: validator,
eraId
Expand All @@ -657,7 +657,7 @@ export class BidAddr implements CLKeyVariant {
return `${BID_ADDR_PREFIX}-${this.toString()}`;
}

static fromFormattedString(input: string): BidAddr {
static fromFormattedString(input: string): KeyBidAddr {
if (!input.startsWith(`${BID_ADDR_PREFIX}-`)) {
throw new Error(`Prefix is not ${BID_ADDR_PREFIX}`);
}
Expand Down Expand Up @@ -692,7 +692,7 @@ export class KeyPackage implements CLKeyVariant {
return `${PACKAGE_PREFIX}-${this.toString()}`;
}

static fromFormattedString(input: string): DeployHash {
static fromFormattedString(input: string): KeyDeployHash {
if (!input.startsWith(`${PACKAGE_PREFIX}-`)) {
throw new Error(`Prefix is not ${PACKAGE_PREFIX}`);
}
Expand Down

0 comments on commit f9734b1

Please sign in to comment.