adapt latest linearize script from bitcoin