@blockfrost/blockfrost-utils
Advanced tools
Comparing version 2.3.1 to 2.3.2
@@ -13,7 +13,8 @@ "use strict"; | ||
paymentCred, options) => { | ||
// Constructs invalid bech32 address consisting only from an address header and a payment part. | ||
// Constructs invalid bech32 address consisting only of an address header and a payment part. | ||
// Useful for matching payment credentials against bech32 address | ||
const NETWORK_TAG = options.network === 'mainnet' ? 1 : 0; | ||
const paymentCredHexWithBytesPrefix = (0, validation_1.paymentCredFromBech32Address)(paymentCred); | ||
const paymentCredHex = paymentCredHexWithBytesPrefix === null || paymentCredHexWithBytesPrefix === void 0 ? void 0 : paymentCredHexWithBytesPrefix.slice(2); | ||
// slice off \\x prefix | ||
const paymentCredHex = paymentCredHexWithBytesPrefix === null || paymentCredHexWithBytesPrefix === void 0 ? void 0 : paymentCredHexWithBytesPrefix.paymentCred.slice(2); | ||
if (!paymentCredHex) | ||
@@ -20,0 +21,0 @@ return null; |
@@ -129,3 +129,3 @@ "use strict"; | ||
// error.name is always "FastifyError", use error.code to provide more info to a client (eg. FST_ERR_CTP_INVALID_MEDIA_TYPE) | ||
error: error.name, | ||
error: error.name === 'FastifyError' ? 'error' : error.name, | ||
message: error.message, | ||
@@ -132,0 +132,0 @@ status_code: error.statusCode, |
declare type BlockfrostNetwork = 'mainnet' | 'testnet' | 'preview' | 'preprod'; | ||
declare type SUPPORTED_PAYMENT_CRED_PREFIX = 'addr_vkh' | 'addr_vk' | 'script'; | ||
export declare const validateHex: (input: string) => boolean; | ||
@@ -6,8 +7,16 @@ export declare const validateStakeAddress: (input: string, network: BlockfrostNetwork) => boolean; | ||
export declare const validateAndConvertPool: (input: string) => string | undefined; | ||
export declare const paymentCredFromBech32Address: (input: string) => string | undefined; | ||
export declare const paymentCredToBech32Address: (input: string) => string | undefined; | ||
export declare const paymentCredFromBech32Address: (input: string) => { | ||
paymentCred: string; | ||
prefix: SUPPORTED_PAYMENT_CRED_PREFIX; | ||
} | undefined; | ||
export declare const paymentCredToBech32Address: (input: string, prefix: SUPPORTED_PAYMENT_CRED_PREFIX) => string | undefined; | ||
export declare const detectAndValidateAddressType: (input: string, network: BlockfrostNetwork) => 'byron' | 'shelley' | undefined; | ||
export declare const getAddressTypeAndPaymentCred: (address: string, network: BlockfrostNetwork) => { | ||
addressType: "byron" | "shelley" | undefined; | ||
paymentCred: string | undefined; | ||
paymentCred: string; | ||
paymentCredPrefix: SUPPORTED_PAYMENT_CRED_PREFIX; | ||
} | { | ||
addressType: "byron" | "shelley" | undefined; | ||
paymentCred?: undefined; | ||
paymentCredPrefix?: undefined; | ||
}; | ||
@@ -14,0 +23,0 @@ export declare const scriptHashFromBech32Address: (input: string) => string | undefined; |
@@ -15,2 +15,3 @@ "use strict"; | ||
POOL: 'pool', | ||
SCRIPT: 'script', | ||
}); | ||
@@ -88,3 +89,3 @@ const MAX_UNSIGNED_INT = 2147483648; | ||
const paymentCred = `\\x${Buffer.from(payload).toString('hex')}`; | ||
return paymentCred; | ||
return { paymentCred, prefix: bech32Info.prefix }; | ||
} | ||
@@ -97,4 +98,10 @@ else if (bech32Info.prefix === Prefixes.PAYMENT_KEY) { | ||
pubKey.free(); | ||
return paymentKeyHash; | ||
return { paymentCred: paymentKeyHash, prefix: bech32Info.prefix }; | ||
} | ||
else if (bech32Info.prefix === Prefixes.SCRIPT) { | ||
const payload = bech32_1.bech32.fromWords(bech32Info.words); | ||
const payloadHex = Buffer.from(payload).toString('hex'); | ||
const paymentCred = `\\x${payloadHex}`; | ||
return { paymentCred, prefix: bech32Info.prefix }; | ||
} | ||
else { | ||
@@ -111,4 +118,4 @@ return undefined; | ||
exports.paymentCredFromBech32Address = paymentCredFromBech32Address; | ||
const paymentCredToBech32Address = (input) => { | ||
// compute paymentCred | ||
const paymentCredToBech32Address = (input, prefix) => { | ||
// Encodes payment credential into its original bech32 prefixed form | ||
try { | ||
@@ -118,4 +125,15 @@ if (!(0, exports.validateHex)(input)) | ||
const words = bech32_1.bech32.toWords(Buffer.from(input, 'hex')); | ||
// if it's in hex, we'll convert it to Bech32 | ||
return bech32_1.bech32.encode(Prefixes.PAYMENT_KEY_HASH, words); | ||
switch (prefix) { | ||
case Prefixes.PAYMENT_KEY_HASH: | ||
case Prefixes.SCRIPT: | ||
// add prefix to payment cred and encode it as bech32 | ||
return bech32_1.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_1.bech32.encode(Prefixes.PAYMENT_KEY_HASH, words); | ||
default: | ||
throw Error(`Prefix ${prefix} is not supported by paymentCredToBech32Address`); | ||
} | ||
} | ||
@@ -143,3 +161,3 @@ catch { | ||
else if (bech32Info.prefix === Prefixes.PAYMENT_KEY_HASH || | ||
bech32Info.prefix === Prefixes.PAYMENT_KEY) { | ||
bech32Info.prefix === Prefixes.SCRIPT) { | ||
// valid shelley - payment_cred | ||
@@ -167,3 +185,14 @@ return 'shelley'; | ||
: undefined; | ||
return { addressType, paymentCred }; | ||
if (paymentCred) { | ||
return { | ||
addressType, | ||
paymentCred: paymentCred.paymentCred, | ||
paymentCredPrefix: paymentCred.prefix, | ||
}; | ||
} | ||
else { | ||
return { | ||
addressType, | ||
}; | ||
} | ||
}; | ||
@@ -170,0 +199,0 @@ exports.getAddressTypeAndPaymentCred = getAddressTypeAndPaymentCred; |
{ | ||
"name": "@blockfrost/blockfrost-utils", | ||
"version": "2.3.1", | ||
"version": "2.3.2", | ||
"repository": "git@github.com:blockfrost/blockfrost-utils.git", | ||
@@ -30,3 +30,2 @@ "license": "Apache-2.0", | ||
"@types/node": "^16", | ||
"@types/utf-8-validate": "^5.0.0", | ||
"@typescript-eslint/eslint-plugin": "^5.30.0", | ||
@@ -33,0 +32,0 @@ "@typescript-eslint/parser": "5.27.0", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
63066
14
1145