Skip to content

Commit

Permalink
Merge pull request #38 from LedgerHQ/y333_241016/update_after_product…
Browse files Browse the repository at this point in the history
…_review

DEPLOY_ACCOUNT Tx clear-signing support (v1 & v3)
  • Loading branch information
yogh333 authored Oct 25, 2024
2 parents d64cf9f + 2dbdd50 commit 65325f9
Show file tree
Hide file tree
Showing 141 changed files with 1,285 additions and 188 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions samples/apdu/tx_deploy_account_v1.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
=> 5a0600001880000a55c741e9c9c47a6028800000008000000000000000
=> 5a060100a0016d12c2c06057995bd961c0eb75ffdbf6710f9776abaa1278234ed4d43bccc901c0bb51e2ce73dc007601a1e7725453627254016c28f118251a71bbb0507fcb01f0bb51e2ce73dc007601a1e77254536272540162a8c118251a71bbb0507faf00000000000000000000000000000000000000000000000000534e5f4d41494e0000000000000000000000000000000000000000000000000000000000000000
=> 5a06020020000000000000000000000000000000000000000000000000000000e8d4a51000
=> 5a060300200000000000000000000000000000000000000000000000000000000000000002
=> 5a0604004000ddfe9f9ebc9dd230f906008e54c8b56994250505a13737609ed57f4e4d79360000000000000000000000000000000000000000000000000000000000000000
47 changes: 47 additions & 0 deletions samples/apdu/tx_deploy_account_v1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[
{
"header": {
"cla": 90,
"ins": 6,
"p1": 0,
"p2": 0
},
"data": "80000a55c741e9c9c47a6028800000008000000000000000"
},
{
"header": {
"cla": 90,
"ins": 6,
"p1": 1,
"p2": 0
},
"data": "016d12c2c06057995bd961c0eb75ffdbf6710f9776abaa1278234ed4d43bccc901c0bb51e2ce73dc007601a1e7725453627254016c28f118251a71bbb0507fcb01f0bb51e2ce73dc007601a1e77254536272540162a8c118251a71bbb0507faf00000000000000000000000000000000000000000000000000534e5f4d41494e0000000000000000000000000000000000000000000000000000000000000000"
},
{
"header": {
"cla": 90,
"ins": 6,
"p1": 2,
"p2": 0
},
"data": "000000000000000000000000000000000000000000000000000000e8d4a51000"
},
{
"header": {
"cla": 90,
"ins": 6,
"p1": 3,
"p2": 0
},
"data": "0000000000000000000000000000000000000000000000000000000000000002"
},
{
"header": {
"cla": 90,
"ins": 6,
"p1": 4,
"p2": 0
},
"data": "00ddfe9f9ebc9dd230f906008e54c8b56994250505a13737609ed57f4e4d79360000000000000000000000000000000000000000000000000000000000000000"
}
]
6 changes: 6 additions & 0 deletions samples/apdu/tx_deploy_account_v3.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
=> 5a0500001880000a55c741e9c9c47a6028800000008000000000000000
=> 5a050100c0016d12c2c06057995bd961c0eb75ffdbf6710f9776abaa1278234ed4d43bccc900000000000000000000000000000000000000000000000000534e5f4d41494e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001c0bb51e2ce73dc007601a1e7725453627254016c28f118251a71bbb0507fcb01f0bb51e2ce73dc007601a1e77254536272540162a8c118251a71bbb0507faf
=> 5a05020060000000000000000000000000000000000000000000000000000000000000000000004c315f47415300000000000000000000000000000000000000000000000000004c325f474153000000000000000000000000000000000000000000000000
=> 5a05030000
=> 5a050400200000000000000000000000000000000000000000000000000000000000000002
=> 5a0505004000ddfe9f9ebc9dd230f906008e54c8b56994250505a13737609ed57f4e4d79360000000000000000000000000000000000000000000000000000000000000000
56 changes: 56 additions & 0 deletions samples/apdu/tx_deploy_account_v3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
[
{
"header": {
"cla": 90,
"ins": 5,
"p1": 0,
"p2": 0
},
"data": "80000a55c741e9c9c47a6028800000008000000000000000"
},
{
"header": {
"cla": 90,
"ins": 5,
"p1": 1,
"p2": 0
},
"data": "016d12c2c06057995bd961c0eb75ffdbf6710f9776abaa1278234ed4d43bccc900000000000000000000000000000000000000000000000000534e5f4d41494e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001c0bb51e2ce73dc007601a1e7725453627254016c28f118251a71bbb0507fcb01f0bb51e2ce73dc007601a1e77254536272540162a8c118251a71bbb0507faf"
},
{
"header": {
"cla": 90,
"ins": 5,
"p1": 2,
"p2": 0
},
"data": "000000000000000000000000000000000000000000000000000000000000000000004c315f47415300000000000000000000000000000000000000000000000000004c325f474153000000000000000000000000000000000000000000000000"
},
{
"header": {
"cla": 90,
"ins": 5,
"p1": 3,
"p2": 0
},
"data": ""
},
{
"header": {
"cla": 90,
"ins": 5,
"p1": 4,
"p2": 0
},
"data": "0000000000000000000000000000000000000000000000000000000000000002"
},
{
"header": {
"cla": 90,
"ins": 5,
"p1": 5,
"p2": 0
},
"data": "00ddfe9f9ebc9dd230f906008e54c8b56994250505a13737609ed57f4e4d79360000000000000000000000000000000000000000000000000000000000000000"
}
]
15 changes: 15 additions & 0 deletions samples/tx/tx_deploy_account_v1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"url": "https://starkscan.co/tx/0x034a78c29cc215a395345350b4ac6d0a24e4831c87ded164edf4435b159e9d1d",
"version": 1,
"contract_address": "0x016d12c2c06057995bd961c0eb75ffdbf6710f9776abaa1278234ed4d43bccc9",
"class_hash": "0x01c0bb51e2ce73dc007601a1e7725453627254016c28f118251a71bbb0507fcb",
"max_fee": "1000000000000",
"chain_id": "0x534e5f4d41494e",
"nonce": "0",
"contract_address_salt": "0x01f0bb51e2ce73dc007601a1e77254536272540162a8c118251a71bbb0507faf",
"constructor_calldata": [
"0xddfe9f9ebc9dd230f906008e54c8b56994250505a13737609ed57f4e4d7936",
"0x0"
],
"dpath": "m/2645'/1195502025'/1148870696'/0'/0'/0"
}
19 changes: 19 additions & 0 deletions samples/tx/tx_deploy_account_v3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"url": "",
"version": 3,
"contract_address": "0x016d12c2c06057995bd961c0eb75ffdbf6710f9776abaa1278234ed4d43bccc9",
"tip": "0",
"l1_gas_bounds": "0x00004C315F474153000000000000000000000000000000000000000000000000",
"l2_gas_bounds": "0x00004C325F474153000000000000000000000000000000000000000000000000",
"paymaster_data": [],
"chain_id": "0x534e5f4d41494e",
"nonce": "0",
"data_availability_mode": "0",
"class_hash": "0x01c0bb51e2ce73dc007601a1e7725453627254016c28f118251a71bbb0507fcb",
"contract_address_salt": "0x01f0bb51e2ce73dc007601a1e77254536272540162a8c118251a71bbb0507faf",
"constructor_calldata": [
"0xddfe9f9ebc9dd230f906008e54c8b56994250505a13737609ed57f4e4d7936",
"0x0"
],
"dpath": "m/2645'/1195502025'/1148870696'/0'/0'/0"
}
6 changes: 3 additions & 3 deletions starknet/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[package]
name = "starknet"
version = "2.1.1"
version = "2.2.0"
edition = "2021"
authors = ["Ledger"]

[dependencies]
ledger_device_sdk = "1.17.3"
ledger_secure_sdk_sys = { version = "1.5.0", features = ["heap"]}
ledger_device_sdk = "1.18.1"
ledger_secure_sdk_sys = { version = "1.5.1", features = ["heap"]}
include_gif = "1.2.0"
hex = { version = "0.4", default-features = false, features = ["alloc"]}
num-bigint = { version = "0.4", default-features = false }
Expand Down
60 changes: 27 additions & 33 deletions starknet/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,8 @@ pub struct Call {
pub calldata: Vec<FieldElement>,
}

impl Call {
pub fn reset(&mut self) {
self.to.clear();
self.selector.clear();
for c in self.calldata.iter_mut() {
c.clear();
}
}
}

#[derive(Default, Debug)]
pub struct Transaction {
pub struct InvokeTransaction {
pub version: FieldElement,
pub sender_address: FieldElement,
pub tip: FieldElement,
Expand All @@ -36,27 +26,29 @@ pub struct Transaction {
pub calls: Vec<Call>,
}

impl Transaction {
pub fn reset(&mut self) {
self.version.clear();
self.sender_address.clear();
self.tip.clear();
self.max_fee.clear();
self.l1_gas_bounds.clear();
self.l2_gas_bounds.clear();
self.chain_id.clear();
self.nonce.clear();
self.data_availability_mode.clear();
for c in self.paymaster_data.iter_mut() {
c.clear();
}
for c in self.account_deployment_data.iter_mut() {
c.clear();
}
for c in self.calls.iter_mut() {
c.reset();
}
}
#[derive(Default, Debug)]
pub struct DeployAccountTransaction {
pub version: FieldElement,
pub contract_address: FieldElement,
pub tip: FieldElement,
pub max_fee: FieldElement,
pub l1_gas_bounds: FieldElement,
pub l2_gas_bounds: FieldElement,
pub paymaster_data: Vec<FieldElement>,
pub chain_id: FieldElement,
pub nonce: FieldElement,
pub data_availability_mode: FieldElement,
pub class_hash: FieldElement,
pub contract_address_salt: FieldElement,
pub constructor_calldata: Vec<FieldElement>,
}

#[derive(Default, Debug)]
pub enum Transaction {
#[default]
None,
Invoke(InvokeTransaction),
DeployAccount(DeployAccountTransaction),
}

pub enum RequestType {
Expand All @@ -66,6 +58,8 @@ pub enum RequestType {
SignHash,
SignTx,
SignTxV1,
SignDeployAccount,
SignDeployAccountV1,
}

#[derive(Default, Debug)]
Expand Down Expand Up @@ -116,7 +110,7 @@ impl Ctx {
pub fn reset(&mut self) {
self.req_type = RequestType::Unknown;
self.bip32_path.fill(0);
self.tx.reset();
self.tx = Transaction::default();
self.hash.reset();
}
}
82 changes: 81 additions & 1 deletion starknet/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use ledger_device_sdk::io::{Reply, SyscallError};
pub mod pedersen;
pub mod poseidon;

use crate::context::{Ctx, Transaction};
use crate::context::{Ctx, DeployAccountTransaction, InvokeTransaction, Transaction};
use crate::types::FieldElement;

/// Length in bytes of an EIP-2645 derivation path (without m), e.g m/2645'/1195502025'/1148870696'/0'/0'/0
Expand Down Expand Up @@ -176,6 +176,14 @@ fn convert_der_to_rs<const R: usize, const S: usize>(
}

pub fn tx_hash(tx: &Transaction) -> FieldElement {
match tx {
Transaction::Invoke(tx) => invoke_tx_hash(tx),
Transaction::DeployAccount(tx) => deploy_account_tx_hash(tx),
Transaction::None => FieldElement::ZERO,
}
}

fn invoke_tx_hash(tx: &InvokeTransaction) -> FieldElement {
match tx.version.into() {
1u8 => {
let mut hasher = pedersen::PedersenHasher::new();
Expand Down Expand Up @@ -261,3 +269,75 @@ pub fn tx_hash(tx: &Transaction) -> FieldElement {
_ => panic!("Invalid version"),
}
}

fn deploy_account_tx_hash(tx: &DeployAccountTransaction) -> FieldElement {
match tx.version.into() {
1u8 => {
let mut hasher = pedersen::PedersenHasher::new();
/* "deploy_account" */
hasher.update(FieldElement::DEPLOY_ACCOUNT);
/* version */
hasher.update(tx.version);
/* contract_address */
hasher.update(tx.contract_address);
/* 0 */
hasher.update(FieldElement::ZERO);
/* h(class_hash, contract_address_salt, constructor_calldata) */
let mut hasher_temp = pedersen::PedersenHasher::new();
hasher_temp.update(tx.class_hash);
hasher_temp.update(tx.contract_address_salt);
for d in &tx.constructor_calldata {
hasher_temp.update(*d);
}
hasher_temp.update(FieldElement::from(2usize + tx.constructor_calldata.len()));
let hash_temp = hasher_temp.finalize();
hasher.update(hash_temp);
/* max fee */
hasher.update(tx.max_fee);
/* chain_id */
hasher.update(tx.chain_id);
/* nonce */
hasher.update(tx.nonce);

hasher.update(FieldElement::from(8u8));

hasher.finalize()
}
3u8 => {
let mut hasher = poseidon::PoseidonHasher::new();
/* "deploy_account" */
hasher.update(FieldElement::DEPLOY_ACCOUNT);
/* version */
hasher.update(tx.version);
/* contract_address */
hasher.update(tx.contract_address);
/* h(tip, l1_gas_bounds, l2_gas_bounds) */
let fee_hash = poseidon::PoseidonStark252::hash_many(&[
tx.tip,
tx.l1_gas_bounds,
tx.l2_gas_bounds,
]);
hasher.update(fee_hash);
/* h(paymaster_data) */
let paymaster_hash = poseidon::PoseidonStark252::hash_many(&tx.paymaster_data);
hasher.update(paymaster_hash);
/* chain_id */
hasher.update(tx.chain_id);
/* nonce */
hasher.update(tx.nonce);
/* data_availability_modes */
hasher.update(tx.data_availability_mode);
/* h(constructor_calldata) */
let constructor_calldata_hash =
poseidon::PoseidonStark252::hash_many(&tx.constructor_calldata);
hasher.update(constructor_calldata_hash);
/* class_hash */
hasher.update(tx.class_hash);
/* contract_address_salt */
hasher.update(tx.contract_address_salt);

hasher.finalize()
}
_ => panic!("Invalid version"),
}
}
Loading

0 comments on commit 65325f9

Please sign in to comment.