Skip to content

Commit

Permalink
refactor: renamed convention
Browse files Browse the repository at this point in the history
  • Loading branch information
hoffmannjan committed Jun 9, 2024
1 parent 59e4cbf commit 4dd6f3c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 22 deletions.
8 changes: 4 additions & 4 deletions src/lib/CLValue/AccountHash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,16 @@ export class CLAccountHash extends CLValue implements CLKeyVariant {
return this.data;
}

toStr(): string {
toString(): string {
const bytes = this.data;
return Buffer.from(bytes).toString('hex');
}

toFormattedStr(): string {
return `${ACCOUNT_HASH_PREFIX}-${this.toStr()}`;
toFormattedString(): string {
return `${ACCOUNT_HASH_PREFIX}-${this.toString()}`;
}

static fromFormattedStr(hexStr: string): CLAccountHash {
static fromFormattedString(hexStr: string): CLAccountHash {
if (hexStr.startsWith(`${ACCOUNT_HASH_PREFIX}-`)) {
const formatedString = hexStr.replace(`${ACCOUNT_HASH_PREFIX}-`, '');
const bytes = Uint8Array.from(Buffer.from(formatedString, 'hex'));
Expand Down
49 changes: 36 additions & 13 deletions src/lib/CLValue/KeyVariants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {
KeyTag,
ResultAndRemainder,
resultHelper,
CLErrorCodes
CLErrorCodes,
CLValueBytesParsers
} from './index';
import { decodeBase16, encodeBase16 } from '../Conversions';

Expand All @@ -12,7 +13,6 @@ import { decodeBase16, encodeBase16 } from '../Conversions';
// - internal type creator
// - stores data
// - serialize when inside a Key
// - normally do not serialize as normal CLValue eg. using CLValueParsers.fromBytes
export abstract class CLKeyVariant {
abstract keyVariant: KeyTag;
abstract data: Uint8Array;
Expand All @@ -21,9 +21,11 @@ export abstract class CLKeyVariant {
return this.data;
}

abstract toStr(): string;
abstract toFormattedStr(): string;
static fromFormattedStr(hexStr: string): CLKeyVariant {
abstract toString(): string;

abstract toFormattedString(): string;

static fromFormattedString(hexStr: string): CLKeyVariant {
throw Error(
`Trying to deserialize KeyVariant - unknown string provided: ${hexStr}`
);
Expand All @@ -32,7 +34,7 @@ export abstract class CLKeyVariant {

export const HASH_PREFIX = 'hash';
export const TRANSFER_PREFIX = 'transfer';
export const DEPLOY_INFO_PREFIX = '
export const DEPLOY_HASH_PREFIX = 'deploy-hash';

const KEY_HASH_LENGTH = 32;

Expand All @@ -55,12 +57,12 @@ export class Hash implements CLKeyVariant {
return this.data;
}

toStr() {
toString() {
return encodeBase16(this.data);
}

toFormattedStr() {
return `${HASH_PREFIX}-${this.toStr()}`;
toFormattedString() {
return `${HASH_PREFIX}-${this.toString()}`;
}

static fromFormattedStr(input: string): Hash {
Expand All @@ -85,12 +87,12 @@ export class TransferAddr implements CLKeyVariant {
return this.data;
}

toStr() {
toString() {
return encodeBase16(this.data);
}

toFormattedStr() {
return `${TRANSFER_PREFIX}-${this.toStr()}`;
toFormattedString() {
return `${TRANSFER_PREFIX}-${this.toString()}`;
}

static fromFormattedStr(input: string): Hash {
Expand All @@ -107,11 +109,32 @@ export class TransferAddr implements CLKeyVariant {

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

constructor(public data: Uint8Array) {}

value(): any {
return this.data;
}

toString() {
return encodeBase16(this.data);
}

toFormattedString() {
return `${DEPLOY_HASH_PREFIX}-${this.toString()}`;
}

static fromFormattedStr(input: string): DeployHash {
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);
}
}


8 changes: 4 additions & 4 deletions src/lib/CLValue/URef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,19 @@ export class CLURef extends CLValue implements CLKeyVariant {
return new CLURef(addr, accessRight);
}

toStr(): string {
toString(): string {
return [
encodeBase16(this.data),
padNum(this.accessRights.toString(8), 3)
].join('-');
}

toFormattedStr(): string {
return [FORMATTED_STRING_PREFIX, this.toStr()].join('-');
toFormattedString(): string {
return [FORMATTED_STRING_PREFIX, this.toString()].join('-');
}

toJSON(): string {
return this.toFormattedStr();
return this.toFormattedString();
}

clType(): CLType {
Expand Down
2 changes: 1 addition & 1 deletion src/services/CasperServiceByJsonRPC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ export class CasperServiceByJsonRPC {
if (accountIdentifier instanceof CLPublicKey) {
identifier = accountIdentifier.toHex();
} else if (accountIdentifier instanceof CLAccountHash) {
identifier = accountIdentifier.toFormattedStr();
identifier = accountIdentifier.toFormattedString();
}
const params: any = {
account_identifier: identifier
Expand Down

0 comments on commit 4dd6f3c

Please sign in to comment.