diff --git a/rust/pkg/cardano_serialization_lib.js.flow b/rust/pkg/cardano_serialization_lib.js.flow index 7e054184..bd79a739 100644 --- a/rust/pkg/cardano_serialization_lib.js.flow +++ b/rust/pkg/cardano_serialization_lib.js.flow @@ -1900,10 +1900,18 @@ declare export class HeaderBody { block_number(): number; /** + * !!! DEPRECATED !!! + * Returns a Slot32 (u32) value in case the underlying original BigNum (u64) value is within the limits. + * Otherwise will just raise an error. * @returns {number} */ slot(): number; + /** + * @returns {BigNum} + */ + slot_bignum(): BigNum; + /** * @returns {BlockHash | void} */ @@ -1950,6 +1958,9 @@ declare export class HeaderBody { protocol_version(): ProtocolVersion; /** + * !!! DEPRECATED !!! + * This constructor uses outdated slot number format. + * Use `.new_headerbody` instead * @param {number} block_number * @param {number} slot * @param {BlockHash | void} prev_hash @@ -1976,12 +1987,51 @@ declare export class HeaderBody { operational_cert: OperationalCert, protocol_version: ProtocolVersion ): HeaderBody; + + /** + * @param {number} block_number + * @param {BigNum} slot + * @param {BlockHash | void} prev_hash + * @param {Vkey} issuer_vkey + * @param {VRFVKey} vrf_vkey + * @param {VRFCert} nonce_vrf + * @param {VRFCert} leader_vrf + * @param {number} block_body_size + * @param {BlockHash} block_body_hash + * @param {OperationalCert} operational_cert + * @param {ProtocolVersion} protocol_version + * @returns {HeaderBody} + */ + static new_headerbody( + block_number: number, + slot: BigNum, + prev_hash: BlockHash | void, + issuer_vkey: Vkey, + vrf_vkey: VRFVKey, + nonce_vrf: VRFCert, + leader_vrf: VRFCert, + block_body_size: number, + block_body_hash: BlockHash, + operational_cert: OperationalCert, + protocol_version: ProtocolVersion + ): HeaderBody; } /** */ declare export class Int { free(): void; + /** + * @returns {Uint8Array} + */ + to_bytes(): Uint8Array; + + /** + * @param {Uint8Array} bytes + * @returns {Int} + */ + static from_bytes(bytes: Uint8Array): Int; + /** * @param {BigNum} x * @returns {Int} @@ -3212,6 +3262,9 @@ declare export class Pointer { free(): void; /** + * !!! DEPRECATED !!! + * This constructor uses outdated slot number format for the ttl value, tx_index and cert_index. + * Use `.new_pointer` instead * @param {number} slot * @param {number} tx_index * @param {number} cert_index @@ -3219,6 +3272,18 @@ declare export class Pointer { */ static new(slot: number, tx_index: number, cert_index: number): Pointer; + /** + * @param {BigNum} slot + * @param {BigNum} tx_index + * @param {BigNum} cert_index + * @returns {Pointer} + */ + static new_pointer( + slot: BigNum, + tx_index: BigNum, + cert_index: BigNum + ): Pointer; + /** * @returns {number} */ @@ -3233,6 +3298,21 @@ declare export class Pointer { * @returns {number} */ cert_index(): number; + + /** + * @returns {BigNum} + */ + slot_bignum(): BigNum; + + /** + * @returns {BigNum} + */ + tx_index_bignum(): BigNum; + + /** + * @returns {BigNum} + */ + cert_index_bignum(): BigNum; } /** */ @@ -4769,10 +4849,24 @@ declare export class TimelockExpiry { slot(): number; /** + * @returns {BigNum} + */ + slot_bignum(): BigNum; + + /** + * !!! DEPRECATED !!! + * This constructor uses outdated slot number format. + * Use `.new_timelockexpiry` instead * @param {number} slot * @returns {TimelockExpiry} */ static new(slot: number): TimelockExpiry; + + /** + * @param {BigNum} slot + * @returns {TimelockExpiry} + */ + static new_timelockexpiry(slot: BigNum): TimelockExpiry; } /** */ @@ -4791,15 +4885,33 @@ declare export class TimelockStart { static from_bytes(bytes: Uint8Array): TimelockStart; /** + * !!! DEPRECATED !!! + * Returns a Slot32 (u32) value in case the underlying original BigNum (u64) value is within the limits. + * Otherwise will just raise an error. + * Use `.slot_bignum` instead * @returns {number} */ slot(): number; /** + * @returns {BigNum} + */ + slot_bignum(): BigNum; + + /** + * !!! DEPRECATED !!! + * This constructor uses outdated slot number format. + * Use `.new_timelockstart` instead. * @param {number} slot * @returns {TimelockStart} */ static new(slot: number): TimelockStart; + + /** + * @param {BigNum} slot + * @returns {TimelockStart} + */ + static new_timelockstart(slot: BigNum): TimelockStart; } /** */ @@ -4923,10 +5035,27 @@ declare export class TransactionBody { fee(): BigNum; /** + * !!! DEPRECATED !!! + * Returns a Slot32 (u32) value in case the underlying original BigNum (u64) value is within the limits. + * Otherwise will just raise an error. * @returns {number | void} */ ttl(): number | void; + /** + * @returns {BigNum | void} + */ + ttl_bignum(): BigNum | void; + + /** + * @param {BigNum} ttl + */ + set_ttl(ttl: BigNum): void; + + /** + */ + remove_ttl(): void; + /** * @param {Certificates} certs */ @@ -4968,11 +5097,27 @@ declare export class TransactionBody { auxiliary_data_hash(): AuxiliaryDataHash | void; /** + * !!! DEPRECATED !!! + * Uses outdated slot number format. * @param {number} validity_start_interval */ set_validity_start_interval(validity_start_interval: number): void; /** + * @param {BigNum} validity_start_interval + */ + set_validity_start_interval_bignum(validity_start_interval: BigNum): void; + + /** + * @returns {BigNum | void} + */ + validity_start_interval_bignum(): BigNum | void; + + /** + * !!! DEPRECATED !!! + * Returns a Option (u32) value in case the underlying original Option (u64) value is within the limits. + * Otherwise will just raise an error. + * Use `.validity_start_interval_bignum` instead. * @returns {number | void} */ validity_start_interval(): number | void; @@ -5035,6 +5180,9 @@ declare export class TransactionBody { network_id(): NetworkId | void; /** + * !!! DEPRECATED !!! + * This constructor uses outdated slot number format for the ttl value. + * Use `.new_tx_body` and then `.set_ttl` instead * @param {TransactionInputs} inputs * @param {TransactionOutputs} outputs * @param {BigNum} fee @@ -5047,6 +5195,21 @@ declare export class TransactionBody { fee: BigNum, ttl?: number ): TransactionBody; + + /** + * Returns a new TransactionBody. + * In the new version of "new" we removed optional ttl for support it by wasm_bingen. + * Your can use "set_ttl" and "remove_ttl" to set a new value for ttl or set it as None. + * @param {TransactionInputs} inputs + * @param {TransactionOutputs} outputs + * @param {BigNum} fee + * @returns {TransactionBody} + */ + static new_tx_body( + inputs: TransactionInputs, + outputs: TransactionOutputs, + fee: BigNum + ): TransactionBody; } /** */ @@ -5141,15 +5304,29 @@ declare export class TransactionBuilder { set_fee(fee: BigNum): void; /** + * !!! DEPRECATED !!! + * Set ttl value. * @param {number} ttl */ set_ttl(ttl: number): void; /** + * @param {BigNum} ttl + */ + set_ttl_bignum(ttl: BigNum): void; + + /** + * !!! DEPRECATED !!! + * Uses outdated slot number format. * @param {number} validity_start_interval */ set_validity_start_interval(validity_start_interval: number): void; + /** + * @param {BigNum} validity_start_interval + */ + set_validity_start_interval_bignum(validity_start_interval: BigNum): void; + /** * @param {Certificates} certs */ diff --git a/rust/src/address.rs b/rust/src/address.rs index 7bb31a97..54d40881 100644 --- a/rust/src/address.rs +++ b/rust/src/address.rs @@ -395,7 +395,7 @@ impl Address { PointerAddress::new( network, &payment_cred, - &Pointer::new( + &Pointer::new_pointer( &to_bignum(slot), &to_bignum(tx_index), &to_bignum(cert_index)))) @@ -618,7 +618,23 @@ pub struct Pointer { #[wasm_bindgen] impl Pointer { - pub fn new(slot: &BigNum, tx_index: &BigNum, cert_index: &BigNum) -> Self { + + /// !!! DEPRECATED !!! + /// This constructor uses outdated slot number format for the ttl value, tx_index and cert_index. + /// Use `.new_pointer` instead + #[deprecated( + since = "10.1.0", + note = "Underlying value capacity of ttl (BigNum u64) bigger then Slot32. Use new_pointer instead." + )] + pub fn new(slot: Slot32, tx_index: TransactionIndex, cert_index: CertificateIndex) -> Self { + Self { + slot: slot.into(), + tx_index: tx_index.into(), + cert_index: cert_index.into(), + } + } + + pub fn new_pointer(slot: &SlotBigNum, tx_index: &BigNum, cert_index: &BigNum) -> Self { Self { slot: slot.clone(), tx_index: tx_index.clone(), @@ -626,15 +642,27 @@ impl Pointer { } } - pub fn slot(&self) -> BigNum { + pub fn slot(&self) -> Result { + self.slot.clone().try_into() + } + + pub fn tx_index(&self) -> Result { + self.tx_index.clone().try_into() + } + + pub fn cert_index(&self) -> Result { + self.cert_index.clone().try_into() + } + + pub fn slot_bignum(&self) -> BigNum { self.slot.clone() } - pub fn tx_index(&self) -> BigNum { + pub fn tx_index_bignum(&self) -> BigNum { self.tx_index.clone() } - pub fn cert_index(&self) -> BigNum { + pub fn cert_index_bignum(&self) -> BigNum { self.cert_index.clone() } } @@ -720,7 +748,7 @@ mod tests { let ptr = PointerAddress::new( 25, &StakeCredential::from_keyhash(&Ed25519KeyHash::from([23; Ed25519KeyHash::BYTE_COUNT])), - &Pointer::new(&to_bignum(2354556573), &to_bignum(127), &to_bignum(0))); + &Pointer::new_pointer(&to_bignum(2354556573), &to_bignum(127), &to_bignum(0))); let addr = ptr.to_address(); let addr2 = Address::from_bytes(addr.to_bytes()).unwrap(); assert_eq!(addr.to_bytes(), addr2.to_bytes()); @@ -836,9 +864,9 @@ mod tests { .derive(0) .to_public(); let spend_cred = StakeCredential::from_keyhash(&spend.to_raw_key().hash()); - let addr_net_0 = PointerAddress::new(NetworkInfo::testnet().network_id(), &spend_cred, &Pointer::new(&to_bignum(1), &to_bignum(2), &to_bignum(3))).to_address(); + let addr_net_0 = PointerAddress::new(NetworkInfo::testnet().network_id(), &spend_cred, &Pointer::new_pointer(&to_bignum(1), &to_bignum(2), &to_bignum(3))).to_address(); assert_eq!(addr_net_0.to_bech32(None).unwrap(), "addr_test1gz2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzerspqgpsqe70et"); - let addr_net_3 = PointerAddress::new(NetworkInfo::mainnet().network_id(), &spend_cred, &Pointer::new(&to_bignum(24157), &to_bignum(177), &to_bignum(42))).to_address(); + let addr_net_3 = PointerAddress::new(NetworkInfo::mainnet().network_id(), &spend_cred, &Pointer::new_pointer(&to_bignum(24157), &to_bignum(177), &to_bignum(42))).to_address(); assert_eq!(addr_net_3.to_bech32(None).unwrap(), "addr1gx2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer5ph3wczvf2w8lunk"); } @@ -892,9 +920,9 @@ mod tests { .derive(0) .to_public(); let spend_cred = StakeCredential::from_keyhash(&spend.to_raw_key().hash()); - let addr_net_0 = PointerAddress::new(NetworkInfo::testnet().network_id(), &spend_cred, &Pointer::new(&to_bignum(1), &to_bignum(2), &to_bignum(3))).to_address(); + let addr_net_0 = PointerAddress::new(NetworkInfo::testnet().network_id(), &spend_cred, &Pointer::new_pointer(&to_bignum(1), &to_bignum(2), &to_bignum(3))).to_address(); assert_eq!(addr_net_0.to_bech32(None).unwrap(), "addr_test1gpu5vlrf4xkxv2qpwngf6cjhtw542ayty80v8dyr49rf5egpqgpsdhdyc0"); - let addr_net_3 = PointerAddress::new(NetworkInfo::mainnet().network_id(), &spend_cred, &Pointer::new(&to_bignum(24157), &to_bignum(177), &to_bignum(42))).to_address(); + let addr_net_3 = PointerAddress::new(NetworkInfo::mainnet().network_id(), &spend_cred, &Pointer::new_pointer(&to_bignum(24157), &to_bignum(177), &to_bignum(42))).to_address(); assert_eq!(addr_net_3.to_bech32(None).unwrap(), "addr1g9u5vlrf4xkxv2qpwngf6cjhtw542ayty80v8dyr49rf5evph3wczvf2kd5vam"); } @@ -975,9 +1003,9 @@ mod tests { .derive(0) .to_public(); let spend_cred = StakeCredential::from_keyhash(&spend.to_raw_key().hash()); - let addr_net_0 = PointerAddress::new(NetworkInfo::testnet().network_id(), &spend_cred, &Pointer::new(&to_bignum(1), &to_bignum(2), &to_bignum(3))).to_address(); + let addr_net_0 = PointerAddress::new(NetworkInfo::testnet().network_id(), &spend_cred, &Pointer::new_pointer(&to_bignum(1), &to_bignum(2), &to_bignum(3))).to_address(); assert_eq!(addr_net_0.to_bech32(None).unwrap(), "addr_test1gqy6nhfyks7wdu3dudslys37v252w2nwhv0fw2nfawemmnqpqgps5mee0p"); - let addr_net_3 = PointerAddress::new(NetworkInfo::mainnet().network_id(), &spend_cred, &Pointer::new(&to_bignum(24157), &to_bignum(177), &to_bignum(42))).to_address(); + let addr_net_3 = PointerAddress::new(NetworkInfo::mainnet().network_id(), &spend_cred, &Pointer::new_pointer(&to_bignum(24157), &to_bignum(177), &to_bignum(42))).to_address(); assert_eq!(addr_net_3.to_bech32(None).unwrap(), "addr1gyy6nhfyks7wdu3dudslys37v252w2nwhv0fw2nfawemmnyph3wczvf2dqflgt"); } @@ -1059,4 +1087,11 @@ mod tests { assert_eq!(u64::MAX, from_bignum(&ptr.tx_index)); assert_eq!(u64::MAX, from_bignum(&ptr.cert_index)); } + + #[test] + fn point_address_old() { + let p1 = Pointer::new(10, 20, 30); + let p2 = Pointer::new_pointer(&to_bignum(10), &to_bignum(20), &to_bignum(30)); + assert_eq!(p1, p2); + } } diff --git a/rust/src/lib.rs b/rust/src/lib.rs index 09db52b5..cbc9fc50 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -95,7 +95,8 @@ impl UnitInterval { type SubCoin = UnitInterval; type Rational = UnitInterval; type Epoch = u32; -type Slot = u32; +type Slot32 = u32; +type SlotBigNum = BigNum; #[wasm_bindgen] #[derive(Clone)] @@ -233,12 +234,12 @@ pub struct TransactionBody { inputs: TransactionInputs, outputs: TransactionOutputs, fee: Coin, - ttl: Option, + ttl: Option, certs: Option, withdrawals: Option, update: Option, auxiliary_data_hash: Option, - validity_start_interval: Option, + validity_start_interval: Option, mint: Option, script_data_hash: Option, collateral: Option, @@ -262,10 +263,36 @@ impl TransactionBody { self.fee.clone() } - pub fn ttl(&self) -> Option { + + /// !!! DEPRECATED !!! + /// Returns a Slot32 (u32) value in case the underlying original BigNum (u64) value is within the limits. + /// Otherwise will just raise an error. + #[deprecated( + since = "10.1.0", + note = "Possible boundary error. Use ttl_bignum instead" + )] + pub fn ttl(&self) -> Result, JsError> { + match self.ttl { + Some(ttl) => + match ttl.try_into() { + Ok(ttl32) => Ok(Some(ttl32)), + Err(err) => Err(err)}, + None => Ok(None) + } + } + + pub fn ttl_bignum(&self) -> Option { self.ttl } + pub fn set_ttl(&mut self, ttl: &SlotBigNum) { + self.ttl = Some(ttl.clone()) + } + + pub fn remove_ttl(&mut self) { + self.ttl = None + } + pub fn set_certs(&mut self, certs: &Certificates) { self.certs = Some(certs.clone()) } @@ -298,14 +325,43 @@ impl TransactionBody { self.auxiliary_data_hash.clone() } - pub fn set_validity_start_interval(&mut self, validity_start_interval: Slot) { - self.validity_start_interval = Some(validity_start_interval) + /// !!! DEPRECATED !!! + /// Uses outdated slot number format. + #[deprecated( + since = "10.1.0", + note = "Underlying value capacity of slot (BigNum u64) bigger then Slot32. Use set_validity_start_interval_bignum instead." + )] + pub fn set_validity_start_interval(&mut self, validity_start_interval: Slot32) { + self.validity_start_interval = Some(validity_start_interval.into()) + } + + pub fn set_validity_start_interval_bignum(&mut self, validity_start_interval: SlotBigNum) { + self.validity_start_interval = Some(validity_start_interval.clone()) } - pub fn validity_start_interval(&self) -> Option { + pub fn validity_start_interval_bignum(&self) -> Option { self.validity_start_interval.clone() } + /// !!! DEPRECATED !!! + /// Returns a Option (u32) value in case the underlying original Option (u64) value is within the limits. + /// Otherwise will just raise an error. + /// Use `.validity_start_interval_bignum` instead. + #[deprecated( + since = "10.1.0", + note = "Possible boundary error. Use validity_start_interval_bignum instead" + )] + pub fn validity_start_interval(&self) -> Result, JsError> { + match self.validity_start_interval.clone() { + Some(interval) => match interval.try_into() + { + Ok(internal32) => Ok(Some(internal32)), + Err(err) => Err(err) + }, + None => Ok(None) + } + } + pub fn set_mint(&mut self, mint: &Mint) { self.mint = Some(mint.clone()) } @@ -356,16 +412,51 @@ impl TransactionBody { self.network_id.clone() } + /// !!! DEPRECATED !!! + /// This constructor uses outdated slot number format for the ttl value. + /// Use `.new_tx_body` and then `.set_ttl` instead + #[deprecated( + since = "10.1.0", + note = "Underlying value capacity of ttl (BigNum u64) bigger then Slot32. Use new_tx_body instead." + )] pub fn new( inputs: &TransactionInputs, outputs: &TransactionOutputs, fee: &Coin, - ttl: Option) -> Self { + ttl: Option) -> Self { + Self { + inputs: inputs.clone(), + outputs: outputs.clone(), + fee: fee.clone(), + ttl: match ttl.clone() { + Some(ttl32) => Some(ttl32.into()), + None => None + }, + certs: None, + withdrawals: None, + update: None, + auxiliary_data_hash: None, + validity_start_interval: None, + mint: None, + script_data_hash: None, + collateral: None, + required_signers: None, + network_id: None, + } + } + + /// Returns a new TransactionBody. + /// In the new version of "new" we removed optional ttl for support it by wasm_bingen. + /// Your can use "set_ttl" and "remove_ttl" to set a new value for ttl or set it as None. + pub fn new_tx_body( + inputs: &TransactionInputs, + outputs: &TransactionOutputs, + fee: &Coin) -> Self { Self { inputs: inputs.clone(), outputs: outputs.clone(), fee: fee.clone(), - ttl: ttl, + ttl: None, certs: None, withdrawals: None, update: None, @@ -1562,20 +1653,47 @@ impl ScriptNOfK { #[wasm_bindgen] #[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)] pub struct TimelockStart { - slot: Slot, + slot: SlotBigNum, } to_from_bytes!(TimelockStart); #[wasm_bindgen] impl TimelockStart { - pub fn slot(&self) -> Slot { + + /// !!! DEPRECATED !!! + /// Returns a Slot32 (u32) value in case the underlying original BigNum (u64) value is within the limits. + /// Otherwise will just raise an error. + /// Use `.slot_bignum` instead + #[deprecated( + since = "10.1.0", + note = "Possible boundary error. Use slot_bignum instead" + )] + pub fn slot(&self) -> Result { + self.slot.try_into() + } + + + pub fn slot_bignum(&self) -> SlotBigNum{ self.slot } - pub fn new(slot: Slot) -> Self { + /// !!! DEPRECATED !!! + /// This constructor uses outdated slot number format. + /// Use `.new_timelockstart` instead. + #[deprecated( + since = "10.1.0", + note = "Underlying value capacity (BigNum u64) bigger then Slot32. Use new_bignum instead." + )] + pub fn new(slot: Slot32) -> Self { Self { - slot, + slot: slot.into(), + } + } + + pub fn new_timelockstart(slot: &SlotBigNum) -> Self { + Self { + slot: slot.clone(), } } } @@ -1583,20 +1701,37 @@ impl TimelockStart { #[wasm_bindgen] #[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)] pub struct TimelockExpiry { - slot: Slot, + slot: SlotBigNum, } to_from_bytes!(TimelockExpiry); #[wasm_bindgen] impl TimelockExpiry { - pub fn slot(&self) -> Slot { + pub fn slot(&self) -> Result { + self.slot.try_into() + } + + pub fn slot_bignum(&self) -> SlotBigNum { self.slot } - pub fn new(slot: Slot) -> Self { + /// !!! DEPRECATED !!! + /// This constructor uses outdated slot number format. + /// Use `.new_timelockexpiry` instead + #[deprecated( + since = "10.1.0", + note = "Underlying value capacity (BigNum u64) bigger then Slot32. Use new_bignum instead." + )] + pub fn new(slot: Slot32) -> Self { + Self { + slot: (slot.into()) + } + } + + pub fn new_timelockexpiry(slot: &SlotBigNum) -> Self { Self { - slot, + slot : slot.clone() } } } @@ -2394,7 +2529,7 @@ impl OperationalCert { #[derive(Clone)] pub struct HeaderBody { block_number: u32, - slot: Slot, + slot: SlotBigNum, prev_hash: Option, issuer_vkey: Vkey, vrf_vkey: VRFVKey, @@ -2414,7 +2549,19 @@ impl HeaderBody { self.block_number.clone() } - pub fn slot(&self) -> Slot { + + /// !!! DEPRECATED !!! + /// Returns a Slot32 (u32) value in case the underlying original BigNum (u64) value is within the limits. + /// Otherwise will just raise an error. + #[deprecated( + since = "10.1.0", + note = "Possible boundary error. Use slot_bignum instead" + )] + pub fn slot(&self) -> Result { + self.slot.clone().try_into() + } + + pub fn slot_bignum(&self) -> SlotBigNum { self.slot.clone() } @@ -2454,10 +2601,33 @@ impl HeaderBody { self.protocol_version.clone() } - pub fn new(block_number: u32, slot: Slot, prev_hash: Option, issuer_vkey: &Vkey, vrf_vkey: &VRFVKey, nonce_vrf: &VRFCert, leader_vrf: &VRFCert, block_body_size: u32, block_body_hash: &BlockHash, operational_cert: &OperationalCert, protocol_version: &ProtocolVersion) -> Self { + /// !!! DEPRECATED !!! + /// This constructor uses outdated slot number format. + /// Use `.new_headerbody` instead + #[deprecated( + since = "10.1.0", + note = "Underlying value capacity of slot (BigNum u64) bigger then Slot32. Use new_bignum instead." + )] + pub fn new(block_number: u32, slot: Slot32, prev_hash: Option, issuer_vkey: &Vkey, vrf_vkey: &VRFVKey, nonce_vrf: &VRFCert, leader_vrf: &VRFCert, block_body_size: u32, block_body_hash: &BlockHash, operational_cert: &OperationalCert, protocol_version: &ProtocolVersion) -> Self { + Self { + block_number: block_number, + slot: slot.clone().into(), + prev_hash: prev_hash.clone(), + issuer_vkey: issuer_vkey.clone(), + vrf_vkey: vrf_vkey.clone(), + nonce_vrf: nonce_vrf.clone(), + leader_vrf: leader_vrf.clone(), + block_body_size: block_body_size, + block_body_hash: block_body_hash.clone(), + operational_cert: operational_cert.clone(), + protocol_version: protocol_version.clone(), + } + } + + pub fn new_headerbody(block_number: u32, slot: &SlotBigNum, prev_hash: Option, issuer_vkey: &Vkey, vrf_vkey: &VRFVKey, nonce_vrf: &VRFCert, leader_vrf: &VRFCert, block_body_size: u32, block_body_hash: &BlockHash, operational_cert: &OperationalCert, protocol_version: &ProtocolVersion) -> Self { Self { block_number: block_number, - slot: slot, + slot: slot.clone(), prev_hash: prev_hash.clone(), issuer_vkey: issuer_vkey.clone(), vrf_vkey: vrf_vkey.clone(), diff --git a/rust/src/serialization.rs b/rust/src/serialization.rs index 8cce61b0..7c398c9a 100644 --- a/rust/src/serialization.rs +++ b/rust/src/serialization.rs @@ -341,7 +341,7 @@ impl Deserialize for TransactionBody { } ttl = Some((|| -> Result<_, DeserializeError> { read_len.read_elems(1)?; - Ok(Slot::deserialize(raw)?) + Ok(SlotBigNum::deserialize(raw)?) })().map_err(|e| e.annotate("ttl"))?); }, 4 => { @@ -386,7 +386,7 @@ impl Deserialize for TransactionBody { } validity_start_interval = Some((|| -> Result<_, DeserializeError> { read_len.read_elems(1)?; - Ok(Slot::deserialize(raw)?) + Ok(SlotBigNum::deserialize(raw)?) })().map_err(|e| e.annotate("validity_start_interval"))?); }, 9 => { @@ -2171,7 +2171,7 @@ impl DeserializeEmbeddedGroup for TimelockStart { Ok(()) })().map_err(|e| e.annotate("index_0"))?; let slot = (|| -> Result<_, DeserializeError> { - Ok(Slot::deserialize(raw)?) + Ok(SlotBigNum::deserialize(raw)?) })().map_err(|e| e.annotate("slot"))?; Ok(TimelockStart { slot, @@ -2223,7 +2223,7 @@ impl DeserializeEmbeddedGroup for TimelockExpiry { Ok(()) })().map_err(|e| e.annotate("index_0"))?; let slot = (|| -> Result<_, DeserializeError> { - Ok(Slot::deserialize(raw)?) + Ok(SlotBigNum::deserialize(raw)?) })().map_err(|e| e.annotate("slot"))?; Ok(TimelockExpiry { slot, @@ -3243,7 +3243,7 @@ impl DeserializeEmbeddedGroup for HeaderBody { Ok(u32::deserialize(raw)?) })().map_err(|e| e.annotate("block_number"))?; let slot = (|| -> Result<_, DeserializeError> { - Ok(Slot::deserialize(raw)?) + Ok(SlotBigNum::deserialize(raw)?) })().map_err(|e| e.annotate("slot"))?; let prev_hash = (|| -> Result<_, DeserializeError> { Ok(match raw.cbor_type()? != CBORType::Special { diff --git a/rust/src/tx_builder.rs b/rust/src/tx_builder.rs index 189ecd4f..84541a8a 100644 --- a/rust/src/tx_builder.rs +++ b/rust/src/tx_builder.rs @@ -308,11 +308,11 @@ pub struct TransactionBuilder { inputs: Vec, outputs: TransactionOutputs, fee: Option, - ttl: Option, // absolute slot number + ttl: Option, // absolute slot number certs: Option, withdrawals: Option, auxiliary_data: Option, - validity_start_interval: Option, + validity_start_interval: Option, input_types: MockWitnessSet, mint: Option, mint_scripts: Option, @@ -710,12 +710,32 @@ impl TransactionBuilder { self.fee = Some(fee.clone()) } - pub fn set_ttl(&mut self, ttl: Slot) { - self.ttl = Some(ttl) + /// !!! DEPRECATED !!! + /// Set ttl value. + #[deprecated( + since = "10.1.0", + note = "Underlying value capacity of ttl (BigNum u64) bigger then Slot32. Use set_ttl_bignum instead." + )] + pub fn set_ttl(&mut self, ttl: Slot32) { + self.ttl = Some(ttl.into()) } - pub fn set_validity_start_interval(&mut self, validity_start_interval: Slot) { - self.validity_start_interval = Some(validity_start_interval) + pub fn set_ttl_bignum(&mut self, ttl: &SlotBigNum) { + self.ttl = Some(ttl.clone()) + } + + /// !!! DEPRECATED !!! + /// Uses outdated slot number format. + #[deprecated( + since = "10.1.0", + note = "Underlying value capacity of validity_start_interval (BigNum u64) bigger then Slot32. Use set_validity_start_interval_bignum instead." + )] + pub fn set_validity_start_interval(&mut self, validity_start_interval: Slot32) { + self.validity_start_interval = Some(validity_start_interval.into()) + } + + pub fn set_validity_start_interval_bignum(&mut self, validity_start_interval: SlotBigNum) { + self.validity_start_interval = Some(validity_start_interval.clone()) } pub fn set_certs(&mut self, certs: &Certificates) { @@ -1846,7 +1866,7 @@ mod tests { &PointerAddress::new( NetworkInfo::testnet().network_id(), &spend_cred, - &Pointer::new( + &Pointer::new_pointer( &to_bignum(0), &to_bignum(0), &to_bignum(0) diff --git a/rust/src/utils.rs b/rust/src/utils.rs index 466d7af6..1ea74e8a 100644 --- a/rust/src/utils.rs +++ b/rust/src/utils.rs @@ -2,6 +2,7 @@ use cbor_event::{self, de::Deserializer, se::{Serialize, Serializer}}; use hex::FromHex; use serde_json; use std::{collections::HashMap, io::{BufRead, Seek, Write}}; +use std::convert::{TryFrom}; use itertools::Itertools; use std::ops::{Rem, Div, Sub}; @@ -228,6 +229,30 @@ impl BigNum { } } +impl TryFrom for u32 { + type Error = JsError; + + fn try_from(value: BigNum) -> Result { + if value.0 > u32::MAX.into() { + Err(JsError::from_str(&format!("Value {} is bigger than max u32 {}", value.0, u32::MAX))) + } else { + Ok(value.0 as u32) + } + } +} + +impl From for BigNum { + fn from(value: u64) -> Self { + return BigNum(value) + } +} + +impl From for BigNum { + fn from(value: u32) -> Self { + return BigNum(value.into()) + } +} + impl cbor_event::se::Serialize for BigNum { fn serialize<'se, W: Write>(&self, serializer: &'se mut Serializer) -> cbor_event::Result<&'se mut Serializer> { serializer.write_unsigned_integer(self.0) @@ -1269,9 +1294,9 @@ fn encode_template_to_native_script( serde_json::Value::Object(map) if map.contains_key("active_from") => { if let serde_json::Value::Number(active_from) = map.get("active_from").unwrap() { if let Some(n) = active_from.as_u64() { - let slot: u32 = n as u32; + let slot: SlotBigNum = n.into(); - let time_lock_start = TimelockStart::new(slot); + let time_lock_start = TimelockStart::new_timelockstart(&slot); Ok(NativeScript::new_timelock_start(&time_lock_start)) } else { @@ -1286,9 +1311,9 @@ fn encode_template_to_native_script( serde_json::Value::Object(map) if map.contains_key("active_until") => { if let serde_json::Value::Number(active_until) = map.get("active_until").unwrap() { if let Some(n) = active_until.as_u64() { - let slot: u32 = n as u32; + let slot: SlotBigNum = n.into(); - let time_lock_expiry = TimelockExpiry::new(slot); + let time_lock_expiry = TimelockExpiry::new_timelockexpiry(&slot); Ok(NativeScript::new_timelock_expiry(&time_lock_expiry)) } else { @@ -2251,7 +2276,7 @@ mod tests { Bip32PublicKey::from_bytes(&hex::decode(cosigner0_hex).unwrap()).unwrap().to_raw_key().hash() ); let all_1 = all.get(1).as_timelock_start().unwrap(); - assert_eq!(all_1.slot(), 120); + assert_eq!(all_1.slot().unwrap(), 120); let any = from.get(1).as_script_any().unwrap().native_scripts(); assert_eq!(all.len(), 2); let any_0 = any.get(0).as_script_pubkey().unwrap(); @@ -2260,7 +2285,7 @@ mod tests { Bip32PublicKey::from_bytes(&hex::decode(cosigner1_hex).unwrap()).unwrap().to_raw_key().hash() ); let any_1 = any.get(1).as_timelock_expiry().unwrap(); - assert_eq!(any_1.slot(), 1000); + assert_eq!(any_1.slot().unwrap(), 1000); let self_key = from.get(2).as_script_pubkey().unwrap(); assert_eq!( self_key.addr_keyhash(),