Skip to content

Commit 92fbd54

Browse files
committed
remove InMemorySignerExt from fiber-lib
1 parent 2cad1f3 commit 92fbd54

File tree

8 files changed

+169
-183
lines changed

8 files changed

+169
-183
lines changed

crates/fiber-lib/src/fiber/channel.rs

Lines changed: 12 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use crate::{
2525
calculate_commitment_tx_fee, calculate_shutdown_tx_fee, calculate_tlc_forward_fee,
2626
shutdown_tx_size,
2727
},
28-
key::blake2b_hash_with_salt,
2928
network::SendOnionPacketCommand,
3029
network::{get_chain_hash, sign_network_message, FiberMessageWithTarget},
3130
types::{
@@ -40,7 +39,7 @@ use crate::{
4039
now_timestamp_as_millis_u64, NetworkServiceEvent,
4140
};
4241
use crate::{debug_event, fiber::types::TxAbort, utils::tx::compute_tx_message};
43-
use ckb_hash::{blake2b_256, new_blake2b};
42+
use ckb_hash::blake2b_256;
4443
use ckb_sdk::{util::blake160, Since, SinceType};
4544
use ckb_types::{
4645
core::{
@@ -52,22 +51,21 @@ use ckb_types::{
5251
H256,
5352
};
5453
use fiber_types::{
55-
AddTlcCommand, AppliedFlags, AwaitingChannelReadyFlags, AwaitingTxSignaturesFlags,
56-
BasicMppPaymentData, ChannelActorData, ChannelAnnouncement, ChannelBasePublicKeys,
57-
ChannelConstraints, ChannelFlags, ChannelOpenRecord, ChannelState, ChannelTlcInfo,
58-
ChannelUpdate, ChannelUpdateChannelFlags, ChannelUpdateMessageFlags, CloseFlags,
59-
CollaboratingFundingTxFlags, CommitmentNumbers, EcdsaSignature, Hash256, InMemorySigner,
60-
InboundTlcStatus, NegotiatingFundingFlags, OutboundTlcStatus, PaymentCustomRecords,
61-
PeeledPaymentOnionPacket, PendingNotifySettleTlc, PrevTlcInfo, Privkey, Pubkey,
62-
PublicChannelInfo, RemoveTlcFulfill, RemoveTlcReason, RetryableTlcOperation, RevocationData,
63-
RevokeAndAck, SettlementData, SettlementTlc, ShutdownInfo, ShuttingDownFlags,
64-
SigningCommitmentFlags, TLCId, TlcErr, TlcErrData, TlcErrPacket, TlcErrorCode, TlcInfo,
65-
TlcStatus, NO_SHARED_SECRET,
54+
blake2b_hash_with_salt, derive_tlc_pubkey, AddTlcCommand, AppliedFlags,
55+
AwaitingChannelReadyFlags, AwaitingTxSignaturesFlags, BasicMppPaymentData, ChannelActorData,
56+
ChannelAnnouncement, ChannelBasePublicKeys, ChannelConstraints, ChannelFlags,
57+
ChannelOpenRecord, ChannelState, ChannelTlcInfo, ChannelUpdate, ChannelUpdateChannelFlags,
58+
ChannelUpdateMessageFlags, CloseFlags, CollaboratingFundingTxFlags, CommitmentNumbers,
59+
EcdsaSignature, Hash256, InMemorySigner, InboundTlcStatus, Musig2Context,
60+
NegotiatingFundingFlags, OutboundTlcStatus, PaymentCustomRecords, PeeledPaymentOnionPacket,
61+
PendingNotifySettleTlc, PrevTlcInfo, Privkey, Pubkey, PublicChannelInfo, RemoveTlcFulfill,
62+
RemoveTlcReason, RetryableTlcOperation, RevocationData, RevokeAndAck, SettlementData,
63+
SettlementTlc, ShutdownInfo, ShuttingDownFlags, SigningCommitmentFlags, TLCId, TlcErr,
64+
TlcErrData, TlcErrPacket, TlcErrorCode, TlcInfo, TlcStatus, NO_SHARED_SECRET,
6665
};
6766
use molecule::prelude::{Builder, Entity};
6867
#[cfg(test)]
6968
use musig2::BinaryEncoding;
70-
use musig2::SecNonceBuilder;
7169
use musig2::{
7270
aggregate_partial_signatures,
7371
errors::{RoundFinalizeError, SigningError, VerifyError},
@@ -3222,24 +3220,6 @@ fn derive_temp_channel_id_from_tlc_key(tlc_basepoint: &Pubkey) -> Hash256 {
32223220
new_channel_id_from_seed(&preimage)
32233221
}
32243222

3225-
pub fn get_commitment_secret(commitment_seed: &[u8; 32], commitment_number: u64) -> [u8; 32] {
3226-
// Note that here, we hold the same assumption to bolts for commitment number,
3227-
// i.e. this number should be in the range [0, 2^48).
3228-
let mut res: [u8; 32] = *commitment_seed;
3229-
for i in 0..48 {
3230-
let bitpos = 47 - i;
3231-
if commitment_number & (1 << bitpos) == (1 << bitpos) {
3232-
res[bitpos / 8] ^= 1 << (bitpos & 7);
3233-
res = blake2b_256(res);
3234-
}
3235-
}
3236-
res
3237-
}
3238-
3239-
pub fn get_commitment_point(commitment_seed: &[u8; 32], commitment_number: u64) -> Pubkey {
3240-
Privkey::from(&get_commitment_secret(commitment_seed, commitment_number)).pubkey()
3241-
}
3242-
32433223
pub(crate) fn get_funding_and_reserved_amount(
32443224
total_amount: u128,
32453225
shutdown_script: &Script,
@@ -7103,119 +7083,3 @@ impl From<&AcceptChannel> for ChannelBasePublicKeys {
71037083
}
71047084
}
71057085
}
7106-
7107-
pub(crate) fn get_tweak_by_commitment_point(commitment_point: &Pubkey) -> [u8; 32] {
7108-
let mut hasher = new_blake2b();
7109-
hasher.update(&commitment_point.serialize());
7110-
let mut result = [0u8; 32];
7111-
hasher.finalize(&mut result);
7112-
result
7113-
}
7114-
7115-
pub(crate) fn derive_private_key(secret: &Privkey, commitment_point: &Pubkey) -> Privkey {
7116-
secret.tweak(get_tweak_by_commitment_point(commitment_point))
7117-
}
7118-
7119-
fn derive_public_key(base_key: &Pubkey, commitment_point: &Pubkey) -> Pubkey {
7120-
base_key.tweak(get_tweak_by_commitment_point(commitment_point))
7121-
}
7122-
7123-
pub(crate) fn derive_tlc_pubkey(base_key: &Pubkey, commitment_point: &Pubkey) -> Pubkey {
7124-
derive_public_key(base_key, commitment_point)
7125-
}
7126-
7127-
pub enum Musig2Context {
7128-
Commitment,
7129-
Revoke,
7130-
}
7131-
7132-
impl std::fmt::Display for Musig2Context {
7133-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
7134-
let context_str = match self {
7135-
Musig2Context::Commitment => "COMMITMENT",
7136-
Musig2Context::Revoke => "REVOKE",
7137-
};
7138-
write!(f, "{}", context_str)
7139-
}
7140-
}
7141-
7142-
/// Extension trait providing methods for `InMemorySigner` that depend on
7143-
/// fiber-lib-only functions (key derivation, ckb_hash, Musig2Context).
7144-
pub trait InMemorySignerExt {
7145-
/// Generate an `InMemorySigner` from a seed.
7146-
fn generate_from_seed(params: &[u8]) -> InMemorySigner;
7147-
/// Get the base public keys for this signer.
7148-
fn get_base_public_keys(&self) -> ChannelBasePublicKeys;
7149-
/// Returns the commitment point for the given commitment number.
7150-
fn get_commitment_point(&self, commitment_number: u64) -> Pubkey;
7151-
/// Returns the commitment secret for the given commitment number.
7152-
fn get_commitment_secret(&self, commitment_number: u64) -> [u8; 32];
7153-
/// Derive the TLC key for the given commitment number.
7154-
fn derive_tlc_key(&self, new_commitment_number: u64) -> Privkey;
7155-
/// Derive a musig2 nonce for the given commitment number and context.
7156-
fn derive_musig2_nonce(&self, commitment_number: u64, context: Musig2Context) -> SecNonce;
7157-
}
7158-
7159-
impl InMemorySignerExt for InMemorySigner {
7160-
fn generate_from_seed(params: &[u8]) -> InMemorySigner {
7161-
let seed = ckb_hash::blake2b_256(params);
7162-
7163-
let commitment_seed = {
7164-
let mut hasher = new_blake2b();
7165-
hasher.update(&seed);
7166-
hasher.update(&b"commitment seed"[..]);
7167-
let mut result = [0u8; 32];
7168-
hasher.finalize(&mut result);
7169-
result
7170-
};
7171-
7172-
let key_derive = |seed: &[u8], info: &[u8]| {
7173-
let result = blake2b_hash_with_salt(seed, info);
7174-
Privkey::from_slice(&result)
7175-
};
7176-
7177-
let funding_key = key_derive(&seed, b"funding key");
7178-
let tlc_base_key = key_derive(funding_key.as_ref(), b"HTLC base key");
7179-
let musig2_base_nonce = key_derive(tlc_base_key.as_ref(), b"musig nocne");
7180-
7181-
InMemorySigner {
7182-
funding_key,
7183-
tlc_base_key,
7184-
musig2_base_nonce,
7185-
commitment_seed,
7186-
}
7187-
}
7188-
7189-
fn get_base_public_keys(&self) -> ChannelBasePublicKeys {
7190-
ChannelBasePublicKeys {
7191-
funding_pubkey: self.funding_key.pubkey(),
7192-
tlc_base_key: self.tlc_base_key.pubkey(),
7193-
}
7194-
}
7195-
7196-
/// Returns the commitment point for the given commitment number.
7197-
///
7198-
/// The commitment point is the public key derived from the commitment seed and the commitment number.
7199-
/// The commitment point is used to derive the pubkeys used in the TLC (htlc and revocation outputs).
7200-
fn get_commitment_point(&self, commitment_number: u64) -> Pubkey {
7201-
get_commitment_point(&self.commitment_seed, commitment_number)
7202-
}
7203-
7204-
fn get_commitment_secret(&self, commitment_number: u64) -> [u8; 32] {
7205-
get_commitment_secret(&self.commitment_seed, commitment_number)
7206-
}
7207-
7208-
fn derive_tlc_key(&self, new_commitment_number: u64) -> Privkey {
7209-
let per_commitment_point = self.get_commitment_point(new_commitment_number);
7210-
derive_private_key(&self.tlc_base_key, &per_commitment_point)
7211-
}
7212-
7213-
fn derive_musig2_nonce(&self, commitment_number: u64, context: Musig2Context) -> SecNonce {
7214-
let commitment_point = self.get_commitment_point(commitment_number);
7215-
let seckey = derive_private_key(&self.musig2_base_nonce, &commitment_point);
7216-
7217-
SecNonceBuilder::new(seckey.as_ref())
7218-
.with_extra_input(&context.to_string())
7219-
.build()
7220-
}
7221-
}

crates/fiber-lib/src/fiber/key.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use ckb_hash::new_blake2b;
21
use serde::{Deserialize, Serialize};
32
use std::{fs, path::Path};
43
use tracing::warn;
@@ -138,12 +137,3 @@ pub(crate) fn read_secret_key(path: &Path) -> Result<Option<KeyPair>, Error> {
138137
.map_err(|_| Error::new(ErrorKind::InvalidData, "invalid secret key data"))
139138
})
140139
}
141-
142-
pub(crate) fn blake2b_hash_with_salt(data: &[u8], salt: &[u8]) -> [u8; 32] {
143-
let mut hasher = new_blake2b();
144-
hasher.update(salt);
145-
hasher.update(data);
146-
let mut result = [0u8; 32];
147-
hasher.finalize(&mut result);
148-
result
149-
}

crates/fiber-lib/src/fiber/network.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ use super::channel::{
5858
};
5959
use super::gossip::{GossipActorMessage, GossipMessageStore, GossipMessageUpdates};
6060
use super::graph::{NetworkGraph, NetworkGraphStateStore, OwnedChannelUpdateEvent};
61-
use super::key::blake2b_hash_with_salt;
6261
use super::types::{
6362
BroadcastMessageWithTimestamp, FiberMessage, ForwardTlcResult, GossipMessage, Init, OpenChannel,
6463
};
@@ -97,12 +96,13 @@ use fiber_types::protocol::AnnouncedNodeName;
9796
#[cfg(any(debug_assertions, test, feature = "bench"))]
9897
use fiber_types::SessionRoute;
9998
use fiber_types::{
100-
AddTlcCommand, AwaitingTxSignaturesFlags, ChannelOpenRecord, ChannelOpeningStatus,
101-
ChannelState, ChannelTlcInfo, CloseFlags, EcdsaSignature, EntityHex, FeatureVector, Hash256,
102-
NodeAnnouncement, PaymentCustomRecords, PaymentStatus, PeeledPaymentOnionPacket,
103-
PersistentNetworkActorState, PrevTlcInfo, Privkey, Pubkey, PublicChannelInfo, RemoveTlcFulfill,
104-
RemoveTlcReason, RetryableTlcOperation, RevocationData, RouterHop, SettlementData,
105-
ShuttingDownFlags, TLCId, TlcErr, TlcErrPacket, TlcErrorCode, TrampolineContext, UdtCfgInfos,
99+
blake2b_hash_with_salt, AddTlcCommand, AwaitingTxSignaturesFlags, ChannelOpenRecord,
100+
ChannelOpeningStatus, ChannelState, ChannelTlcInfo, CloseFlags, EcdsaSignature, EntityHex,
101+
FeatureVector, Hash256, NodeAnnouncement, PaymentCustomRecords, PaymentStatus,
102+
PeeledPaymentOnionPacket, PersistentNetworkActorState, PrevTlcInfo, Privkey, Pubkey,
103+
PublicChannelInfo, RemoveTlcFulfill, RemoveTlcReason, RetryableTlcOperation, RevocationData,
104+
RouterHop, SettlementData, ShuttingDownFlags, TLCId, TlcErr, TlcErrPacket, TlcErrorCode,
105+
TrampolineContext, UdtCfgInfos,
106106
};
107107

108108
pub const FIBER_PROTOCOL_ID: ProtocolId = ProtocolId::new(42);

crates/fiber-lib/src/fiber/tests/channel.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::ckb::tests::test_utils::complete_commitment_tx;
2-
use crate::fiber::channel::InMemorySignerExt;
32
use crate::fiber::channel::{
43
AddTlcResponse, UpdateCommand, MAX_COMMITMENT_DELAY_EPOCHS, MIN_COMMITMENT_DELAY_EPOCHS,
54
XUDT_COMPATIBLE_WITNESS,
@@ -22,8 +21,8 @@ use crate::{
2221
ckb::contracts::{get_cell_deps, Contract},
2322
fiber::{
2423
channel::{
25-
derive_private_key, derive_tlc_pubkey, ChannelActorStateStore, ChannelCommand,
26-
ChannelCommandWithId, RemoveTlcCommand, ShutdownCommand, DEFAULT_COMMITMENT_FEE_RATE,
24+
ChannelActorStateStore, ChannelCommand, ChannelCommandWithId, RemoveTlcCommand,
25+
ShutdownCommand, DEFAULT_COMMITMENT_FEE_RATE,
2726
},
2827
config::DEFAULT_AUTO_ACCEPT_CHANNEL_CKB_FUNDING_AMOUNT,
2928
network::{AcceptChannelCommand, OpenChannelCommand},
@@ -39,9 +38,9 @@ use ckb_types::{
3938
prelude::{AsTransactionBuilder, Builder, Entity, Pack, Unpack},
4039
};
4140
use fiber_types::{
42-
AddTlcCommand, ChannelState, HashAlgorithm, InMemorySigner, NegotiatingFundingFlags,
43-
OutboundTlcStatus, PaymentHopData, PaymentStatus, Privkey, RemoveTlcFulfill, RemoveTlcReason,
44-
TLCId, TlcErrorCode, TlcStatus, NO_SHARED_SECRET,
41+
derive_private_key, derive_tlc_pubkey, AddTlcCommand, ChannelState, HashAlgorithm,
42+
InMemorySigner, NegotiatingFundingFlags, OutboundTlcStatus, PaymentHopData, PaymentStatus,
43+
Privkey, RemoveTlcFulfill, RemoveTlcReason, TLCId, TlcErrorCode, TlcStatus, NO_SHARED_SECRET,
4544
};
4645
use fiber_types::{CloseFlags, FeatureVector};
4746
use musig2::secp::Point;

crates/fiber-lib/src/fiber/tests/settle_tlc_set_command_tests.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Tests for SettleTlcSetCommand
22
3-
use crate::fiber::channel::InMemorySignerExt;
43
use crate::fiber::channel::{ChannelActorState, ChannelActorStateStore};
54
use crate::fiber::settle_tlc_set_command::{SettleTlcSetCommand, TlcSettlement};
65
use crate::fiber::types::{Hash256, HoldTlc, Pubkey, RemoveTlcReason};

crates/fiber-lib/src/store/sample/sample_channel.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use musig2::secp::MaybeScalar;
1212
use musig2::SecNonceBuilder;
1313

1414
use crate::fiber::channel::ChannelActorState;
15-
use crate::fiber::channel::InMemorySignerExt;
1615
use crate::fiber::{
1716
AddTlcCommand, AppliedFlags, ChannelActorData, ChannelAnnouncement, ChannelBasePublicKeys,
1817
ChannelConstraints, ChannelState, ChannelTlcInfo, ChannelUpdate, ChannelUpdateChannelFlags,

crates/fiber-lib/src/store/tests/store.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::fiber::types::new_channel_update_unsigned;
66
use crate::fiber::types::*;
77
#[allow(unused)]
88
use crate::fiber::{
9+
blake2b_hash_with_salt,
910
config::{DEFAULT_TLC_EXPIRY_DELTA, MAX_PAYMENT_TLC_EXPIRY_LIMIT},
1011
graph::*,
1112
payment::{PaymentSessionExt, SendPaymentDataBuilder},
@@ -31,7 +32,6 @@ use crate::time::SystemTime;
3132
use crate::watchtower::*;
3233
#[cfg(not(target_arch = "wasm32"))]
3334
use ckb_hash::blake2b_256;
34-
use ckb_hash::new_blake2b;
3535
use ckb_types::packed::*;
3636
use ckb_types::prelude::*;
3737
use ckb_types::H256;
@@ -468,14 +468,6 @@ fn test_channel_state_serialize() {
468468
assert_eq!(flags, new_flags);
469469
}
470470

471-
fn blake2b_hash_with_salt(data: &[u8], salt: &[u8]) -> [u8; 32] {
472-
let mut hasher = new_blake2b();
473-
hasher.update(salt);
474-
hasher.update(data);
475-
let mut result = [0u8; 32];
476-
hasher.finalize(&mut result);
477-
result
478-
}
479471
#[cfg(not(target_arch = "wasm32"))]
480472
#[cfg_attr(not(target_arch = "wasm32"), test)]
481473
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]

0 commit comments

Comments
 (0)