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

asset-security -> staging #4250

Merged
merged 1 commit into from
Sep 1, 2023
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
10 changes: 5 additions & 5 deletions benchmarks/benches/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use criterion::{criterion_group, criterion_main, BatchSize, Criterion};
use ironfish::{
assets::{asset::Asset, asset_identifier::NATIVE_ASSET},
test_util::make_fake_witness,
transaction::{batch_verify_transactions, verify_transaction},
transaction::{batch_verify_transactions, verify_transaction, TransactionVersion},
Note, ProposedTransaction, SaplingKey, Transaction,
};

Expand All @@ -24,7 +24,7 @@ pub fn simple(c: &mut Criterion) {
},
// Benchmark
|(key, spend_note, witness, out_note)| {
let mut proposed = ProposedTransaction::new(key);
let mut proposed = ProposedTransaction::new(key, TransactionVersion::latest());

proposed.add_spend(spend_note, &witness).unwrap();
proposed.add_output(out_note).unwrap();
Expand Down Expand Up @@ -60,7 +60,7 @@ pub fn all_descriptions(c: &mut Criterion) {
|(key, spend_note, witness, out_note, asset)| {
let asset_value = 10;

let mut proposed = ProposedTransaction::new(key);
let mut proposed = ProposedTransaction::new(key, TransactionVersion::latest());

proposed.add_spend(spend_note, &witness).unwrap();
proposed.add_output(out_note).unwrap();
Expand Down Expand Up @@ -92,7 +92,7 @@ pub fn verify(c: &mut Criterion) {

let out_note = Note::new(public_address, 41, "", NATIVE_ASSET, public_address);

let mut proposed = ProposedTransaction::new(key);
let mut proposed = ProposedTransaction::new(key, TransactionVersion::latest());

proposed.add_spend(spend_note, &witness).unwrap();
proposed.add_output(out_note).unwrap();
Expand Down Expand Up @@ -127,7 +127,7 @@ pub fn batch_verify(c: &mut Criterion) {

let out_note = Note::new(public_address, 41, "", NATIVE_ASSET, public_address);

let mut proposed = ProposedTransaction::new(key);
let mut proposed = ProposedTransaction::new(key, TransactionVersion::latest());

proposed.add_spend(spend_note, &witness).unwrap();
proposed.add_output(out_note).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions ironfish-rust-nodejs/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const TRANSACTION_SIGNATURE_LENGTH: number
export const TRANSACTION_PUBLIC_KEY_RANDOMNESS_LENGTH: number
export const TRANSACTION_EXPIRATION_LENGTH: number
export const TRANSACTION_FEE_LENGTH: number
export const TRANSACTION_VERSION: number
export const LATEST_TRANSACTION_VERSION: number
export function verifyTransactions(serializedTransactions: Array<Buffer>): boolean
export const enum LanguageCode {
English = 0,
Expand Down Expand Up @@ -165,7 +165,7 @@ export class TransactionPosted {
}
export type NativeTransaction = Transaction
export class Transaction {
constructor(spenderHexKey: string, version?: number | undefined | null)
constructor(spenderHexKey: string, version: number)
/** Create a proof of a new note owned by the recipient in this transaction. */
output(note: Note): void
/** Spend the note owned by spender_hex_key at the given witness location. */
Expand Down
4 changes: 2 additions & 2 deletions ironfish-rust-nodejs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ if (!nativeBinding) {
throw new Error(`Failed to load native binding`)
}

const { contribute, verifyTransform, KEY_LENGTH, NONCE_LENGTH, BoxKeyPair, randomBytes, boxMessage, unboxMessage, RollingFilter, initSignalHandler, triggerSegfault, ASSET_ID_LENGTH, ASSET_METADATA_LENGTH, ASSET_NAME_LENGTH, ASSET_LENGTH, Asset, NOTE_ENCRYPTION_KEY_LENGTH, MAC_LENGTH, ENCRYPTED_NOTE_PLAINTEXT_LENGTH, ENCRYPTED_NOTE_LENGTH, NoteEncrypted, PUBLIC_ADDRESS_LENGTH, RANDOMNESS_LENGTH, MEMO_LENGTH, AMOUNT_VALUE_LENGTH, DECRYPTED_NOTE_LENGTH, Note, PROOF_LENGTH, TRANSACTION_SIGNATURE_LENGTH, TRANSACTION_PUBLIC_KEY_RANDOMNESS_LENGTH, TRANSACTION_EXPIRATION_LENGTH, TRANSACTION_FEE_LENGTH, TRANSACTION_VERSION, TransactionPosted, Transaction, verifyTransactions, LanguageCode, generateKey, spendingKeyToWords, wordsToSpendingKey, generateKeyFromPrivateKey, initializeSapling, FoundBlockResult, ThreadPoolHandler, isValidPublicAddress } = nativeBinding
const { contribute, verifyTransform, KEY_LENGTH, NONCE_LENGTH, BoxKeyPair, randomBytes, boxMessage, unboxMessage, RollingFilter, initSignalHandler, triggerSegfault, ASSET_ID_LENGTH, ASSET_METADATA_LENGTH, ASSET_NAME_LENGTH, ASSET_LENGTH, Asset, NOTE_ENCRYPTION_KEY_LENGTH, MAC_LENGTH, ENCRYPTED_NOTE_PLAINTEXT_LENGTH, ENCRYPTED_NOTE_LENGTH, NoteEncrypted, PUBLIC_ADDRESS_LENGTH, RANDOMNESS_LENGTH, MEMO_LENGTH, AMOUNT_VALUE_LENGTH, DECRYPTED_NOTE_LENGTH, Note, PROOF_LENGTH, TRANSACTION_SIGNATURE_LENGTH, TRANSACTION_PUBLIC_KEY_RANDOMNESS_LENGTH, TRANSACTION_EXPIRATION_LENGTH, TRANSACTION_FEE_LENGTH, LATEST_TRANSACTION_VERSION, TransactionPosted, Transaction, verifyTransactions, LanguageCode, generateKey, spendingKeyToWords, wordsToSpendingKey, generateKeyFromPrivateKey, initializeSapling, FoundBlockResult, ThreadPoolHandler, isValidPublicAddress } = nativeBinding

module.exports.contribute = contribute
module.exports.verifyTransform = verifyTransform
Expand Down Expand Up @@ -286,7 +286,7 @@ module.exports.TRANSACTION_SIGNATURE_LENGTH = TRANSACTION_SIGNATURE_LENGTH
module.exports.TRANSACTION_PUBLIC_KEY_RANDOMNESS_LENGTH = TRANSACTION_PUBLIC_KEY_RANDOMNESS_LENGTH
module.exports.TRANSACTION_EXPIRATION_LENGTH = TRANSACTION_EXPIRATION_LENGTH
module.exports.TRANSACTION_FEE_LENGTH = TRANSACTION_FEE_LENGTH
module.exports.TRANSACTION_VERSION = TRANSACTION_VERSION
module.exports.LATEST_TRANSACTION_VERSION = LATEST_TRANSACTION_VERSION
module.exports.TransactionPosted = TransactionPosted
module.exports.Transaction = Transaction
module.exports.verifyTransactions = verifyTransactions
Expand Down
31 changes: 7 additions & 24 deletions ironfish-rust-nodejs/src/structs/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@ use ironfish::transaction::{
batch_verify_transactions, TransactionVersion, TRANSACTION_EXPIRATION_SIZE,
TRANSACTION_FEE_SIZE, TRANSACTION_PUBLIC_KEY_SIZE, TRANSACTION_SIGNATURE_SIZE,
};
use ironfish::{
MerkleNoteHash, ProposedTransaction, PublicAddress, SaplingKey, Transaction,
TRANSACTION_VERSION as TX_VERSION,
};
use ironfish::{MerkleNoteHash, ProposedTransaction, PublicAddress, SaplingKey, Transaction};
use napi::{
bindgen_prelude::{i64n, BigInt, Buffer, Env, Error, Object, Result, Undefined},
bindgen_prelude::{i64n, BigInt, Buffer, Env, Object, Result, Undefined},
JsBuffer,
};
use napi_derive::napi;
Expand Down Expand Up @@ -44,7 +41,7 @@ pub const TRANSACTION_EXPIRATION_LENGTH: u32 = TRANSACTION_EXPIRATION_SIZE as u3
pub const TRANSACTION_FEE_LENGTH: u32 = TRANSACTION_FEE_SIZE as u32;

#[napi]
pub const TRANSACTION_VERSION: u8 = TX_VERSION.as_u8();
pub const LATEST_TRANSACTION_VERSION: u8 = TransactionVersion::latest() as u8;

#[napi(js_name = "TransactionPosted")]
pub struct NativeTransactionPosted {
Expand Down Expand Up @@ -170,26 +167,12 @@ pub struct NativeTransaction {
#[napi]
impl NativeTransaction {
#[napi(constructor)]
pub fn new(spender_hex_key: String, version: Option<u8>) -> Result<Self> {
pub fn new(spender_hex_key: String, version: u8) -> Result<Self> {
let spender_key = SaplingKey::from_hex(&spender_hex_key).map_err(to_napi_err)?;
let transaction = match version {
None => ProposedTransaction::new(spender_key),
Some(version) => {
let version = TransactionVersion::from_u8(version)
.ok_or_else(|| Error::from_reason(format!("unsupported version: {version}")))?;
ProposedTransaction::with_version(spender_key, version)
}
};
Ok(NativeTransaction { transaction })
}
let tx_version = version.try_into().map_err(to_napi_err)?;
let transaction = ProposedTransaction::new(spender_key, tx_version);

pub fn with_version(spender_hex_key: String, version: u8) -> Result<Self> {
let spender_key = SaplingKey::from_hex(&spender_hex_key).map_err(to_napi_err)?;
let version = TransactionVersion::from_u8(version)
.ok_or_else(|| Error::from_reason(format!("unsupported version: {version}")))?;
Ok(NativeTransaction {
transaction: ProposedTransaction::with_version(spender_key, version),
})
Ok(NativeTransaction { transaction })
}

/// Create a proof of a new note owned by the recipient in this transaction.
Expand Down
8 changes: 4 additions & 4 deletions ironfish-rust-nodejs/tests/demo.test.slow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

import { Asset, DECRYPTED_NOTE_LENGTH, initSignalHandler, LanguageCode, spendingKeyToWords, verifyTransactions, wordsToSpendingKey } from '..'
import { Asset, DECRYPTED_NOTE_LENGTH, initSignalHandler, LanguageCode, LATEST_TRANSACTION_VERSION, spendingKeyToWords, verifyTransactions, wordsToSpendingKey } from '..'
import {
initializeSapling,
generateKey,
Expand Down Expand Up @@ -58,7 +58,7 @@ describe('Demonstrate the Sapling API', () => {
it(`Should create a miner's fee transaction`, () => {
const key = generateKey()

const transaction = new Transaction(key.spendingKey)
const transaction = new Transaction(key.spendingKey, LATEST_TRANSACTION_VERSION)
const note = new Note(key.publicAddress, 20n, 'test', Asset.nativeId(), key.publicAddress)
transaction.output(note)

Expand Down Expand Up @@ -95,13 +95,13 @@ describe('Demonstrate the Sapling API', () => {
const key = generateKey()
const recipientKey = generateKey()

const minersFeeTransaction = new Transaction(key.spendingKey)
const minersFeeTransaction = new Transaction(key.spendingKey, LATEST_TRANSACTION_VERSION)
const minersFeeNote = new Note(key.publicAddress, 20n, 'miner', Asset.nativeId(), key.publicAddress)
minersFeeTransaction.output(minersFeeNote)

const postedMinersFeeTransaction = new TransactionPosted(minersFeeTransaction.post_miners_fee())

const transaction = new Transaction(key.spendingKey)
const transaction = new Transaction(key.spendingKey, LATEST_TRANSACTION_VERSION)
transaction.setExpiration(10)
const encryptedNote = new NoteEncrypted(postedMinersFeeTransaction.getNote(0))
const decryptedNote = Note.deserialize(encryptedNote.decryptNoteForOwner(key.incomingViewKey)!)
Expand Down
1 change: 0 additions & 1 deletion ironfish-rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ pub use {
note::Note,
transaction::{
outputs::OutputDescription, spends::SpendDescription, ProposedTransaction, Transaction,
TRANSACTION_VERSION,
},
};

Expand Down
9 changes: 2 additions & 7 deletions ironfish-rust/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ pub use version::TransactionVersion;

const SIGNATURE_HASH_PERSONALIZATION: &[u8; 8] = b"IFsighsh";
const TRANSACTION_SIGNATURE_VERSION: &[u8; 1] = &[0];
pub const TRANSACTION_VERSION: TransactionVersion = TransactionVersion::V1;
pub const TRANSACTION_SIGNATURE_SIZE: usize = 64;
pub const TRANSACTION_PUBLIC_KEY_SIZE: usize = 32;
pub const TRANSACTION_EXPIRATION_SIZE: usize = 4;
Expand Down Expand Up @@ -123,12 +122,8 @@ pub struct ProposedTransaction {
}

impl ProposedTransaction {
pub fn new(spender_key: SaplingKey) -> Self {
Self::with_version(spender_key, TRANSACTION_VERSION)
}

pub fn with_version(spender_key: SaplingKey, version: TransactionVersion) -> Self {
ProposedTransaction {
pub fn new(spender_key: SaplingKey, version: TransactionVersion) -> Self {
Self {
version,
spends: vec![],
outputs: vec![],
Expand Down
22 changes: 12 additions & 10 deletions ironfish-rust/src/transaction/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fn test_transaction() {
spender_key.public_address(),
);

let mut transaction = ProposedTransaction::new(spender_key);
let mut transaction = ProposedTransaction::new(spender_key, TransactionVersion::latest());

// Spend
transaction.add_spend(in_note, &witness).unwrap();
Expand Down Expand Up @@ -175,7 +175,7 @@ fn test_transaction_simple() {
);
let witness = make_fake_witness(&in_note);

let mut transaction = ProposedTransaction::new(spender_key);
let mut transaction = ProposedTransaction::new(spender_key, TransactionVersion::latest());
transaction.add_spend(in_note, &witness).unwrap();
assert_eq!(transaction.spends.len(), 1);
transaction.add_output(out_note).unwrap();
Expand Down Expand Up @@ -211,7 +211,7 @@ fn test_miners_fee() {
NATIVE_ASSET,
spender_key.public_address(),
);
let mut transaction = ProposedTransaction::new(spender_key);
let mut transaction = ProposedTransaction::new(spender_key, TransactionVersion::latest());
transaction.add_output(out_note).unwrap();
let posted_transaction = transaction
.post_miners_fee()
Expand All @@ -236,7 +236,7 @@ fn test_transaction_signature() {
let receiver_address = receiver_key.public_address();
let sender_key = SaplingKey::generate_key();

let mut transaction = ProposedTransaction::new(spender_key);
let mut transaction = ProposedTransaction::new(spender_key, TransactionVersion::latest());
let in_note = Note::new(
spender_address,
42,
Expand Down Expand Up @@ -290,7 +290,7 @@ fn test_transaction_created_with_version_1() {
);
let witness = make_fake_witness(&in_note);

let mut transaction = ProposedTransaction::new(spender_key);
let mut transaction = ProposedTransaction::new(spender_key, TransactionVersion::V1);
transaction.add_spend(in_note, &witness).unwrap();
transaction.add_output(out_note).unwrap();

Expand Down Expand Up @@ -354,7 +354,7 @@ fn test_transaction_value_overflows() {
);
let witness = make_fake_witness(&note);

let mut tx = ProposedTransaction::new(key);
let mut tx = ProposedTransaction::new(key, TransactionVersion::latest());

// spend
assert!(tx.add_spend(note.clone(), &witness).is_err());
Expand Down Expand Up @@ -458,7 +458,7 @@ fn test_batch_verify_wrong_params() {
key.public_address(),
);

let mut proposed_transaction1 = ProposedTransaction::new(key);
let mut proposed_transaction1 = ProposedTransaction::new(key, TransactionVersion::latest());

proposed_transaction1.add_spend(in_note, &witness).unwrap();
proposed_transaction1.add_output(out_note).unwrap();
Expand All @@ -474,7 +474,8 @@ fn test_batch_verify_wrong_params() {
.post(None, 1)
.expect("should be able to post transaction");

let mut proposed_transaction2 = ProposedTransaction::new(other_key);
let mut proposed_transaction2 =
ProposedTransaction::new(other_key, TransactionVersion::latest());
proposed_transaction2.add_mint(asset2, 5).unwrap();

let transaction2 = proposed_transaction2.post(None, 0).unwrap();
Expand Down Expand Up @@ -552,7 +553,7 @@ fn test_batch_verify() {
key.public_address(),
);

let mut proposed_transaction1 = ProposedTransaction::new(key);
let mut proposed_transaction1 = ProposedTransaction::new(key, TransactionVersion::latest());

proposed_transaction1.add_spend(in_note, &witness).unwrap();
proposed_transaction1.add_output(out_note).unwrap();
Expand All @@ -568,7 +569,8 @@ fn test_batch_verify() {
.post(None, 1)
.expect("should be able to post transaction");

let mut proposed_transaction2 = ProposedTransaction::new(other_key);
let mut proposed_transaction2 =
ProposedTransaction::new(other_key, TransactionVersion::latest());
proposed_transaction2.add_mint(asset2, 5).unwrap();

let transaction2 = proposed_transaction2.post(None, 0).unwrap();
Expand Down
4 changes: 4 additions & 0 deletions ironfish-rust/src/transaction/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ impl TransactionVersion {
}
}

pub const fn latest() -> Self {
Self::V2
}

pub fn write<W: io::Write>(&self, mut writer: W) -> Result<(), IronfishError> {
writer.write_u8((*self).into())?;
Ok(())
Expand Down
Loading