(str: string)
| 71 | } |
| 72 | |
| 73 | function bech32Decode(str: string): { prefix: string; words: number[] } { |
| 74 | const lower = str.toLowerCase() |
| 75 | const split = lower.lastIndexOf('1') |
| 76 | if (split < 1 || split + 7 > str.length) { throw new Error(`Invalid bech32: ${str}`) } |
| 77 | const prefix = lower.slice(0, split) |
| 78 | const wordChars = lower.slice(split + 1) |
| 79 | let chk = bech32PrefixChk(prefix) |
| 80 | const words: number[] = [] |
| 81 | for (let i = 0; i < wordChars.length; ++i) { |
| 82 | const v = BECH32_ALPHABET_MAP[wordChars[i]] |
| 83 | if (v === undefined) { throw new Error(`Unknown bech32 character: ${wordChars[i]}`) } |
| 84 | chk = bech32PolymodStep(chk) ^ v |
| 85 | if (i + 6 < wordChars.length) { words.push(v) } |
| 86 | } |
| 87 | if (chk !== 1) { throw new Error('Invalid bech32 checksum') } |
| 88 | return { prefix, words } |
| 89 | } |
| 90 | |
| 91 | function bech32Encode(prefix: string, words: number[]): string { |
| 92 | prefix = prefix.toLowerCase() |
no test coverage detected