Skip to content

Commit

Permalink
Merge branch 'main' into ci/config
Browse files Browse the repository at this point in the history
  • Loading branch information
popcnt1 authored Sep 21, 2024
2 parents eac5bef + 85ee9e5 commit d31f7c6
Show file tree
Hide file tree
Showing 78 changed files with 32,503 additions and 224 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

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

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ default-members = [
"crates/rooch-oracle"
]

exclude = [
"generator/rust",
]

# All workspace members should inherit these keys
# for package declarations.
[workspace.package]
Expand Down Expand Up @@ -201,7 +205,6 @@ reqwest = { version = "0.12", features = ["json"] }
schemars = { version = "0.8.21", features = ["either"] }
serde = { version = "1.0.210", features = ["derive", "rc"] }
serde_bytes = "0.11.15"
#serde_json = { version = "1.0.111", features = ["preserve_order", "arbitrary_precision"] }
serde_json = { version = "1.0.127", features = ["preserve_order"] }
serde_yaml = "0.9"
serde_repr = "0.1"
Expand Down
36 changes: 18 additions & 18 deletions crates/rooch-open-rpc-spec/schemas/openrpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -940,23 +940,6 @@
}
}
},
"BitcoinOutPointView": {
"type": "object",
"required": [
"txid",
"vout"
],
"properties": {
"txid": {
"$ref": "#/components/schemas/bitcoin::blockdata::transaction::Txid"
},
"vout": {
"type": "integer",
"format": "uint32",
"minimum": 0.0
}
}
},
"DisplayFieldsView": {
"type": "object",
"required": [
Expand Down Expand Up @@ -2378,6 +2361,23 @@
}
]
},
"OutPointView": {
"type": "object",
"required": [
"txid",
"vout"
],
"properties": {
"txid": {
"$ref": "#/components/schemas/bitcoin::blockdata::transaction::Txid"
},
"vout": {
"type": "integer",
"format": "uint32",
"minimum": 0.0
}
}
},
"PageView_for_BalanceInfoView_and_IndexerStateIDView": {
"description": "`next_cursor` points to the last item in the page; Reading with `next_cursor` will start from the next item after `next_cursor` if `next_cursor` is `Some`, otherwise it will start from the first item.",
"type": "object",
Expand Down Expand Up @@ -2720,7 +2720,7 @@
"$ref": "#/components/schemas/u64"
},
"output": {
"$ref": "#/components/schemas/BitcoinOutPointView"
"$ref": "#/components/schemas/OutPointView"
}
}
},
Expand Down
78 changes: 76 additions & 2 deletions crates/rooch-rpc-api/src/jsonrpc_types/btc/ord.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

use super::utxo::BitcoinOutPointView;
use super::utxo::OutPointView;
use crate::jsonrpc_types::ObjectStateView;
use crate::jsonrpc_types::{
BytesView, IndexerObjectStateView, IndexerStateIDView, MoveStringView, ObjectIDVecView,
ObjectMetaView, StrView, UnitedAddressView,
};
use anyhow::Result;
use moveos_types::move_std::option::MoveOption;
use moveos_types::move_std::string::MoveString;
use moveos_types::state::MoveState;
use moveos_types::state::MoveStructType;
Expand All @@ -33,6 +35,12 @@ impl Display for InscriptionIDView {
}
}

impl From<InscriptionIDView> for InscriptionID {
fn from(view: InscriptionIDView) -> Self {
view.0
}
}

#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum InscriptionFilterView {
Expand Down Expand Up @@ -68,7 +76,7 @@ impl InscriptionFilterView {

#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
pub struct SatPointView {
pub output: BitcoinOutPointView,
pub output: OutPointView,
pub offset: StrView<u64>,
}

Expand All @@ -81,6 +89,15 @@ impl From<SatPoint> for SatPointView {
}
}

impl From<SatPointView> for SatPoint {
fn from(view: SatPointView) -> Self {
SatPoint {
outpoint: view.output.into(),
offset: view.offset.0,
}
}
}

#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
pub struct InscriptionView {
pub id: InscriptionIDView,
Expand Down Expand Up @@ -117,6 +134,28 @@ impl From<Inscription> for InscriptionView {
}
}

impl From<InscriptionView> for Inscription {
fn from(view: InscriptionView) -> Self {
let inscription_number = view.inscription_number;
Inscription {
id: view.id.0,
location: view.location.into(),
sequence_number: view.sequence_number,
inscription_number: inscription_number.unsigned_abs(),
is_cursed: inscription_number < 0,
charms: view.charms,
body: view.body.0,
content_encoding: view.content_encoding.map(|v| v.0).into(),
content_type: view.content_type.map(|v| v.0).into(),
metadata: view.metadata.0,
metaprotocol: view.metaprotocol.map(|v| v.0).into(),
parents: view.parents.into_iter().map(Into::into).collect(),
pointer: view.pointer.map(|v| v.0).into(),
rune: MoveOption::none(),
}
}
}

#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
pub struct InscriptionStateView {
#[serde(flatten)]
Expand All @@ -139,3 +178,38 @@ impl TryFrom<IndexerObjectStateView> for InscriptionStateView {
})
}
}

#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
pub struct InscriptionObjectView {
#[serde(flatten)]
pub metadata: ObjectMetaView,
pub value: InscriptionView,
}

impl InscriptionObjectView {
pub fn location(&self) -> SatPoint {
self.value.location.clone().into()
}
}

impl From<InscriptionStateView> for InscriptionObjectView {
fn from(state: InscriptionStateView) -> Self {
InscriptionObjectView {
metadata: state.metadata,
value: state.value,
}
}
}

impl TryFrom<ObjectStateView> for InscriptionObjectView {
type Error = anyhow::Error;

fn try_from(state: ObjectStateView) -> Result<Self, Self::Error> {
let inscription = Inscription::from_bytes(&state.value.0)?;
let inscription_view = InscriptionView::from(inscription);
Ok(InscriptionObjectView {
metadata: state.metadata,
value: inscription_view,
})
}
}
92 changes: 83 additions & 9 deletions crates/rooch-rpc-api/src/jsonrpc_types/btc/utxo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
use crate::jsonrpc_types::btc::transaction::TxidView;
use crate::jsonrpc_types::{
H256View, IndexerObjectStateView, IndexerStateIDView, ObjectIDVecView, ObjectIDView,
ObjectMetaView, StrView, UnitedAddressView,
ObjectMetaView, ObjectStateView, StrView, UnitedAddressView,
};
use anyhow::Result;
use bitcoin::hashes::Hash;
use bitcoin::{Amount, Txid};
use bitcoin::{Amount, TxOut, Txid};

use moveos_types::move_std::string::MoveString;
use moveos_types::state::{MoveState, MoveStructType};
use rooch_types::address::BitcoinAddress;
use rooch_types::bitcoin::types::OutPoint;
use rooch_types::bitcoin::utxo::{self, UTXO};
use rooch_types::indexer::state::ObjectStateFilter;
Expand All @@ -22,20 +23,29 @@ use std::collections::HashMap;
use std::str::FromStr;

#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Eq, JsonSchema)]
pub struct BitcoinOutPointView {
pub struct OutPointView {
pub txid: TxidView,
pub vout: u32,
}

impl From<BitcoinOutPointView> for bitcoin::OutPoint {
fn from(view: BitcoinOutPointView) -> Self {
bitcoin::OutPoint::new(view.txid.into(), view.vout)
impl From<OutPointView> for bitcoin::OutPoint {
fn from(view: OutPointView) -> Self {
bitcoin::OutPoint::new(view.txid.0, view.vout)
}
}

impl From<OutPoint> for BitcoinOutPointView {
impl From<OutPointView> for OutPoint {
fn from(view: OutPointView) -> Self {
OutPoint {
txid: view.txid.0.into_address(),
vout: view.vout,
}
}
}

impl From<OutPoint> for OutPointView {
fn from(outpoint: OutPoint) -> Self {
BitcoinOutPointView {
OutPointView {
txid: Txid::from_address(outpoint.txid).into(),
vout: outpoint.vout,
}
Expand Down Expand Up @@ -110,7 +120,7 @@ impl UTXOView {
let mut seals_view: HashMap<String, Vec<ObjectIDView>> = HashMap::new();
utxo.seals.data.into_iter().for_each(|element| {
seals_view.insert(
format!("0x{}", element.key),
element.key.to_string(),
element
.value
.into_iter()
Expand Down Expand Up @@ -190,3 +200,67 @@ impl TryFrom<IndexerObjectStateView> for UTXOStateView {
})
}
}

#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
pub struct UTXOObjectView {
#[serde(flatten)]
pub metadata: ObjectMetaView,
pub value: UTXOView,
}

impl From<UTXOStateView> for UTXOObjectView {
fn from(state: UTXOStateView) -> Self {
UTXOObjectView {
metadata: state.metadata,
value: state.value,
}
}
}

impl UTXOObjectView {
pub fn owner_bitcoin_address(&self) -> Option<BitcoinAddress> {
self.metadata
.owner_bitcoin_address
.as_ref()
.and_then(|addr| BitcoinAddress::from_str(addr).ok())
}

pub fn outpoint(&self) -> OutPoint {
self.value.outpoint()
}

pub fn amount(&self) -> Amount {
self.value.amount()
}

pub fn tx_output(&self) -> Result<TxOut> {
// Rooch UTXO does keep the original tx output script_pubkey,
// We convert the owner bitcoin address to script_pubkey here.
// But if the TxOut is a non-standard script_pubkey, we can not convert it back to bitcoin address.
// Find a way to keep the original script_pubkey in UTXO.
let script_pubkey = self
.owner_bitcoin_address()
.map(|addr| addr.script_pubkey())
.transpose()?
.ok_or_else(|| {
anyhow::anyhow!("Can not recognize the owner of UTXO {}", self.outpoint())
})?;
Ok(TxOut {
value: self.amount(),
script_pubkey,
})
}
}

impl TryFrom<ObjectStateView> for UTXOObjectView {
type Error = anyhow::Error;

fn try_from(state: ObjectStateView) -> Result<Self, Self::Error> {
let utxo = UTXO::from_bytes(&state.value.0)?;
let utxo_view = UTXOView::try_new_from_utxo(utxo)?;
Ok(UTXOObjectView {
metadata: state.metadata,
value: utxo_view,
})
}
}
1 change: 1 addition & 0 deletions crates/rooch-rpc-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ serde_json = { workspace = true }
log = { workspace = true }
hex = { workspace = true }
bitcoin = { workspace = true }
bitcoincore-rpc = { workspace = true }
tracing = { workspace = true }

move-core-types = { workspace = true }
Expand Down
Loading

0 comments on commit d31f7c6

Please sign in to comment.