Skip to content

Commit

Permalink
[Framework] Unify auth payload to use bitcoin consensus codec (#2580)
Browse files Browse the repository at this point in the history
resolve auth payload to use bitcoin consensus codec
  • Loading branch information
baichuan3 authored Sep 5, 2024
1 parent 1d1ad88 commit f765347
Show file tree
Hide file tree
Showing 11 changed files with 329 additions and 296 deletions.
27 changes: 25 additions & 2 deletions crates/rooch-types/src/framework/auth_payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
transaction::RoochTransactionData,
};
use anyhow::{ensure, Result};
use bitcoin::consensus::{Decodable, Encodable};
use fastcrypto::{
hash::Sha256,
secp256k1::{Secp256k1PublicKey, Secp256k1Signature},
Expand All @@ -18,11 +19,12 @@ use moveos_types::{
state::{MoveStructState, MoveStructType},
};
use serde::{Deserialize, Serialize};
use std::io;

pub const MODULE_NAME: &IdentStr = ident_str!("auth_payload");

/// The original message prefix of the Bitcoin wallet includes the length of the message `x18`
/// We remove the length because the bcs serialization format already contains the length information
/// We remove the length because the bitcoin consensus codec serialization format already contains the length information
const MESSAGE_INFO_PREFIX: &[u8] = b"Bitcoin Signed Message:\n";
const MESSAGE_INFO: &[u8] = b"Rooch Transaction:\n";

Expand Down Expand Up @@ -57,7 +59,10 @@ impl SignData {
}

pub fn encode(&self) -> Vec<u8> {
bcs::to_bytes(self).expect("Serialize SignData should success")
let mut data = Vec::new();
self.consensus_encode(&mut data)
.expect("Serialize SignData should success");
data
}

/// The message info without tx hash, the verifier should append the tx hash to the message info
Expand All @@ -71,6 +76,24 @@ impl SignData {
}
}

impl Encodable for SignData {
fn consensus_encode<S: io::Write + ?Sized>(&self, s: &mut S) -> Result<usize, io::Error> {
let len = self.message_prefix.consensus_encode(s)?;
Ok(len + self.message_info.consensus_encode(s)?)
}
}

impl Decodable for SignData {
fn consensus_decode<D: io::Read + ?Sized>(
d: &mut D,
) -> Result<Self, bitcoin::consensus::encode::Error> {
Ok(SignData {
message_prefix: Decodable::consensus_decode(d)?,
message_info: Decodable::consensus_decode(d)?,
})
}
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AuthPayload {
// Message signature
Expand Down
1 change: 0 additions & 1 deletion frameworks/bitcoin-move/doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ This is the reference documentation of the Bitcoin Move Framework.
- [`0x4::bitcoin`](bitcoin.md#0x4_bitcoin)
- [`0x4::bitcoin_hash`](bitcoin_hash.md#0x4_bitcoin_hash)
- [`0x4::bitcoin_multisign_validator`](bitcoin_multisign_validator.md#0x4_bitcoin_multisign_validator)
- [`0x4::consensus_codec`](consensus_codec.md#0x4_consensus_codec)
- [`0x4::genesis`](genesis.md#0x4_genesis)
- [`0x4::inscription_updater`](inscription_updater.md#0x4_inscription_updater)
- [`0x4::multisign_account`](multisign_account.md#0x4_multisign_account)
Expand Down
287 changes: 0 additions & 287 deletions frameworks/bitcoin-move/doc/consensus_codec.md

This file was deleted.

Binary file modified frameworks/framework-release/released/8/0x2/package.rpd
Binary file not shown.
Binary file modified frameworks/framework-release/released/8/0x3/package.rpd
Binary file not shown.
Binary file modified frameworks/framework-release/released/8/stdlib
Binary file not shown.
1 change: 1 addition & 0 deletions frameworks/moveos-stdlib/doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ This is the reference documentation of the MoveOS standard library.
- [`0x2::bls12381`](bls12381.md#0x2_bls12381)
- [`0x2::cbor`](cbor.md#0x2_cbor)
- [`0x2::compare`](compare.md#0x2_compare)
- [`0x2::consensus_codec`](consensus_codec.md#0x2_consensus_codec)
- [`0x2::copyable_any`](copyable_any.md#0x2_copyable_any)
- [`0x2::core_addresses`](core_addresses.md#0x2_core_addresses)
- [`0x2::decimal_value`](decimal_value.md#0x2_decimal_value)
Expand Down
Loading

0 comments on commit f765347

Please sign in to comment.