Skip to content

Commit a4054d8

Browse files
authored
feat: enforce 0x prefix for hex data input (FuelLabs#125)
closes FuelLabs#121.
1 parent bee0964 commit a4054d8

File tree

2 files changed

+9
-17
lines changed

2 files changed

+9
-17
lines changed

README.md

+2-8
Original file line numberDiff line numberDiff line change
@@ -98,22 +98,16 @@ You may also sign a hex-encoded byte string:
9898
forc-wallet account <account_index> sign hex 0x0123456789ABCDEF
9999
```
100100

101-
The hex prefix at the beginning of the string is optional, e.g. the following is the same as above:
102-
103-
```sh
104-
forc-wallet account <account_index> sign hex 0123456789ABCDEF
105-
```
106-
107101
You can also use the `sign` subcommand directly, e.g. the following is the same:
108102

109103
```sh
110-
forc-wallet sign --account <account_index> hex 0123456789ABCDEF
104+
forc-wallet sign --account <account_index> hex 0x0123456789ABCDEF
111105
```
112106

113107
Using the `sign` subcommand, you can choose to sign directly with a private key (rather than a wallet account):
114108

115109
```sh
116-
forc-wallet sign --private-key hex 0123456789ABCDEF
110+
forc-wallet sign --private-key hex 0x0123456789ABCDEF
117111
```
118112

119113
## Other useful commands

src/sign.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,12 @@ fn msg_from_data(data: Data) -> Result<Message> {
169169
}
170170

171171
fn bytes_from_hex_str(mut hex_str: &str) -> Result<Vec<u8>> {
172-
// Strip the optional prefix.
173-
const OPTIONAL_PREFIX: &str = "0x";
174-
if hex_str.starts_with(OPTIONAL_PREFIX) {
175-
hex_str = &hex_str[OPTIONAL_PREFIX.len()..];
172+
// Check for the prefix.
173+
const PREFIX: &str = "0x";
174+
if hex_str.starts_with(PREFIX) {
175+
hex_str = &hex_str[PREFIX.len()..];
176+
} else {
177+
bail!("missing 0x at the beginning of hex string")
176178
}
177179
hex::decode(hex_str).context("failed to decode bytes from hex string")
178180
}
@@ -228,16 +230,12 @@ mod tests {
228230
#[test]
229231
fn sign_hex() {
230232
with_tmp_dir_and_wallet(|_dir, wallet_path| {
231-
let hex_encoded = hex::encode(TEST_STR);
233+
let hex_encoded = format!("0x{}", hex::encode(TEST_STR));
232234
let msg = msg_from_hex_str(&hex_encoded).unwrap();
233235
let account_ix = 0;
234236
let sig =
235237
sign_msg_with_wallet_account(wallet_path, account_ix, &msg, TEST_PASSWORD).unwrap();
236238
assert_eq!(sig.to_string(), EXPECTED_SIG);
237-
// The hex prefix should be ignored if it exists.
238-
let prefixed = format!("0x{hex_encoded}");
239-
let prefixed_msg = msg_from_hex_str(&prefixed).unwrap();
240-
assert_eq!(msg, prefixed_msg);
241239
});
242240
}
243241
}

0 commit comments

Comments
 (0)