MCPcopy Create free account
hub / github.com/cameri/nostream / bech32Decode

Function bech32Decode

src/utils/transform.ts:73–89  ·  view source on GitHub ↗
(str: string)

Source from the content-addressed store, hash-verified

71}
72
73function 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
91function bech32Encode(prefix: string, words: number[]): string {
92 prefix = prefix.toLowerCase()

Callers 1

fromBech32Function · 0.85

Calls 2

bech32PrefixChkFunction · 0.85
bech32PolymodStepFunction · 0.85

Tested by

no test coverage detected