|
| 1 | +use anyhow::Result; |
| 2 | +use k256::ecdsa::VerifyingKey as Secp256k1VerifyingKey; |
| 3 | +use prism_serde::{bech32::ToBech32, raw_or_b64}; |
| 4 | +use ripemd::Ripemd160; |
| 5 | +use serde::{Deserialize, Serialize}; |
| 6 | +use sha2::{Digest, Sha256}; |
| 7 | + |
| 8 | +#[derive(Serialize, Deserialize)] |
| 9 | +struct CosmosSignDoc { |
| 10 | + account_number: String, |
| 11 | + chain_id: String, |
| 12 | + fee: CosmosFee, |
| 13 | + memo: String, |
| 14 | + msgs: Vec<CosmosMessage>, |
| 15 | + sequence: String, |
| 16 | +} |
| 17 | + |
| 18 | +#[derive(Serialize, Deserialize)] |
| 19 | +struct CosmosFee { |
| 20 | + amount: Vec<String>, |
| 21 | + gas: String, |
| 22 | +} |
| 23 | + |
| 24 | +#[derive(Serialize, Deserialize)] |
| 25 | +struct CosmosMessage { |
| 26 | + #[serde(rename = "type")] |
| 27 | + msg_type: String, |
| 28 | + value: CosmosMessageValue, |
| 29 | +} |
| 30 | + |
| 31 | +#[derive(Serialize, Deserialize)] |
| 32 | +struct CosmosMessageValue { |
| 33 | + #[serde(with = "raw_or_b64")] |
| 34 | + data: Vec<u8>, |
| 35 | + signer: String, |
| 36 | +} |
| 37 | + |
| 38 | +impl CosmosSignDoc { |
| 39 | + fn new(signer: String, data: Vec<u8>) -> CosmosSignDoc { |
| 40 | + CosmosSignDoc { |
| 41 | + chain_id: "".to_string(), |
| 42 | + account_number: "0".to_string(), |
| 43 | + sequence: "0".to_string(), |
| 44 | + fee: CosmosFee { |
| 45 | + gas: "0".to_string(), |
| 46 | + amount: vec![], |
| 47 | + }, |
| 48 | + msgs: vec![CosmosMessage { |
| 49 | + msg_type: "sign/MsgSignData".to_string(), |
| 50 | + value: CosmosMessageValue { signer, data }, |
| 51 | + }], |
| 52 | + memo: "".to_string(), |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +/// Hashes a message according to the Cosmos ADR-36 specification. |
| 58 | +/// |
| 59 | +/// This function creates a standardized Cosmos sign doc from the provided message, |
| 60 | +/// serializes it according to ADR-36 requirements, and returns its SHA256 hash. |
| 61 | +/// |
| 62 | +/// # Arguments |
| 63 | +/// * `message` - The message to be hashed, which can be any type that can be referenced as a byte slice |
| 64 | +/// * `verifying_key` - The Secp256k1 verifying key associated with the signer |
| 65 | +/// |
| 66 | +/// # Returns |
| 67 | +/// * `Result<Vec<u8>>` - The SHA256 hash of the serialized sign doc or an error |
| 68 | +pub fn cosmos_adr36_hash_message( |
| 69 | + message: impl AsRef<[u8]>, |
| 70 | + verifying_key: &Secp256k1VerifyingKey, |
| 71 | +) -> Result<Vec<u8>> { |
| 72 | + // TODO: Support arbitrary address prefixes |
| 73 | + // At the moment we expect users to use "cosmoshub-4" as chainId when |
| 74 | + // signing prism data via `signArbitrary(..)`, resulting in "cosmos" as address prefix |
| 75 | + const ADDRESS_PREFIX: &str = "cosmos"; |
| 76 | + |
| 77 | + let signer = signer_from_key(ADDRESS_PREFIX, verifying_key)?; |
| 78 | + let serialized_sign_doc = create_serialized_adr36_sign_doc(message.as_ref().to_vec(), signer)?; |
| 79 | + let hashed_sign_doc = Sha256::digest(&serialized_sign_doc).to_vec(); |
| 80 | + Ok(hashed_sign_doc) |
| 81 | +} |
| 82 | + |
| 83 | +/// Creates a serialized Cosmos ADR-36 sign document. |
| 84 | +/// |
| 85 | +/// This function constructs a CosmosSignDoc with the provided data and signer, |
| 86 | +/// serializes it to JSON, and escapes certain HTML special characters to comply |
| 87 | +/// with ADR-36 requirements. |
| 88 | +/// |
| 89 | +/// # Arguments |
| 90 | +/// * `data` - The binary data to be included in the sign document |
| 91 | +/// * `signer` - The bech32-encoded address of the signer |
| 92 | +/// |
| 93 | +/// # Returns |
| 94 | +/// * `Result<Vec<u8>>` - The serialized sign document as bytes or an error |
| 95 | +fn create_serialized_adr36_sign_doc(data: Vec<u8>, signer: String) -> Result<Vec<u8>> { |
| 96 | + let adr36_sign_doc = CosmosSignDoc::new(signer, data); |
| 97 | + |
| 98 | + let sign_doc_str = serde_json::to_string(&adr36_sign_doc)? |
| 99 | + .replace("<", "\\u003c") |
| 100 | + .replace(">", "\\u003e") |
| 101 | + .replace("&", "\\u0026"); |
| 102 | + Ok(sign_doc_str.into_bytes()) |
| 103 | +} |
| 104 | + |
| 105 | +/// Derives a Cosmos bech32-encoded address from a Secp256k1 verifying key. |
| 106 | +/// |
| 107 | +/// This follows the Cosmos address derivation process: |
| 108 | +/// 1. Takes the SEC1-encoded public key bytes |
| 109 | +/// 2. Computes SHA256 hash of those bytes |
| 110 | +/// 3. Computes RIPEMD160 hash of the SHA256 result |
| 111 | +/// 4. Encodes the resulting 20-byte hash with bech32 using the provided prefix |
| 112 | +/// |
| 113 | +/// # Arguments |
| 114 | +/// * `address_prefix` - The bech32 human-readable part (e.g., "cosmos") |
| 115 | +/// * `verifying_key` - The Secp256k1 verifying key to derive the address from |
| 116 | +/// |
| 117 | +/// # Returns |
| 118 | +/// * `Result<String>` - The bech32-encoded address or an error |
| 119 | +fn signer_from_key(address_prefix: &str, verifying_key: &Secp256k1VerifyingKey) -> Result<String> { |
| 120 | + let verifying_key_bytes = verifying_key.to_sec1_bytes(); |
| 121 | + let hashed_key_bytes = Sha256::digest(verifying_key_bytes); |
| 122 | + let cosmos_address = Ripemd160::digest(hashed_key_bytes); |
| 123 | + |
| 124 | + let signer = cosmos_address.to_bech32(address_prefix)?; |
| 125 | + Ok(signer) |
| 126 | +} |
0 commit comments