Skip to content

Commit

Permalink
Merge pull request #47 from blockfrost/chore/cleanup-paymentcred
Browse files Browse the repository at this point in the history
chore: remove addr_vk from paymentCredToBech32Address
  • Loading branch information
1000101 authored Mar 15, 2023
2 parents 06f7261 + 4096469 commit 1f927b5
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 18 deletions.
11 changes: 3 additions & 8 deletions src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const Prefixes = Object.freeze({
SCRIPT: 'script',
});

type SUPPORTED_PAYMENT_CRED_PREFIX = 'addr_vkh' | 'addr_vk' | 'script';
type PaymentCredPrefix = 'addr_vkh' | 'addr_vk' | 'script';

const MAX_UNSIGNED_INT = 2_147_483_648;
const MAX_SIGNED_INT = 2_147_483_647;
Expand Down Expand Up @@ -96,7 +96,7 @@ export const paymentCredFromBech32Address = (
):
| {
paymentCred: string;
prefix: SUPPORTED_PAYMENT_CRED_PREFIX;
prefix: PaymentCredPrefix;
}
| undefined => {
// compute paymentCred
Expand Down Expand Up @@ -133,7 +133,7 @@ export const paymentCredFromBech32Address = (

export const paymentCredToBech32Address = (
input: string,
prefix: SUPPORTED_PAYMENT_CRED_PREFIX,
prefix: PaymentCredPrefix,
): string | undefined => {
// Encodes payment credential into its original bech32 prefixed form
try {
Expand All @@ -146,11 +146,6 @@ export const paymentCredToBech32Address = (
case Prefixes.SCRIPT:
// add prefix to payment cred and encode it as bech32
return bech32.encode(prefix, words);
case Prefixes.PAYMENT_KEY:
// Payment key was already converted to key hash, so we cannot restore the original key.
// We could supply orig via a parameter and then compare its hash to the input, but that's too much trouble.
// Due to the above return payment key hash (input) with addr_vkh prefix instead of the original addr_vk
return bech32.encode(Prefixes.PAYMENT_KEY_HASH, words);
default:
throw Error(
`Prefix ${prefix} is not supported by paymentCredToBech32Address`,
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/cardano.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const getPaymentPartBech32 = [
},
{
// https://cips.cardano.org/cips/cip19/#testvectors
// converted to PKH: addr_vkh1jjfnzhxe966a33psfenm0ct2udkkr569qf55v4uprgkgu8zsvmg
description: 'addr_vk addr type 0',
paymentCred:
'addr_vk1w0l2sr2zgfm26ztc6nl9xy8ghsk5sh6ldwemlpmp9xylzy4dtf7st80zhd',
Expand Down
10 changes: 0 additions & 10 deletions test/fixtures/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +162,6 @@ export const paymentCredToBech32Address = [
prefix: 'script',
result: 'script1cda3khwqv60360rp5m7akt50m6ttapacs8rqhn5w342z7r35m37',
},
{
// TODO: With addr_vk prefix we assume payment key was already converted
// to the key hash, so we cannot restore the original key.
// Due to the above the function returns payment key hash (input) with addr_vkh prefix instead of the original addr_vk.
// Alternatively, we could just drop support for addr_vk.
description: 'Valid payment key from addr_vk gets converted to addr_vkh',
input: 'c37b1b5dc0669f1d3c61a6fddb2e8fde96be87b881c60bce8e8d542f',
prefix: 'addr_vk',
result: 'addr_vkh1cda3khwqv60360rp5m7akt50m6ttapacs8rqhn5w342z7lydx4l',
},
{
description: 'Valid script address',
input: '59a38a122f5278190f6f34230b7376eb8bebabf92f87240d2271e012',
Expand Down

0 comments on commit 1f927b5

Please sign in to comment.