0.1.0 |
|
---|
#139 in #private-key
用于 crw-client
25KB
379 行
钱包包
钱包包包含创建钱包和使用它来签名交易所需的所有内容。
钱包
/// Keychain contains a pair of Secp256k1 keys.
pub struct Keychain {
pub ext_public_key: ExtendedPubKey,
pub ext_private_key: ExtendedPrivKey,
}
/// Wallet is a facility used to manipulate private and public keys associated
/// to a BIP-32 mnemonic.
pub struct Wallet {
pub keychain: Keychain,
pub bech32_address: String,
}
从助记词导入钱包
fn import_wallet_from_mnemonic_example() {
let wallet = Wallet::from_mnemonic(
"battle call once stool three mammal hybrid list sign field athlete amateur cinnamon eagle shell erupt voyage hero assist maple matrix maximum able barrel",
"m/44'/852'/0'/0/0".to_string(),
"desmos".to_string(),
).unwrap();
}
签名交易
fn sign_tx_example() {
let wallet = Wallet::from_mnemonic(...).unwrap();
let lcd_endpoint = "https://127.0.0.1:1317";
let node_info = get_node_info(lcd_endpoint.to_string())
.await
.unwrap()
.node_info;
let grpc_endpoint = "https://127.0.0.1:9090";
let chain_client = ChainClient::new(
node_info,
lcd_endpoint.to_string(),
grpc_endpoint.to_string(),
);
let account = chain_client
.get_account_data(wallet.bech32_address.clone())
.await
.unwrap();
// Gas Fee
let coin = Coin {
denom: "stake".to_string(),
amount: "5000".to_string(),
};
let fee = Fee {
amount: vec![coin],
gas_limit: 300000,
payer: "".to_string(),
granter: "".to_string(),
};
let amount = Coin {
denom: "stake".to_string(),
amount: "100000".to_string(),
};
let msg = MsgSend {
from_address: wallet.bech32_address.clone(),
to_address: "desmos16kjmymxuxjns7usuke2604arqm9222gjgp9d56".to_string(),
amount: vec![amount],
};
let mut msg_bytes = Vec::new();
prost::Message::encode(&msg, &mut msg_bytes).unwrap();
let proto_msg = Msg(Any {
type_url: "/cosmos.bank.v1beta1.Msg/Send".to_string(),
value: msg_bytes,
});
let msgs = vec![proto_msg];
let tx_signed_bytes = wallet
.sign_tx(account, chain_client.node_info.network, msgs, fee, None, 0)
.unwrap();
}
依赖项
~21MB
~379K SLoC