breez_sdk_liquid/sync/model/
client.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use crate::{
    prelude::{Signer, SignerError},
    utils,
};
use anyhow::Result;
use log::trace;
use lwk_wollet::hashes::hex::DisplayHex as _;
use sdk_common::bitcoin::hashes::{sha256, Hash};
use sdk_common::utils::Arc;

use super::{
    ListChangesRequest, ListenChangesRequest, Record, SetRecordRequest, CURRENT_SCHEMA_VERSION,
    MESSAGE_PREFIX,
};

fn sign_message(msg: &[u8], signer: Arc<Box<dyn Signer>>) -> Result<String, SignerError> {
    let msg = [MESSAGE_PREFIX, msg].concat();
    trace!("About to compute sha256 hash of msg: {msg:?}");
    let digest = sha256::Hash::hash(&sha256::Hash::hash(&msg));
    trace!("About to sign digest: {digest:?}");
    signer
        .sign_ecdsa_recoverable(digest.to_vec())
        .map(|bytes| zbase32::encode_full_bytes(&bytes))
}

impl ListChangesRequest {
    pub(crate) fn new(since_revision: u64, signer: Arc<Box<dyn Signer>>) -> Result<Self> {
        let request_time = utils::now();
        let msg = format!("{}-{}", since_revision, request_time);
        let signature = sign_message(msg.as_bytes(), signer)?;
        Ok(Self {
            since_revision,
            request_time,
            signature,
        })
    }
}
impl SetRecordRequest {
    pub(crate) fn new(
        record: Record,
        request_time: u32,
        signer: Arc<Box<dyn Signer>>,
        client_id: String,
    ) -> Result<Self, SignerError> {
        let msg = format!(
            "{}-{}-{}-{}-{}",
            record.id,
            record.data.to_lower_hex_string(),
            record.revision,
            *CURRENT_SCHEMA_VERSION,
            request_time,
        );
        trace!("About to sign message: {}", msg);
        let signature = sign_message(msg.as_bytes(), signer)?;
        trace!("Got signature: {}", signature);
        Ok(Self {
            record: Some(record),
            request_time,
            signature,
            client_id: Some(client_id),
        })
    }
}
impl ListenChangesRequest {
    pub(crate) fn new(signer: Arc<Box<dyn Signer>>) -> Result<Self, SignerError> {
        let request_time = utils::now();
        let msg = format!("{}", request_time);
        let signature = sign_message(msg.as_bytes(), signer)?;
        Ok(Self {
            request_time,
            signature,
        })
    }
}