|
1 | | -import { bech32 } from "bech32"; |
| 1 | +import {bech32} from "bech32"; |
| 2 | +import AppError from "../errors/AppError"; |
2 | 3 |
|
3 | 4 | export function convertToHexIfBech32(address: string): string { |
4 | | - if (!address) return '' |
5 | | - if (address.startsWith('stake')|| address.startsWith('drep')){ |
6 | | - const decoded = bech32.decode(address); |
7 | | - const data = bech32.fromWords(decoded.words); |
8 | | - return Buffer.from(data).toString('hex'); |
9 | | - } |
10 | | - else if (address.length === 58 && (address.startsWith('22') || address.startsWith('23'))){ |
11 | | - return address.slice(2) |
12 | | - } |
13 | | - return address |
| 5 | + if (!address) return '' |
| 6 | + if (address.startsWith('stake') || address.startsWith('drep')) { |
| 7 | + const decoded = bech32.decode(address); |
| 8 | + const data = bech32.fromWords(decoded.words); |
| 9 | + return Buffer.from(data).toString('hex'); |
| 10 | + } else if (address.length === 58 && (address.startsWith('22') || address.startsWith('23'))) { |
| 11 | + return address.slice(2) |
| 12 | + } |
| 13 | + return address |
14 | 14 | } |
15 | 15 |
|
16 | | -export function decodeDrep(address:string):{credential:string,isScript?:boolean}{ |
17 | | - const drep = decodeAddress(address); |
18 | | - if(drep.bech32Prefix === 'drep_script'){ |
19 | | - return { |
20 | | - credential: drep.credential.toString('hex'), |
21 | | - isScript: true |
22 | | - } |
23 | | - }else{ |
24 | | - if(drep.dataHeader.length >1){ |
25 | | - throw new Error("Drep credential contains invalid header: "+address) |
26 | | - } |
27 | | - let isScript |
28 | | - const header=drep.dataHeader.at(0) |
29 | | - if(header == 0x22){ |
30 | | - isScript = false |
31 | | - }else if (header == 0x23){ |
32 | | - isScript = true |
33 | | - }else if ( header){ |
34 | | - throw new Error("Drep credential contains invalid header 0x"+ header.toString(16)+ ": "+address) |
| 16 | +export function decodeDrep(address: string): { credential: string, isScript?: boolean } { |
| 17 | + const drep = decodeAddress(address); |
| 18 | + if (drep.bech32Prefix === 'drep_script') { |
| 19 | + return { |
| 20 | + credential: drep.credential.toString('hex'), |
| 21 | + isScript: true |
| 22 | + } |
| 23 | + } else { |
| 24 | + if (drep.dataHeader.length > 1) { |
| 25 | + throw new AppError("Drep credential contains invalid header: " + address) |
| 26 | + } |
| 27 | + let isScript |
| 28 | + const header = drep.dataHeader.at(0) |
| 29 | + if (header == 0x22) { |
| 30 | + isScript = false |
| 31 | + } else if (header == 0x23) { |
| 32 | + isScript = true |
| 33 | + } else if (header) { |
| 34 | + throw new AppError("Drep credential contains invalid header 0x" + header.toString(16) + ": " + address) |
| 35 | + } |
| 36 | + return { |
| 37 | + credential: drep.credential.toString('hex'), |
| 38 | + isScript: isScript |
| 39 | + } |
35 | 40 | } |
36 | | - return { |
37 | | - credential: drep.credential.toString('hex'), |
38 | | - isScript: isScript |
39 | | - } |
40 | | - } |
| 41 | +} |
41 | 42 |
|
| 43 | +export function encodeDrep(hexValue: string, isScript: boolean) { |
| 44 | + try { |
| 45 | + let drepHexVal = hexValue |
| 46 | + if (isScript) { |
| 47 | + drepHexVal = '23' + drepHexVal |
| 48 | + } else { |
| 49 | + drepHexVal = '22' + drepHexVal |
| 50 | + } |
| 51 | + const drepBufferVal = Buffer.from(drepHexVal,'hex') |
| 52 | + const bech32Words = bech32.toWords(drepBufferVal) |
| 53 | + return bech32.encode(isScript ? 'drep_script' : 'drep', bech32Words, 100) |
| 54 | + } catch (e: any) { |
| 55 | + throw new AppError(e?.message || 'Error During DrepViewConversion: ', e) |
| 56 | + } |
42 | 57 | } |
43 | 58 |
|
44 | 59 |
|
45 | 60 | export function decodeAddress(address: string): { bech32Prefix: string, dataHeader: Buffer, credential: Buffer } { |
46 | | - if (!address) return { bech32Prefix: "", dataHeader: Buffer.alloc(0), credential: Buffer.alloc(0) }; // Return empty if address is falsy |
| 61 | + if (!address) return {bech32Prefix: "", dataHeader: Buffer.alloc(0), credential: Buffer.alloc(0)}; // Return empty if address is falsy |
47 | 62 |
|
48 | | - if (isHexValue(address)) { |
49 | | - // Handle the case where the address is hex-encoded (you can tweak this based on your needs) |
50 | | - const buffer = Buffer.from(address, 'hex'); |
51 | | - const dataHeader = buffer.subarray(0, buffer.length - 28); |
52 | | - const credential = buffer.subarray(buffer.length - 28); |
53 | | - return { bech32Prefix: "hex", dataHeader, credential }; |
54 | | - } else { |
55 | | - try { |
56 | | - // Decode the Bech32 address |
57 | | - const decoded = bech32.decode(address); |
58 | | - const data = bech32.fromWords(decoded.words); |
59 | | - const buffer = Buffer.from(data); |
| 63 | + if (isHexValue(address)) { |
| 64 | + // Handle the case where the address is hex-encoded (you can tweak this based on your needs) |
| 65 | + const buffer = Buffer.from(address, 'hex'); |
| 66 | + const dataHeader = buffer.subarray(0, buffer.length - 28); |
| 67 | + const credential = buffer.subarray(buffer.length - 28); |
| 68 | + return {bech32Prefix: "hex", dataHeader, credential}; |
| 69 | + } else { |
| 70 | + try { |
| 71 | + // Decode the Bech32 address |
| 72 | + const decoded = bech32.decode(address); |
| 73 | + const data = bech32.fromWords(decoded.words); |
| 74 | + const buffer = Buffer.from(data); |
60 | 75 |
|
61 | | - // Split the buffer into header and credential (last 28 bytes for credential) |
62 | | - const dataHeader = buffer.subarray(0, buffer.length - 28); |
63 | | - const credential = buffer.subarray(buffer.length - 28); |
| 76 | + // Split the buffer into header and credential (last 28 bytes for credential) |
| 77 | + const dataHeader = buffer.subarray(0, buffer.length - 28); |
| 78 | + const credential = buffer.subarray(buffer.length - 28); |
64 | 79 |
|
65 | | - return { |
66 | | - bech32Prefix: decoded.prefix, // Extract prefix from the Bech32 decoding result |
67 | | - dataHeader, |
68 | | - credential |
69 | | - }; |
| 80 | + return { |
| 81 | + bech32Prefix: decoded.prefix, // Extract prefix from the Bech32 decoding result |
| 82 | + dataHeader, |
| 83 | + credential |
| 84 | + }; |
70 | 85 |
|
71 | | - } catch (e: any) { |
72 | | - throw new Error("Data is not hex or bech32: " + address); |
| 86 | + } catch (e: any) { |
| 87 | + throw new AppError("Data is not hex or bech32: " + address); |
| 88 | + } |
73 | 89 | } |
74 | | - } |
75 | 90 | } |
76 | 91 |
|
77 | 92 |
|
78 | | -export function isHexValue(value:string):boolean{ |
79 | | - const hexRegex = /^(?:[0-9a-fA-F]{56}|[0-9a-fA-F]{58})$/; |
80 | | - return hexRegex.test(value); |
| 93 | +export function isHexValue(value: string): boolean { |
| 94 | + const hexRegex = /^(?:[0-9a-fA-F]{56}|[0-9a-fA-F]{58})$/; |
| 95 | + return hexRegex.test(value); |
81 | 96 | } |
82 | 97 |
|
83 | | -export function validateHash(value:string) { |
84 | | - const regex = /^[a-f0-9A-F]{64}$/ |
85 | | - if (value.includes('#')){ |
86 | | - return regex.test(value.slice(0,-2)) |
87 | | - }else return regex.test(value) |
| 98 | +export function validateHash(value: string) { |
| 99 | + const regex = /^[a-f0-9A-F]{64}$/ |
| 100 | + if (value.includes('#')) { |
| 101 | + return regex.test(value.slice(0, -2)) |
| 102 | + } else return regex.test(value) |
88 | 103 | } |
89 | 104 |
|
90 | 105 | export function fromHex(prefix: string, hex: string) { |
91 | | - return bech32.encode(prefix, bech32.toWords(Buffer.from(hex, "hex"))); |
| 106 | + return bech32.encode(prefix, bech32.toWords(Buffer.from(hex, "hex"))); |
92 | 107 | } |
93 | 108 |
|
94 | 109 | export function validateAddress(value: string): boolean { |
95 | | - if (isHexValue(value)){ |
96 | | - return value.length === 56 || value.length === 58 |
97 | | - } |
98 | | - return false |
| 110 | + if (isHexValue(value)) { |
| 111 | + return value.length === 56 || value.length === 58 |
| 112 | + } |
| 113 | + return false |
99 | 114 | } |
0 commit comments