Skip to content

Commit

Permalink
refactor: addressed comments of naming for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
hui-an-yang committed Jan 3, 2024
1 parent da9b6eb commit 40b1ec7
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 42 deletions.
28 changes: 14 additions & 14 deletions packages/taquito-local-forging/src/codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const prefixDecoder = (pre: Prefix) => (str: Uint8ArrayConsumer) => {

export const tz1Decoder = prefixDecoder(Prefix.TZ1);
export const branchDecoder = prefixDecoder(Prefix.B);
export const pkhDecoder = (val: Uint8ArrayConsumer) => {
export const publicKeyHashDecoder = (val: Uint8ArrayConsumer) => {
const prefix = val.consume(1);

if (prefix[0] === 0x00) {
Expand All @@ -57,16 +57,16 @@ export const pkhDecoder = (val: Uint8ArrayConsumer) => {
}
};

export const pkhsDecoder = (val: Uint8ArrayConsumer) => {
export const publicKeyHashesDecoder = (val: Uint8ArrayConsumer) => {
if (!boolDecoder(val)) {
return undefined;
}
const pkhs = [];
const publicKeyHashes = [];
val.consume(4);
while (val.length() > 0) {
pkhs.push(pkhDecoder(val));
publicKeyHashes.push(publicKeyHashDecoder(val));
}
return pkhs;
return publicKeyHashes;
};

export const branchEncoder = prefixEncoder(Prefix.B);
Expand Down Expand Up @@ -151,7 +151,7 @@ export const pvmKindDecoder = (pvm: Uint8ArrayConsumer): string => {

export const delegateEncoder = (val: string) => {
if (val) {
return boolEncoder(true) + pkhEncoder(val);
return boolEncoder(true) + publicKeyHashEncoder(val);
} else {
return boolEncoder(false);
}
Expand Down Expand Up @@ -205,11 +205,11 @@ export const boolDecoder = (val: Uint8ArrayConsumer): boolean => {
export const delegateDecoder = (val: Uint8ArrayConsumer) => {
const hasDelegate = boolDecoder(val);
if (hasDelegate) {
return pkhDecoder(val);
return publicKeyHashDecoder(val);
}
};

export const pkhEncoder = (val: string) => {
export const publicKeyHashEncoder = (val: string) => {
const pubkeyPrefix = val.substring(0, 3);
switch (pubkeyPrefix) {
case Prefix.TZ1:
Expand All @@ -229,17 +229,17 @@ export const pkhEncoder = (val: string) => {
}
};

export const pkhsEncoder = (val?: string[]) => {
export const publicKeyHashesEncoder = (val?: string[]) => {
if (!val) {
return boolEncoder(false);
}
if (val.length === 0) {
return boolEncoder(true) + pad(0);
}
const pkhs = val.reduce((prev, curr) => {
return prev + pkhEncoder(curr);
const publicKeyHashes = val.reduce((prev, curr) => {
return prev + publicKeyHashEncoder(curr);
}, '');
return boolEncoder(true) + pad(pkhs.length / 2) + pkhs;
return boolEncoder(true) + pad(publicKeyHashes.length / 2) + publicKeyHashes;
};

export const publicKeyEncoder = (val: string) => {
Expand Down Expand Up @@ -267,7 +267,7 @@ export const addressEncoder = (val: string): string => {
case Prefix.TZ2:
case Prefix.TZ3:
case Prefix.TZ4:
return '00' + pkhEncoder(val);
return '00' + publicKeyHashEncoder(val);
case Prefix.KT1:
return '01' + prefixEncoder(Prefix.KT1)(val) + '00';
default:
Expand Down Expand Up @@ -333,7 +333,7 @@ export const addressDecoder = (val: Uint8ArrayConsumer) => {
const preamble = val.consume(1);
switch (preamble[0]) {
case 0x00:
return pkhDecoder(val);
return publicKeyHashDecoder(val);
case 0x01: {
const address = prefixDecoder(Prefix.KT1)(val);
val.consume(1);
Expand Down
8 changes: 4 additions & 4 deletions packages/taquito-local-forging/src/decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
int32Decoder,
paddedBytesDecoder,
parametersDecoder,
pkhDecoder,
pkhsDecoder,
publicKeyHashDecoder,
publicKeyHashesDecoder,
smartRollupMessageDecoder,
proposalDecoder,
proposalsDecoder,
Expand Down Expand Up @@ -63,8 +63,8 @@ export const decoders: { [key: string]: Decoder } = {
[CODEC.BRANCH]: branchDecoder,
[CODEC.ZARITH]: zarithDecoder,
[CODEC.PUBLIC_KEY]: publicKeyDecoder,
[CODEC.PKH]: pkhDecoder,
[CODEC.PKH_ARR]: pkhsDecoder,
[CODEC.PKH]: publicKeyHashDecoder,
[CODEC.PKH_ARR]: publicKeyHashesDecoder,
[CODEC.DELEGATE]: delegateDecoder,
[CODEC.INT32]: int32Decoder,
[CODEC.SCRIPT]: scriptDecoder,
Expand Down
8 changes: 4 additions & 4 deletions packages/taquito-local-forging/src/encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
int32Encoder,
paddedBytesEncoder,
parametersEncoder,
pkhEncoder,
pkhsEncoder,
publicKeyHashEncoder,
publicKeyHashesEncoder,
smartRollupMessageEncoder,
proposalEncoder,
proposalsEncoder,
Expand Down Expand Up @@ -61,8 +61,8 @@ export const encoders: { [key: string]: Encoder<any> } = {
[CODEC.BRANCH]: branchEncoder,
[CODEC.ZARITH]: zarithEncoder,
[CODEC.PUBLIC_KEY]: publicKeyEncoder,
[CODEC.PKH]: pkhEncoder,
[CODEC.PKH_ARR]: pkhsEncoder,
[CODEC.PKH]: publicKeyHashEncoder,
[CODEC.PKH_ARR]: publicKeyHashesEncoder,
[CODEC.DELEGATE]: delegateEncoder,
[CODEC.SCRIPT]: scriptEncoder,
[CODEC.BALLOT_STATEMENT]: ballotEncoder,
Expand Down
42 changes: 22 additions & 20 deletions packages/taquito-local-forging/test/codec.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import {
} from '../src/codec';
import { Uint8ArrayConsumer } from '../src/uint8array-consumer';
import {
pkhEncoder,
pkhsEncoder,
pkhsDecoder,
publicKeyHashEncoder,
publicKeyHashesEncoder,
publicKeyHashesDecoder,
publicKeyDecoder,
publicKeyEncoder,
} from '../src/codec';
Expand Down Expand Up @@ -65,29 +65,31 @@ describe('Tests for Entrypoint functions and for encode and decoder error messag
);
});

test(`Verify pkhEncoder`, async () => {
const tz1 = pkhEncoder('tz1e42w8ZaGAbM3gucbBy8iRypdbnqUj7oWY');
test(`Verify publicKeyHashEncoder`, async () => {
const tz1 = publicKeyHashEncoder('tz1e42w8ZaGAbM3gucbBy8iRypdbnqUj7oWY');
expect(tz1).toEqual('00c9fc72e8491bd2973e196f04ec6918ad5bcee22d');
const tz2 = pkhEncoder('tz2Ch1abG7FNiibmV26Uzgdsnfni9XGrk5wD');
const tz2 = publicKeyHashEncoder('tz2Ch1abG7FNiibmV26Uzgdsnfni9XGrk5wD');
expect(tz2).toEqual('012ffebbf1560632ca767bc960ccdb84669d284c2c');
const tz3 = pkhEncoder('tz3WXYtyDUNL91qfiCJtVUX746QpNv5i5ve5');
const tz3 = publicKeyHashEncoder('tz3WXYtyDUNL91qfiCJtVUX746QpNv5i5ve5');
expect(tz3).toEqual('026fde46af0356a0476dae4e4600172dc9309b3aa4');
const tz4 = pkhEncoder('tz4HQ8VeXAyrZMhES1qLMJAc9uAVXjbMpS8u');
const tz4 = publicKeyHashEncoder('tz4HQ8VeXAyrZMhES1qLMJAc9uAVXjbMpS8u');
expect(tz4).toEqual('035c14a7a05c10fc8b402fbcdd48dc8136236bf3c1');
expect(() => pkhEncoder('tz5WXYtyDUNL91qfiCJtVUX746QpNv5i5ve5')).toThrow(InvalidKeyHashError);
expect(() => publicKeyHashEncoder('tz5WXYtyDUNL91qfiCJtVUX746QpNv5i5ve5')).toThrow(
InvalidKeyHashError
);
try {
pkhEncoder('tz5WXYtyDUNL91qfiCJtVUX746QpNv5i5ve5');
publicKeyHashEncoder('tz5WXYtyDUNL91qfiCJtVUX746QpNv5i5ve5');
} catch (e) {
expect(e.message).toContain(`Invalid public key hash "tz5WXYtyDUNL91qfiCJtVUX746QpNv5i5ve5"`);
}
});

test(`Verify pkhsEncoder`, async () => {
const none = pkhsEncoder();
test(`Verify publicKeyHashesEncoder`, async () => {
const none = publicKeyHashesEncoder();
expect(none).toEqual('00');
const empty = pkhsEncoder([]);
const empty = publicKeyHashesEncoder([]);
expect(empty).toEqual('ff00000000');
const tz = pkhsEncoder([
const tz = publicKeyHashesEncoder([
'tz1e42w8ZaGAbM3gucbBy8iRypdbnqUj7oWY',
'tz2Ch1abG7FNiibmV26Uzgdsnfni9XGrk5wD',
'tz3WXYtyDUNL91qfiCJtVUX746QpNv5i5ve5',
Expand All @@ -97,22 +99,22 @@ describe('Tests for Entrypoint functions and for encode and decoder error messag
'ff0000005400c9fc72e8491bd2973e196f04ec6918ad5bcee22d012ffebbf1560632ca767bc960ccdb84669d284c2c026fde46af0356a0476dae4e4600172dc9309b3aa4035c14a7a05c10fc8b402fbcdd48dc8136236bf3c1'
);

expect(() => pkhsEncoder(['tz5WXYtyDUNL91qfiCJtVUX746QpNv5i5ve5'])).toThrow(
expect(() => publicKeyHashesEncoder(['tz5WXYtyDUNL91qfiCJtVUX746QpNv5i5ve5'])).toThrow(
InvalidKeyHashError
);
try {
pkhsEncoder(['tz5WXYtyDUNL91qfiCJtVUX746QpNv5i5ve5']);
publicKeyHashesEncoder(['tz5WXYtyDUNL91qfiCJtVUX746QpNv5i5ve5']);
} catch (e) {
expect(e.message).toContain(`Invalid public key hash "tz5WXYtyDUNL91qfiCJtVUX746QpNv5i5ve5"`);
}
});

test(`Verify pkhsDecoder`, async () => {
const none = pkhsDecoder(Uint8ArrayConsumer.fromHexString('00'));
test(`Verify publicKeyHashesDecoder`, async () => {
const none = publicKeyHashesDecoder(Uint8ArrayConsumer.fromHexString('00'));
expect(none).toEqual(undefined);
const empty = pkhsDecoder(Uint8ArrayConsumer.fromHexString('ff00000000'));
const empty = publicKeyHashesDecoder(Uint8ArrayConsumer.fromHexString('ff00000000'));
expect(empty).toEqual([]);
const tz = pkhsDecoder(
const tz = publicKeyHashesDecoder(
Uint8ArrayConsumer.fromHexString(
'ff0000005400c9fc72e8491bd2973e196f04ec6918ad5bcee22d012ffebbf1560632ca767bc960ccdb84669d284c2c026fde46af0356a0476dae4e4600172dc9309b3aa4'
)
Expand Down

0 comments on commit 40b1ec7

Please sign in to comment.