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