mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-06 04:28:01 +00:00
Use array-bytes for All Array/Bytes/Hex Operations (#12190)
* Use `array-bytes` for All Array/Bytes/Hex Operations Signed-off-by: Xavier Lau <xavier@inv.cafe> * Reorder * Self Review * Format * Fix Tests * Bump `array-bytes` * Optimize large test res Signed-off-by: Xavier Lau <xavier@inv.cafe> Co-authored-by: parity-processbot <>
This commit is contained in:
@@ -57,7 +57,7 @@ impl GenerateNodeKeyCmd {
|
||||
let file_data = if self.bin {
|
||||
secret.as_ref().to_owned()
|
||||
} else {
|
||||
hex::encode(secret.as_ref()).into_bytes()
|
||||
array_bytes::bytes2hex("", secret.as_ref()).into_bytes()
|
||||
};
|
||||
|
||||
match &self.file {
|
||||
@@ -85,6 +85,6 @@ mod tests {
|
||||
assert!(generate.run().is_ok());
|
||||
let mut buf = String::new();
|
||||
assert!(file.read_to_string(&mut buf).is_ok());
|
||||
assert!(hex::decode(buf).is_ok());
|
||||
assert!(array_bytes::hex2bytes(&buf).is_ok());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,7 +127,7 @@ fn expect_public_from_phrase<Pair: sp_core::Pair>(
|
||||
) -> Result<(), Error> {
|
||||
let secret_uri = SecretUri::from_str(suri).map_err(|e| format!("{:?}", e))?;
|
||||
let expected_public = if let Some(public) = expect_public.strip_prefix("0x") {
|
||||
let hex_public = hex::decode(&public)
|
||||
let hex_public = array_bytes::hex2bytes(public)
|
||||
.map_err(|_| format!("Invalid expected public key hex: `{}`", expect_public))?;
|
||||
Pair::Public::try_from(&hex_public)
|
||||
.map_err(|_| format!("Invalid expected public key: `{}`", expect_public))?
|
||||
@@ -208,7 +208,7 @@ mod tests {
|
||||
.expect("Valid")
|
||||
.0
|
||||
.public();
|
||||
let valid_public_hex = format!("0x{}", hex::encode(valid_public.as_slice()));
|
||||
let valid_public_hex = array_bytes::bytes2hex("0x", valid_public.as_slice());
|
||||
let valid_accountid = format!("{}", valid_public.into_account());
|
||||
|
||||
// It should fail with the invalid public key
|
||||
@@ -226,7 +226,7 @@ mod tests {
|
||||
.0
|
||||
.public();
|
||||
let valid_public_hex_with_password =
|
||||
format!("0x{}", hex::encode(&valid_public_with_password.as_slice()));
|
||||
array_bytes::bytes2hex("0x", valid_public_with_password.as_slice());
|
||||
let valid_accountid_with_password =
|
||||
format!("{}", &valid_public_with_password.into_account());
|
||||
|
||||
@@ -248,7 +248,7 @@ mod tests {
|
||||
.0
|
||||
.public();
|
||||
let valid_public_hex_with_password_and_derivation =
|
||||
format!("0x{}", hex::encode(&valid_public_with_password_and_derivation.as_slice()));
|
||||
array_bytes::bytes2hex("0x", valid_public_with_password_and_derivation.as_slice());
|
||||
|
||||
// They should still be valid, because we check the base secret key.
|
||||
check_cmd(&seed_with_password_and_derivation, &valid_public_hex_with_password, true);
|
||||
|
||||
@@ -66,7 +66,8 @@ impl InspectNodeKeyCmd {
|
||||
if !self.bin {
|
||||
// With hex input, give to the user a bit of tolerance about whitespaces
|
||||
let keyhex = String::from_utf8_lossy(&file_data);
|
||||
file_data = hex::decode(keyhex.trim()).map_err(|_| "failed to decode secret as hex")?;
|
||||
file_data = array_bytes::hex2bytes(keyhex.trim())
|
||||
.map_err(|_| "failed to decode secret as hex")?;
|
||||
}
|
||||
|
||||
let secret =
|
||||
|
||||
@@ -70,7 +70,7 @@ fn sign<P: sp_core::Pair>(
|
||||
message: Vec<u8>,
|
||||
) -> error::Result<String> {
|
||||
let pair = utils::pair_from_suri::<P>(suri, password)?;
|
||||
Ok(hex::encode(pair.sign(&message)))
|
||||
Ok(array_bytes::bytes2hex("", pair.sign(&message).as_ref()))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -203,7 +203,7 @@ where
|
||||
Pair: sp_core::Pair,
|
||||
Pair::Public: Into<MultiSigner>,
|
||||
{
|
||||
let public = decode_hex(public_str)?;
|
||||
let public = array_bytes::hex2bytes(public_str)?;
|
||||
|
||||
let public_key = Pair::Public::try_from(&public)
|
||||
.map_err(|_| "Failed to construct public key from given hex")?;
|
||||
@@ -273,26 +273,17 @@ where
|
||||
format!("0x{}", HexDisplay::from(&public_key.into().into_account().as_ref()))
|
||||
}
|
||||
|
||||
/// helper method for decoding hex
|
||||
pub fn decode_hex<T: AsRef<[u8]>>(message: T) -> Result<Vec<u8>, Error> {
|
||||
let mut message = message.as_ref();
|
||||
if message[..2] == [b'0', b'x'] {
|
||||
message = &message[2..]
|
||||
}
|
||||
Ok(hex::decode(message)?)
|
||||
}
|
||||
|
||||
/// checks if message is Some, otherwise reads message from stdin and optionally decodes hex
|
||||
pub fn read_message(msg: Option<&String>, should_decode: bool) -> Result<Vec<u8>, Error> {
|
||||
let mut message = vec![];
|
||||
match msg {
|
||||
Some(m) => {
|
||||
message = decode_hex(m)?;
|
||||
message = array_bytes::hex2bytes(m.as_str())?;
|
||||
},
|
||||
None => {
|
||||
std::io::stdin().lock().read_to_end(&mut message)?;
|
||||
if should_decode {
|
||||
message = decode_hex(&message)?;
|
||||
message = array_bytes::hex2bytes(array_bytes::hex_bytes2hex_str(&message)?)?;
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
@@ -178,7 +178,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_generation_with_single_char() {
|
||||
let seed = generate_key::<sr25519::Pair>("ab", default_ss58_version()).unwrap();
|
||||
assert!(sr25519::Pair::from_seed_slice(&hex::decode(&seed[2..]).unwrap())
|
||||
assert!(sr25519::Pair::from_seed_slice(&array_bytes::hex2bytes_unchecked(&seed))
|
||||
.unwrap()
|
||||
.public()
|
||||
.to_ss58check()
|
||||
@@ -190,7 +190,7 @@ mod tests {
|
||||
let seed =
|
||||
generate_key::<sr25519::Pair>("ab", Ss58AddressFormatRegistry::PolkadotAccount.into())
|
||||
.unwrap();
|
||||
assert!(sr25519::Pair::from_seed_slice(&hex::decode(&seed[2..]).unwrap())
|
||||
assert!(sr25519::Pair::from_seed_slice(&array_bytes::hex2bytes_unchecked(&seed))
|
||||
.unwrap()
|
||||
.public()
|
||||
.to_ss58check_with_version(Ss58AddressFormatRegistry::PolkadotAccount.into())
|
||||
|
||||
@@ -55,7 +55,7 @@ impl VerifyCmd {
|
||||
/// Run the command
|
||||
pub fn run(&self) -> error::Result<()> {
|
||||
let message = utils::read_message(self.message.as_ref(), self.hex)?;
|
||||
let sig_data = utils::decode_hex(&self.sig)?;
|
||||
let sig_data = array_bytes::hex2bytes(&self.sig)?;
|
||||
let uri = utils::read_uri(self.uri.as_ref())?;
|
||||
let uri = if let Some(uri) = uri.strip_prefix("0x") { uri } else { &uri };
|
||||
|
||||
@@ -71,7 +71,7 @@ where
|
||||
let signature =
|
||||
Pair::Signature::try_from(&sig_data).map_err(|_| error::Error::SignatureFormatInvalid)?;
|
||||
|
||||
let pubkey = if let Ok(pubkey_vec) = hex::decode(uri) {
|
||||
let pubkey = if let Ok(pubkey_vec) = array_bytes::hex2bytes(uri) {
|
||||
Pair::Public::from_slice(pubkey_vec.as_slice())
|
||||
.map_err(|_| error::Error::KeyFormatInvalid)?
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user