| 1 | package Bitcoin::Crypto::Types; | ||||||||||
| 2 | 73 | $Bitcoin::Crypto::Types::VERSION = '4.006'; | |||||||||
| 3 | 73 | ▶ | use v5.14; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 73 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 73 | ▶ | use Type::Library -base; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 73 | ▶ | use Type::Coercion; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 73 | ▶ | use Types::Common -types; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 73 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | # make sure Math::BigInt is properly loaded - this module loads it | ||||||||||
| 12 | 73 | ▶ | use Bitcoin::Crypto::Helpers qw(die_no_trace); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 73 | ▶ | use Bitcoin::Crypto::Constants qw(:bip44 :psbt USE_BIGINTS); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | 73 | our $CHECK_BYTESTRINGS = !!1; | |||||||||
| 16 | |||||||||||
| 17 | 73 | __PACKAGE__->add_type( | |||||||||
| 18 | name => 'BIP44Purpose', | ||||||||||
| 19 | parent => Maybe [ | ||||||||||
| 20 | Enum->of( | ||||||||||
| 21 | BIP44_PURPOSE, | ||||||||||
| 22 | BIP44_COMPAT_PURPOSE, | ||||||||||
| 23 | BIP44_SEGWIT_PURPOSE, | ||||||||||
| 24 | BIP44_TAPROOT_PURPOSE, | ||||||||||
| 25 | ) | ||||||||||
| 26 | ], | ||||||||||
| 27 | ); | ||||||||||
| 28 | |||||||||||
| 29 | 73 | my $formatstr = __PACKAGE__->add_type( | |||||||||
| 30 | name => 'FormatStr', | ||||||||||
| 31 | parent => Enum->of( | ||||||||||
| 32 | 'bytes', | ||||||||||
| 33 | 'hex', | ||||||||||
| 34 | 'base58', | ||||||||||
| 35 | 'base64', | ||||||||||
| 36 | ) | ||||||||||
| 37 | ); | ||||||||||
| 38 | |||||||||||
| 39 | 73 | my $formatdesc = __PACKAGE__->add_type( | |||||||||
| 40 | name => 'FormatDesc', | ||||||||||
| 41 | parent => Tuple->of( | ||||||||||
| 42 | $formatstr, | ||||||||||
| 43 | Str, | ||||||||||
| 44 | ) | ||||||||||
| 45 | ); | ||||||||||
| 46 | |||||||||||
| 47 | my $bytestr = __PACKAGE__->add_type( | ||||||||||
| 48 | name => 'ByteStr', | ||||||||||
| 49 | parent => Str, | ||||||||||
| 50 | |||||||||||
| 51 | constraint => q{ $Bitcoin::Crypto::Types::CHECK_BYTESTRINGS ? utf8::downgrade($_, 1) : !!1 }, | ||||||||||
| 52 | |||||||||||
| 53 | 0 | ▶ | inline => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 54 | 0 | my $varname = pop; | |||||||||
| 55 | |||||||||||
| 56 | return ( | ||||||||||
| 57 | undef, | ||||||||||
| 58 | 0 | qq{ \$Bitcoin::Crypto::Types::CHECK_BYTESTRINGS ? utf8::downgrade($varname, 1) : !!1 } | |||||||||
| 59 | ); | ||||||||||
| 60 | }, | ||||||||||
| 61 | |||||||||||
| 62 | 2 | ▶ | message => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 63 | 2 | return 'Value is not a bytestring'; | |||||||||
| 64 | }, | ||||||||||
| 65 | 73 | ); | |||||||||
| 66 | |||||||||||
| 67 | 73 | $bytestr->coercion->add_type_coercions( | |||||||||
| 68 | $formatdesc, q{ Bitcoin::Crypto::Helpers::parse_formatdesc(@{$_}) }, | ||||||||||
| 69 | HasMethods ['as_string'], q{ $_->as_string }, | ||||||||||
| 70 | ); | ||||||||||
| 71 | |||||||||||
| 72 | 73 | my $secret = __PACKAGE__->add_type( | |||||||||
| 73 | name => 'BitcoinSecret', | ||||||||||
| 74 | parent => InstanceOf->of('Bitcoin::Crypto::Secret') | InstanceOf->of('Crypt::SecretBuffer'), | ||||||||||
| 75 | ); | ||||||||||
| 76 | |||||||||||
| 77 | 73 | $secret->coercion->add_type_coercions( | |||||||||
| 78 | Defined, q{ | ||||||||||
| 79 | require Bitcoin::Crypto::Secret; | ||||||||||
| 80 | Bitcoin::Crypto::Secret->new($_); | ||||||||||
| 81 | }, | ||||||||||
| 82 | ); | ||||||||||
| 83 | |||||||||||
| 84 | my $bytestrlen = __PACKAGE__->add_type( | ||||||||||
| 85 | name => 'ByteStrLen', | ||||||||||
| 86 | parent => $bytestr, | ||||||||||
| 87 | |||||||||||
| 88 | 141 | ▶ | constraint_generator => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
| |||||||||||
| 89 | 141 | my $len = shift; | |||||||||
| 90 | 141 | PositiveInt->assert_valid($len); | |||||||||
| 91 | |||||||||||
| 92 | 0 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 93 | 0 | return length $_ == $len; | |||||||||
| 94 | 141 | }; | |||||||||
| 95 | }, | ||||||||||
| 96 | |||||||||||
| 97 | 141 | ▶ | inline_generator => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 98 | 141 | my $len = shift; | |||||||||
| 99 | |||||||||||
| 100 | 606 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 101 | 606 | my $varname = pop; | |||||||||
| 102 | |||||||||||
| 103 | 606 | return (undef, qq{ length $varname == $len }); | |||||||||
| 104 | } | ||||||||||
| 105 | 141 | }, | |||||||||
| 106 | |||||||||||
| 107 | 129 | ▶ | coercion_generator => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 108 | 129 | return $bytestr->coercion; | |||||||||
| 109 | }, | ||||||||||
| 110 | |||||||||||
| 111 | 0 | ▶ | message => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 112 | 0 | my $len = shift; | |||||||||
| 113 | 0 | return "Bytestring does not have length of $len"; | |||||||||
| 114 | }, | ||||||||||
| 115 | 73 | ); | |||||||||
| 116 | |||||||||||
| 117 | 73 | my $scripttype = __PACKAGE__->add_type( | |||||||||
| 118 | name => 'ScriptType', | ||||||||||
| 119 | parent => Enum->of(qw(P2PK P2PKH P2SH P2MS P2WPKH P2WSH P2TR UNKNOWN_SEGWIT NULLDATA)) | ||||||||||
| 120 | ); | ||||||||||
| 121 | |||||||||||
| 122 | 73 | my $scriptdesc = __PACKAGE__->add_type( | |||||||||
| 123 | name => 'ScriptDesc', | ||||||||||
| 124 | parent => Tuple->of( | ||||||||||
| 125 | $scripttype | Enum->of(qw(address)), | ||||||||||
| 126 | Defined, | ||||||||||
| 127 | ) | ||||||||||
| 128 | ); | ||||||||||
| 129 | |||||||||||
| 130 | 73 | my $script = __PACKAGE__->add_type( | |||||||||
| 131 | name => 'BitcoinScript', | ||||||||||
| 132 | parent => InstanceOf->of('Bitcoin::Crypto::Script'), | ||||||||||
| 133 | ); | ||||||||||
| 134 | |||||||||||
| 135 | 73 | $script->coercion->add_type_coercions( | |||||||||
| 136 | $scriptdesc, q{ require Bitcoin::Crypto::Script; Bitcoin::Crypto::Script->from_standard(@$_) }, | ||||||||||
| 137 | $bytestr->coercibles, q{ require Bitcoin::Crypto::Script; Bitcoin::Crypto::Script->from_serialized($_) }, | ||||||||||
| 138 | ); | ||||||||||
| 139 | |||||||||||
| 140 | 73 | my $script_tree = __PACKAGE__->add_type( | |||||||||
| 141 | name => 'BitcoinScriptTree', | ||||||||||
| 142 | parent => InstanceOf->of('Bitcoin::Crypto::Script::Tree'), | ||||||||||
| 143 | ); | ||||||||||
| 144 | |||||||||||
| 145 | 73 | $script_tree->coercion->add_type_coercions( | |||||||||
| 146 | ArrayRef [ArrayRef | HashRef], | ||||||||||
| 147 | q{ require Bitcoin::Crypto::Script::Tree; Bitcoin::Crypto::Script::Tree->new(tree => $_) } | ||||||||||
| 148 | ); | ||||||||||
| 149 | |||||||||||
| 150 | 73 | my $digest = __PACKAGE__->add_type( | |||||||||
| 151 | name => 'BitcoinDigest', | ||||||||||
| 152 | parent => InstanceOf->of('Bitcoin::Crypto::Transaction::Digest::Result'), | ||||||||||
| 153 | ); | ||||||||||
| 154 | |||||||||||
| 155 | 73 | $digest->coercion->add_type_coercions( | |||||||||
| 156 | $bytestr->coercibles, q{ | ||||||||||
| 157 | require Bitcoin::Crypto::Transaction::Digest::Result; | ||||||||||
| 158 | Bitcoin::Crypto::Transaction::Digest::Result->new(preimage => $_); | ||||||||||
| 159 | }, | ||||||||||
| 160 | ); | ||||||||||
| 161 | |||||||||||
| 162 | 73 | my $transaction_flags = __PACKAGE__->add_type( | |||||||||
| 163 | name => 'TransactionFlags', | ||||||||||
| 164 | parent => InstanceOf->of('Bitcoin::Crypto::Transaction::Flags'), | ||||||||||
| 165 | ); | ||||||||||
| 166 | |||||||||||
| 167 | 73 | $transaction_flags->coercion->add_type_coercions( | |||||||||
| 168 | HashRef, q{ require Bitcoin::Crypto::Transaction::Flags; Bitcoin::Crypto::Transaction::Flags->new(%$_) }, | ||||||||||
| 169 | Undef, q{ require Bitcoin::Crypto::Transaction::Flags; Bitcoin::Crypto::Transaction::Flags->new }, | ||||||||||
| 170 | ); | ||||||||||
| 171 | |||||||||||
| 172 | 73 | my $psbt_map_type = __PACKAGE__->add_type( | |||||||||
| 173 | name => 'PSBTMapType', | ||||||||||
| 174 | parent => Enum->of( | ||||||||||
| 175 | PSBT_GLOBAL_MAP, | ||||||||||
| 176 | PSBT_INPUT_MAP, | ||||||||||
| 177 | PSBT_OUTPUT_MAP, | ||||||||||
| 178 | ), | ||||||||||
| 179 | ); | ||||||||||
| 180 | |||||||||||
| 181 | 73 | my $psbt_field_type = __PACKAGE__->add_type( | |||||||||
| 182 | name => 'PSBTFieldType', | ||||||||||
| 183 | parent => InstanceOf->of('Bitcoin::Crypto::PSBT::FieldType'), | ||||||||||
| 184 | ); | ||||||||||
| 185 | |||||||||||
| 186 | 73 | $psbt_field_type->coercion->add_type_coercions( | |||||||||
| 187 | Tuple->of($psbt_map_type, PositiveOrZeroInt), | ||||||||||
| 188 | q{ require Bitcoin::Crypto::PSBT::FieldType; Bitcoin::Crypto::PSBT::FieldType->get_field_by_code(@$_) }, | ||||||||||
| 189 | Str, q{ require Bitcoin::Crypto::PSBT::FieldType; Bitcoin::Crypto::PSBT::FieldType->get_field_by_name($_) }, | ||||||||||
| 190 | ); | ||||||||||
| 191 | |||||||||||
| 192 | __PACKAGE__->add_type( | ||||||||||
| 193 | name => 'IntMaxBits', | ||||||||||
| 194 | parent => PositiveOrZeroInt, | ||||||||||
| 195 | |||||||||||
| 196 | 140 | ▶ | constraint_generator => sub { | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=12.0
| |||||||||||
| 197 | 140 | my $bits = shift; | |||||||||
| 198 | 140 | PositiveInt->assert_valid($bits); | |||||||||
| 199 | |||||||||||
| 200 | # for same bits as system, no need for special constraint | ||||||||||
| 201 | 0 | ▶ | return sub { 1 } | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 202 | 140 | ▶ | if Bitcoin::Crypto::Constants::ivsize * 8 == $bits; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 203 | |||||||||||
| 204 | # can't handle | ||||||||||
| 205 | 140 | ▶ | die_no_trace 'IntMaxBits only handles up to ' | ||||||||
|
Branch: 1 branch
| |||||||||||
| 206 | . (Bitcoin::Crypto::Constants::ivsize * 8) | ||||||||||
| 207 | . ' bits on this system' | ||||||||||
| 208 | if Bitcoin::Crypto::Constants::ivsize * 8 < $bits; | ||||||||||
| 209 | |||||||||||
| 210 | 140 | my $limit = 1 << $bits; | |||||||||
| 211 | 0 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 212 | 0 | return $_ < $limit; | |||||||||
| 213 | 140 | }; | |||||||||
| 214 | }, | ||||||||||
| 215 | |||||||||||
| 216 | 140 | ▶ | inline_generator => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 217 | 140 | my $bits = shift; | |||||||||
| 218 | |||||||||||
| 219 | 283 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||
| 220 | |||||||||||
| 221 | # for same bits as system, no need for special constraint | ||||||||||
| 222 | 283 | ▶ | return (undef, qq{ 1 }) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 223 | if Bitcoin::Crypto::Constants::ivsize * 8 == $bits; | ||||||||||
| 224 | |||||||||||
| 225 | 283 | my $varname = pop; | |||||||||
| 226 | |||||||||||
| 227 | 283 | my $limit = 1 << $bits; | |||||||||
| 228 | 283 | return (undef, qq{ $varname < $limit }); | |||||||||
| 229 | } | ||||||||||
| 230 | 140 | }, | |||||||||
| 231 | |||||||||||
| 232 | 0 | ▶ | message => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 233 | 0 | my $bits = shift; | |||||||||
| 234 | 0 | return "Value does not fit in $bits bits"; | |||||||||
| 235 | }, | ||||||||||
| 236 | 73 | ); | |||||||||
| 237 | |||||||||||
| 238 | 73 | my $satoshi_amount; | |||||||||
| 239 | 73 | if (USE_BIGINTS) { | |||||||||
| 240 | $satoshi_amount = __PACKAGE__->add_type( | ||||||||||
| 241 | name => 'SatoshiAmount', | ||||||||||
| 242 | parent => InstanceOf->of('Math::BigInt')->where(q{$_ >= 0}), | ||||||||||
| 243 | ); | ||||||||||
| 244 | |||||||||||
| 245 | $satoshi_amount->coercion->add_type_coercions( | ||||||||||
| 246 | Int | Str, q{ Math::BigInt->new($_) }, | ||||||||||
| 247 | ); | ||||||||||
| 248 | } | ||||||||||
| 249 | else { | ||||||||||
| 250 | 73 | $satoshi_amount = __PACKAGE__->add_type( | |||||||||
| 251 | name => 'SatoshiAmount', | ||||||||||
| 252 | parent => PositiveOrZeroInt, | ||||||||||
| 253 | ); | ||||||||||
| 254 | |||||||||||
| 255 | 73 | $satoshi_amount->coercion->add_type_coercions( | |||||||||
| 256 | Str, q{ | ||||||||||
| 257 | if (/^0x/) { | ||||||||||
| 258 | hex $_ | ||||||||||
| 259 | } | ||||||||||
| 260 | else { | ||||||||||
| 261 | s/_//g; | ||||||||||
| 262 | $_ | ||||||||||
| 263 | } | ||||||||||
| 264 | }, | ||||||||||
| 265 | InstanceOf ['Math::BigInt'], q{ $_->numify }, | ||||||||||
| 266 | ); | ||||||||||
| 267 | } | ||||||||||
| 268 | |||||||||||
| 269 | 73 | my $derivation_path = __PACKAGE__->add_type( | |||||||||
| 270 | name => 'DerivationPath', | ||||||||||
| 271 | parent => InstanceOf->of('Bitcoin::Crypto::DerivationPath'), | ||||||||||
| 272 | ); | ||||||||||
| 273 | |||||||||||
| 274 | 73 | $derivation_path->coercion->add_type_coercions( | |||||||||
| 275 | Str, q{ require Bitcoin::Crypto::DerivationPath; Bitcoin::Crypto::DerivationPath->from_string($_) }, | ||||||||||
| 276 | ConsumerOf->of('Bitcoin::Crypto::Role::WithDerivationPath'), q{ $_->get_derivation_path }, | ||||||||||
| 277 | ); | ||||||||||
| 278 | |||||||||||
| 279 | 73 | __PACKAGE__->make_immutable; | |||||||||
| 280 | |||||||||||
| 281 | 73 | 1; | |||||||||
| 282 | |||||||||||
| 283 | __END__ | ||||||||||