Skip to content

Commit

Permalink
[WalletConnect/BNB]: Add signature and signature_json to Binance:…
Browse files Browse the repository at this point in the history
…:SigningOutput
  • Loading branch information
satoshiotomakan committed Jan 8, 2024
1 parent a81302e commit 77a917e
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 22 deletions.
1 change: 1 addition & 0 deletions .github/workflows/codegen-v2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ on:
jobs:
test:
runs-on: ubuntu-latest
if: github.event.pull_request.draft == false
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
Expand Down
20 changes: 17 additions & 3 deletions rust/chains/tw_binance/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.

use crate::context::BinanceContext;
use crate::modules::preimager::{JsonPreimager, JsonTxPreimage};
use crate::modules::serializer::BinanceAminoSerializer;
use crate::modules::tx_builder::TxBuilder;
Expand All @@ -12,9 +13,12 @@ use crate::transaction::SignerInfo;
use tw_coin_entry::coin_context::CoinContext;
use tw_coin_entry::coin_entry::{PublicKeyBytes, SignatureBytes};
use tw_coin_entry::common::compile_input::SingleSignaturePubkey;
use tw_coin_entry::error::SigningResult;
use tw_coin_entry::error::{SigningError, SigningErrorType, SigningResult};
use tw_coin_entry::signing_output_error;
use tw_keypair::ecdsa::secp256k1;
use tw_cosmos_sdk::modules::serializer::json_serializer::JsonSerializer;
use tw_cosmos_sdk::public_key::secp256k1::Secp256PublicKey;
use tw_cosmos_sdk::public_key::CosmosPublicKey;
use tw_misc::traits::ToBytesVec;
use tw_proto::Binance::Proto;
use tw_proto::TxCompiler::Proto as CompilerProto;

Expand Down Expand Up @@ -69,7 +73,13 @@ impl BinanceCompiler {
public_key,
} = SingleSignaturePubkey::from_sign_pubkey_list(signatures, public_keys)?;
let signature = BinanceSignature::try_from(signature.as_slice())?;
let public_key = secp256k1::PublicKey::try_from(public_key.as_slice())?;
let public_key = Secp256PublicKey::from_bytes(coin, public_key.as_slice())?;

let signature_bytes = signature.to_vec();
let signature_json = JsonSerializer::<BinanceContext>::serialize_signature(
&public_key,
signature_bytes.clone(),
);

let unsigned_tx = TxBuilder::unsigned_tx_from_proto(coin, &input)?;
let signed_tx = unsigned_tx.into_signed(SignerInfo {
Expand All @@ -79,8 +89,12 @@ impl BinanceCompiler {

let encoded_tx = BinanceAminoSerializer::serialize_signed_tx(&signed_tx)?;

let signature_json = serde_json::to_string(&signature_json)
.map_err(|_| SigningError(SigningErrorType::Error_internal))?;
Ok(Proto::SigningOutput {
encoded: encoded_tx.into(),
signature: signature_bytes.into(),
signature_json: signature_json.into(),
..Proto::SigningOutput::default()
})
}
Expand Down
2 changes: 0 additions & 2 deletions rust/chains/tw_binance/src/modules/preimager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ impl JsonPreimager {
pub fn preimage_hash(unsigned: &UnsignedTransaction) -> SigningResult<JsonTxPreimage> {
let encoded_tx = serde_json::to_string(unsigned)
.map_err(|_| SigningError(SigningErrorType::Error_internal))?;
// TODO
println!("{}", encoded_tx);
let tx_hash = sha2::sha256(encoded_tx.as_bytes());
let tx_hash = H256::try_from(tx_hash.as_slice()).expect("sha256 must return 32 bytes");
Ok(JsonTxPreimage {
Expand Down
6 changes: 3 additions & 3 deletions rust/chains/tw_binance/src/modules/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::amino::AminoEncoder;
use crate::transaction::SignedTransaction;
use std::borrow::Cow;
use tw_coin_entry::error::{SigningError, SigningErrorType, SigningResult};
use tw_hash::H264;
use tw_cosmos_sdk::public_key::CosmosPublicKey;
use tw_memory::Data;
use tw_misc::traits::ToBytesVec;
use tw_proto::serialize;
Expand Down Expand Up @@ -43,7 +43,7 @@ impl BinanceAminoSerializer {
.encode_size_prefixed()?)
}

pub fn serialize_public_key(public_key: H264) -> Data {
pub fn serialize_public_key(public_key: Data) -> Data {
let public_key_len = public_key.len() as u8;
AminoEncoder::new(&PUBLIC_KEY_PREFIX)
// Push the length of the public key.
Expand All @@ -54,7 +54,7 @@ impl BinanceAminoSerializer {

pub fn serialize_signature(signed: &SignedTransaction) -> SigningResult<Data> {
let sign_msg = Proto::Signature {
pub_key: Self::serialize_public_key(signed.signer.public_key.compressed()).into(),
pub_key: Self::serialize_public_key(signed.signer.public_key.to_bytes()).into(),
signature: signed.signer.signature.to_vec().into(),
account_number: signed.unsigned.account_number,
sequence: signed.unsigned.sequence,
Expand Down
18 changes: 16 additions & 2 deletions rust/chains/tw_binance/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.

use crate::context::BinanceContext;
use crate::modules::preimager::{JsonPreimager, JsonTxPreimage};
use crate::modules::serializer::BinanceAminoSerializer;
use crate::modules::tx_builder::TxBuilder;
use crate::signature::BinanceSignature;
use crate::transaction::SignerInfo;
use tw_coin_entry::coin_context::CoinContext;
use tw_coin_entry::error::SigningResult;
use tw_coin_entry::error::{SigningError, SigningErrorType, SigningResult};
use tw_coin_entry::signing_output_error;
use tw_cosmos_sdk::modules::serializer::json_serializer::JsonSerializer;
use tw_cosmos_sdk::public_key::secp256k1::Secp256PublicKey;
use tw_keypair::ecdsa::secp256k1;
use tw_keypair::traits::{KeyPairTrait, SigningKeyTrait};
use tw_misc::traits::ToBytesVec;
use tw_proto::Binance::Proto;

pub struct BinanceSigner;
Expand All @@ -37,16 +41,26 @@ impl BinanceSigner {
let key_pair = secp256k1::KeyPair::try_from(input.private_key.as_ref())?;

let signature = BinanceSignature::from(key_pair.sign(tx_hash)?);
let public_key = key_pair.public().clone();
let public_key = Secp256PublicKey::from_secp256k1_public_key(coin, key_pair.public())?;

let signature_bytes = signature.to_vec();
let signature_json = JsonSerializer::<BinanceContext>::serialize_signature(
&public_key,
signature_bytes.clone(),
);

let signed_tx = unsigned_tx.into_signed(SignerInfo {
public_key,
signature,
});
let encoded_tx = BinanceAminoSerializer::serialize_signed_tx(&signed_tx)?;

let signature_json = serde_json::to_string(&signature_json)
.map_err(|_| SigningError(SigningErrorType::Error_internal))?;
Ok(Proto::SigningOutput {
encoded: encoded_tx.into(),
signature: signature_bytes.into(),
signature_json: signature_json.into(),
..Proto::SigningOutput::default()
})
}
Expand Down
4 changes: 2 additions & 2 deletions rust/chains/tw_binance/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use crate::signature::BinanceSignature;
use crate::transaction::message::BinanceMessageEnum;
use serde::{Deserialize, Serialize};
use tw_keypair::ecdsa::secp256k1;
use tw_cosmos_sdk::public_key::secp256k1::Secp256PublicKey;
use tw_memory::Data;
use tw_misc::serde::as_string;

Expand Down Expand Up @@ -37,7 +37,7 @@ impl UnsignedTransaction {
}

pub struct SignerInfo {
pub public_key: secp256k1::PublicKey,
pub public_key: Secp256PublicKey,
pub signature: BinanceSignature,
}

Expand Down
15 changes: 11 additions & 4 deletions rust/tw_any_coin/tests/chains/binance/binance_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,19 @@ fn test_binance_compile() {
// Step 3: Compile transaction info

// Simulate signature, normally obtained from signature server.
let signature = "1b1181faec30b60a2ddaa2804c253cf264c69180ec31814929b5de62088c0c5a45e8a816d1208fc5366bb8b041781a6771248550d04094c3d7a504f9e8310679"
.decode_hex()
.unwrap();
let signature = "1b1181faec30b60a2ddaa2804c253cf264c69180ec31814929b5de62088c0c5a45e8a816d1208fc5366bb8b041781a6771248550d04094c3d7a504f9e8310679";
let signature_bytes = signature.decode_hex().unwrap();
let public_key = "026a35920088d98c3888ca68c53dfc93f4564602606cbb87f0fe5ee533db38e502"
.decode_hex()
.unwrap();

let mut compiler = CompilerHelper::<Proto::SigningOutput>::default();
let output = compiler.compile(CoinType::Binance, &input, vec![signature], vec![public_key]);
let output = compiler.compile(
CoinType::Binance,
&input,
vec![signature_bytes],
vec![public_key],
);

assert_eq!(output.error, SigningError::OK);
let expected_tx = concat!(
Expand All @@ -74,4 +78,7 @@ fn test_binance_compile() {
"04f9e8310679",
);
assert_eq!(output.encoded.to_hex(), expected_tx);
assert_eq!(output.signature.to_hex(), signature);
let expected_signature_json = r#"{"pub_key":{"type":"tendermint/PubKeySecp256k1","value":"Amo1kgCI2Yw4iMpoxT38k/RWRgJgbLuH8P5e5TPbOOUC"},"signature":"GxGB+uwwtgot2qKATCU88mTGkYDsMYFJKbXeYgiMDFpF6KgW0SCPxTZruLBBeBpncSSFUNBAlMPXpQT56DEGeQ=="}"#;
assert_eq!(output.signature_json, expected_signature_json);
}
Loading

0 comments on commit 77a917e

Please sign in to comment.