This software is not audited, use at your own risk.
Bech32 Noir provides functions for decoding the locking script components (P2WSH, P2WPKH) from bitcoin segwit addresses in Noir Circuits.
At the moment, this library only supports decoding.
In your Nargo.toml
file, add the following dependency:
[dependencies]
bech32 = { tag = "v0.1.0", git = "https://github.com/alpinevm/bech32-noir" }
use dep::bech32;
fn main(
address: str<42>, // example address: bc1q3x5ecspn9jjp9ah33000lcnrx35s0kq7f8tyan
pub_key_hash: [u8; 20] // example hash: 89a99c40332ca412f6f18bdeffe263346907d81e (encoded as array of u8s)
) -> pub Field {
let address_bvec: BoundedVec<u8, 90> = bech32::str_to_bvec(address);
let witness = bech32::segwit_decode(address_bvec);
// hardcoded version '0' given the above address
assert(witness == bech32::witness_builder(0, pub_key_hash));
}
use dep::bech32;
fn main() {
let data: BoundedVec<u8, 90> = bech32::str_to_bvec("bc1qsktu2fffyuwkmxk85z4ad9mq9hhldqqcnrzmzs");
let witness_program = bech32::segwit_decode(data);
}
% nargo info
+-------------+----------------------+--------------+----------------------+
| Package | Expression Width | ACIR Opcodes | Backend Circuit Size |
+-------------+----------------------+--------------+----------------------+
| bech32_test | Bounded { width: 3 } | 22405 | 27126 |
+-------------+----------------------+--------------+----------------------+