Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RegularTransactionEssence 2.0 changes #657

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 33 additions & 31 deletions bindings/nodejs/tests/client/offlineSigningExamples.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,35 +94,37 @@ describe('Offline signing examples', () => {
expect(blockId).toBe(blockIdAndBlock[0]);
expect(blockId).toBeValidBlockId;
});
it('create a signature unlock', async () => {
// Verifies that an unlock created in Rust matches that created by the binding when the mnemonic is identical.
const secretManager = {
mnemonic:
'good reason pipe keen price glory mystery illegal loud isolate wolf trash raise guilt inflict guide modify bachelor length galaxy lottery there mango comfort',
};
const preparedTx = sigUnlockPreparedTx as any as PreparedTransactionData;
const txEssenceHash = Utils.hashTransactionEssence(
preparedTx.essence,
);

if (preparedTx.inputsData[0].chain === undefined) {
throw 'chain is undefined';
}

const unlock = await new SecretManager(secretManager).signatureUnlock(
txEssenceHash,
preparedTx.inputsData[0].chain,
);

expect(unlock).toStrictEqual({
type: 0,
signature: {
type: 0,
publicKey:
'0xb76a23de43b8132ae18a4a479cb158563e76d89bd1e20d3ccdc7fd1db2a009d4',
signature:
'0xcd905dae45010980e95ddddaebede830d9b8d7489c67e4d91a0cbfbdb03b02d337dc8162f15582ad18ee0e953cd517e32f809d533f9ccfb4beee5cb2cba16d0c',
},
});
});
// TODO temporarily disabled
// https://github.com/iotaledger/iota-sdk/issues/647
// it('create a signature unlock', async () => {
// // Verifies that an unlock created in Rust matches that created by the binding when the mnemonic is identical.
// const secretManager = {
// mnemonic:
// 'good reason pipe keen price glory mystery illegal loud isolate wolf trash raise guilt inflict guide modify bachelor length galaxy lottery there mango comfort',
// };
// const preparedTx = sigUnlockPreparedTx as any as PreparedTransactionData;
// const txEssenceHash = Utils.hashTransactionEssence(
// preparedTx.essence,
// );

// if (preparedTx.inputsData[0].chain === undefined) {
// throw 'chain is undefined';
// }

// const unlock = await new SecretManager(secretManager).signatureUnlock(
// txEssenceHash,
// preparedTx.inputsData[0].chain,
// );

// expect(unlock).toStrictEqual({
// type: 0,
// signature: {
// type: 0,
// publicKey:
// '0xb76a23de43b8132ae18a4a479cb158563e76d89bd1e20d3ccdc7fd1db2a009d4',
// signature:
// '0xcd905dae45010980e95ddddaebede830d9b8d7489c67e4d91a0cbfbdb03b02d337dc8162f15582ad18ee0e953cd517e32f809d533f9ccfb4beee5cb2cba16d0c',
// },
// });
// });
});
33 changes: 32 additions & 1 deletion sdk/src/types/block/payload/transaction/essence/regular.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,19 @@ impl RegularTransactionEssenceBuilder {

verify_payload::<true>(&self.payload)?;

#[cfg(feature = "std")]
let creation_time = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.expect("Time went backwards")
.as_nanos() as u64;
// TODO no_std way to have a nanosecond timestamp
// https://github.com/iotaledger/iota-sdk/issues/647
#[cfg(not(feature = "std"))]
let creation_time = 0;

Ok(RegularTransactionEssence {
network_id: self.network_id,
creation_time,
inputs,
inputs_commitment: self.inputs_commitment,
outputs,
Expand Down Expand Up @@ -123,8 +134,19 @@ impl RegularTransactionEssenceBuilder {

verify_payload::<true>(&self.payload)?;

#[cfg(feature = "std")]
let creation_time = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.expect("Time went backwards")
.as_nanos() as u64;
// TODO no_std way to have a nanosecond timestamp
// https://github.com/iotaledger/iota-sdk/issues/647
#[cfg(not(feature = "std"))]
let creation_time = 0;

Ok(RegularTransactionEssence {
network_id: self.network_id,
creation_time,
inputs,
inputs_commitment: self.inputs_commitment,
outputs,
Expand All @@ -145,6 +167,8 @@ pub struct RegularTransactionEssence {
/// The unique value denoting whether the block was meant for mainnet, testnet, or a private network.
#[packable(verify_with = verify_network_id)]
network_id: u64,
/// The time at which this transaction was created by the client. It's a Unix-like timestamp in nanosecond.
creation_time: u64,
#[packable(verify_with = verify_inputs_packable)]
#[packable(unpack_error_with = |e| e.unwrap_item_err_or_else(|p| Error::InvalidInputCount(p.into())))]
inputs: BoxedSlicePrefix<Input, InputCount>,
Expand All @@ -159,7 +183,7 @@ pub struct RegularTransactionEssence {

impl RegularTransactionEssence {
/// The essence kind of a [`RegularTransactionEssence`].
pub const KIND: u8 = 1;
pub const KIND: u8 = 2;

/// Creates a new [`RegularTransactionEssenceBuilder`] to build a [`RegularTransactionEssence`].
pub fn builder(network_id: u64, inputs_commitment: InputsCommitment) -> RegularTransactionEssenceBuilder {
Expand All @@ -171,6 +195,11 @@ impl RegularTransactionEssence {
self.network_id
}

/// Returns the creation time of a [`RegularTransactionEssence`].
pub fn creation_time(&self) -> u64 {
self.creation_time
}

/// Returns the inputs of a [`RegularTransactionEssence`].
pub fn inputs(&self) -> &[Input] {
&self.inputs
Expand Down Expand Up @@ -346,6 +375,7 @@ pub mod dto {
#[serde(rename = "type")]
pub kind: u8,
pub network_id: String,
pub creation_time: u64,
pub inputs: Vec<InputDto>,
pub inputs_commitment: String,
pub outputs: Vec<OutputDto>,
Expand All @@ -358,6 +388,7 @@ pub mod dto {
Self {
kind: RegularTransactionEssence::KIND,
network_id: value.network_id().to_string(),
creation_time: value.creation_time(),
inputs: value.inputs().iter().map(Into::into).collect::<Vec<_>>(),
inputs_commitment: value.inputs_commitment().to_string(),
outputs: value.outputs().iter().map(Into::into).collect::<Vec<_>>(),
Expand Down
4 changes: 2 additions & 2 deletions sdk/tests/types/transaction_essence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn essence_kind() {
#[test]
fn essence_unpack_invalid_kind() {
assert!(matches!(
TransactionEssence::unpack_verified([2u8; 32], &protocol_parameters()),
Err(UnpackError::Packable(Error::InvalidEssenceKind(2)))
TransactionEssence::unpack_verified([3u8; 32], &protocol_parameters()),
Err(UnpackError::Packable(Error::InvalidEssenceKind(3)))
));
}
2 changes: 1 addition & 1 deletion sdk/tests/types/transaction_regular_essence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const ED25519_SIGNATURE: &str = "0xc6a40edf9a089f42c18f4ebccb35fe4b578d93b879e99

#[test]
fn kind() {
assert_eq!(RegularTransactionEssence::KIND, 1);
assert_eq!(RegularTransactionEssence::KIND, 2);
}

#[test]
Expand Down