@@ -3,41 +3,6 @@ use crate::*;
33use bech32:: ToBase32 ;
44use ed25519_bip32:: XPub ;
55
6- // returns (Number represented, bytes read) if valid encoding
7- // or None if decoding prematurely finished
8- pub ( crate ) fn variable_nat_decode ( bytes : & [ u8 ] , fallback_to_max : bool ) -> Option < ( u64 , usize ) > {
9- let mut output = 0u128 ;
10- let mut bytes_read = 0 ;
11- for byte in bytes {
12- if output < u64:: MAX . into ( ) || !fallback_to_max {
13- output = ( output << 7 ) | ( byte & 0x7F ) as u128 ;
14- }
15- //Since Conway era forbids pointer addresses, technically such large numbers would not make sense on mainnet
16- if output > u64:: MAX . into ( ) {
17- if !fallback_to_max {
18- return None ;
19- }
20- output = u64:: MAX . into ( ) ;
21- }
22- bytes_read += 1 ;
23- if ( byte & 0x80 ) == 0 {
24- return Some ( ( output as u64 , bytes_read) ) ;
25- }
26- }
27- None
28- }
29-
30- pub ( crate ) fn variable_nat_encode ( mut num : u64 ) -> Vec < u8 > {
31- let mut output = vec ! [ num as u8 & 0x7F ] ;
32- num /= 128 ;
33- while num > 0 {
34- output. push ( ( num & 0x7F ) as u8 | 0x80 ) ;
35- num /= 128 ;
36- }
37- output. reverse ( ) ;
38- output
39- }
40-
416#[ wasm_bindgen]
427#[ derive( Debug , Clone , Eq , Hash , Ord , PartialEq , PartialOrd ) ]
438pub enum AddressKind {
@@ -375,9 +340,7 @@ impl Address {
375340 0b0100_0000 | ( ( ptr. payment . kind ( ) as u8 ) << 4 ) | ( ptr. network & 0xF ) ;
376341 buf. push ( header) ;
377342 buf. extend ( ptr. payment . to_raw_bytes ( ) ) ;
378- buf. extend ( variable_nat_encode ( ptr. stake . slot . into ( ) ) ) ;
379- buf. extend ( variable_nat_encode ( ptr. stake . tx_index . into ( ) ) ) ;
380- buf. extend ( variable_nat_encode ( ptr. stake . cert_index . into ( ) ) ) ;
343+ buf. extend ( ptr. stake . to_bytes ( ) ) ;
381344 }
382345 AddrType :: Enterprise ( enterprise) => {
383346 let header: u8 = 0b0110_0000
@@ -479,7 +442,7 @@ impl Address {
479442 let mut byte_index = 1 ;
480443 let payment_cred = read_addr_cred ( 4 , 1 ) ;
481444 byte_index += HASH_LEN ;
482- match Self :: decode_pointer ( & data[ byte_index..] ) {
445+ match Pointer :: from_bytes ( & data[ byte_index..] ) {
483446 Ok ( ( pointer, offset) ) => {
484447 byte_index += offset;
485448 if byte_index < data. len ( ) && !ignore_leftover_bytes {
@@ -547,35 +510,6 @@ impl Address {
547510 . map_err ( |e| e. annotate ( "Address" ) )
548511 }
549512
550- fn decode_pointer ( data : & [ u8 ] ) -> Result < ( Pointer , usize ) , DeserializeError > {
551- let mut offset = 0 ;
552- let ( slot, slot_bytes) = variable_nat_decode ( & data, true ) . ok_or ( DeserializeError :: new (
553- "Address.Pointer.slot" ,
554- DeserializeFailure :: VariableLenNatDecodeFailed ,
555- ) ) ?;
556- offset += slot_bytes;
557- let ( tx_index, tx_bytes) =
558- variable_nat_decode ( & data[ offset..] , true ) . ok_or ( DeserializeError :: new (
559- "Address.Pointer.tx_index" ,
560- DeserializeFailure :: VariableLenNatDecodeFailed ,
561- ) ) ?;
562- offset += tx_bytes;
563- let ( cert_index, cert_bytes) =
564- variable_nat_decode ( & data[ offset..] , true ) . ok_or ( DeserializeError :: new (
565- "Address.Pointer.cert_index" ,
566- DeserializeFailure :: VariableLenNatDecodeFailed ,
567- ) ) ?;
568- offset += cert_bytes;
569- Ok ( (
570- Pointer :: new_pointer (
571- & slot. into ( ) ,
572- & tx_index. into ( ) ,
573- & cert_index. into ( ) ,
574- ) ,
575- offset,
576- ) )
577- }
578-
579513 pub fn to_bech32 ( & self , prefix : Option < String > ) -> Result < String , JsError > {
580514 let final_prefix = match prefix {
581515 Some ( prefix) => prefix,
@@ -817,64 +751,6 @@ impl Deserialize for RewardAddress {
817751 }
818752}
819753
820- #[ wasm_bindgen]
821- #[ derive( Debug , Clone , Eq , Ord , PartialEq , PartialOrd ) ]
822- pub struct Pointer {
823- pub ( crate ) slot : BigNum ,
824- pub ( crate ) tx_index : BigNum ,
825- pub ( crate ) cert_index : BigNum ,
826- }
827-
828- #[ wasm_bindgen]
829- impl Pointer {
830- /// !!! DEPRECATED !!!
831- /// This constructor uses outdated slot number format for the ttl value, tx_index and cert_index.
832- /// Use `.new_pointer` instead
833- #[ deprecated(
834- since = "10.1.0" ,
835- note = "Underlying value capacity of ttl (BigNum u64) bigger then Slot32. Use new_pointer instead."
836- ) ]
837- pub fn new ( slot : Slot32 , tx_index : TransactionIndex , cert_index : CertificateIndex ) -> Self {
838- Self {
839- slot : slot. into ( ) ,
840- tx_index : tx_index. into ( ) ,
841- cert_index : cert_index. into ( ) ,
842- }
843- }
844-
845- pub fn new_pointer ( slot : & SlotBigNum , tx_index : & BigNum , cert_index : & BigNum ) -> Self {
846- Self {
847- slot : slot. clone ( ) ,
848- tx_index : tx_index. clone ( ) ,
849- cert_index : cert_index. clone ( ) ,
850- }
851- }
852-
853- pub fn slot ( & self ) -> Result < u32 , JsError > {
854- self . slot . clone ( ) . try_into ( )
855- }
856-
857- pub fn tx_index ( & self ) -> Result < u32 , JsError > {
858- self . tx_index . clone ( ) . try_into ( )
859- }
860-
861- pub fn cert_index ( & self ) -> Result < u32 , JsError > {
862- self . cert_index . clone ( ) . try_into ( )
863- }
864-
865- pub fn slot_bignum ( & self ) -> BigNum {
866- self . slot . clone ( )
867- }
868-
869- pub fn tx_index_bignum ( & self ) -> BigNum {
870- self . tx_index . clone ( )
871- }
872-
873- pub fn cert_index_bignum ( & self ) -> BigNum {
874- self . cert_index . clone ( )
875- }
876- }
877-
878754#[ wasm_bindgen]
879755#[ derive( Debug , Clone , Eq , Ord , PartialEq , PartialOrd ) ]
880756pub struct PointerAddress {
0 commit comments