-
Notifications
You must be signed in to change notification settings - Fork 44
refactor(sdk): conventional naming to/from methods #2850
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v2.2-dev
Are you sure you want to change the base?
Changes from all commits
7698392
e99494f
ee56d78
2814fe1
24711ff
bc476ce
84a4ce9
aa70d63
44fd7d9
bd0c39a
9d4796b
06c576f
ee6ecc2
3adc61d
f1d296d
77e95ae
6796ba3
8717ec2
b62ce96
9703af9
cb0ad52
d09e11f
03abb96
e9f6f2e
2a2142b
7717fed
252e4e6
7af57a7
267ae9c
fc83930
3dbeddc
c24080d
fadde6d
7572cff
6b636a1
e9f77e2
c1559b6
de92ae7
e346dfa
4168580
236a680
dc34f75
175cc08
136e253
5f9bed7
2afb5f3
b100690
5e0f1b4
39deaba
85ab1d5
71b73c2
3561301
fcd291a
de398f9
16d7b23
29dd696
4c77e8b
aa0d6e4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -42,39 +42,42 @@ impl CoreScriptWasm { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[wasm_bindgen(js_name = "newP2PKH")] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub fn new_p2pkh(js_key_hash: Vec<u8>) -> Self { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let mut key_hash = [0u8; 20]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let bytes = js_key_hash.as_slice(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub fn new_p2pkh(key_hash: Vec<u8>) -> Self { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let mut key_hash_bytes = [0u8; 20]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let bytes = key_hash.as_slice(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let len = bytes.len().min(32); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| key_hash[..len].copy_from_slice(&bytes[..len]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| key_hash_bytes[..len].copy_from_slice(&bytes[..len]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| CoreScriptWasm(CoreScript::new_p2pkh(key_hash)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| CoreScriptWasm(CoreScript::new_p2pkh(key_hash_bytes)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[wasm_bindgen(js_name = "newP2SH")] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub fn new_p2sh(js_script_hash: Vec<u8>) -> Self { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let mut script_hash = [0u8; 20]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let bytes = js_script_hash.as_slice(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub fn new_p2sh(script_hash: Vec<u8>) -> Self { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let mut script_hash_bytes = [0u8; 20]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let bytes = script_hash.as_slice(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let len = bytes.len().min(32); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| script_hash[..len].copy_from_slice(&bytes[..len]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| script_hash_bytes[..len].copy_from_slice(&bytes[..len]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let mut bytes = vec![ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| opcodes::all::OP_HASH160.to_u8(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| opcodes::all::OP_PUSHBYTES_20.to_u8(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bytes.extend_from_slice(&script_hash); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bytes.extend_from_slice(&script_hash_bytes); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bytes.push(opcodes::all::OP_EQUAL.to_u8()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Self::from_bytes(bytes) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+55
to
68
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix array bounds issue to prevent panic. Same issue as Apply this diff to fix the bounds check: pub fn new_p2sh(script_hash: Vec<u8>) -> Self {
let mut script_hash_bytes = [0u8; 20];
let bytes = script_hash.as_slice();
- let len = bytes.len().min(32);
+ let len = bytes.len().min(20);
script_hash_bytes[..len].copy_from_slice(&bytes[..len]);
let mut bytes = vec![📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[wasm_bindgen(js_name = "toAddress")] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub fn to_address(&self, js_network: &JsValue) -> WasmDppResult<String> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let network = NetworkWasm::try_from(js_network.clone())?; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub fn to_address( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| &self, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[wasm_bindgen(unchecked_param_type = "Network | string")] network: &JsValue, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) -> WasmDppResult<String> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let network_wasm = NetworkWasm::try_from(network.clone())?; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let payload = Payload::from_script(self.0.as_script()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .map_err(|err| WasmDppError::invalid_argument(err.to_string()))?; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let address = Address::new(network.into(), payload); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let address = Address::new(network_wasm.into(), payload); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Ok(address.to_string()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -94,7 +97,7 @@ impl CoreScriptWasm { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| encode(self.0.to_bytes().as_slice(), Hex) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[wasm_bindgen(js_name = "base64")] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[wasm_bindgen(js_name = "toBase64")] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub fn to_base64(&self) -> String { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| encode(self.0.to_bytes().as_slice(), Base64) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix array bounds issue to prevent panic.
The code uses
.min(32)butkey_hash_bytesis only a 20-byte array. If the inputkey_hashhas a length between 21-32 bytes,lenwill exceed 20, causingkey_hash_bytes[..len]to panic at runtime.Apply this diff to fix the bounds check:
pub fn new_p2pkh(key_hash: Vec<u8>) -> Self { let mut key_hash_bytes = [0u8; 20]; let bytes = key_hash.as_slice(); - let len = bytes.len().min(32); + let len = bytes.len().min(20); key_hash_bytes[..len].copy_from_slice(&bytes[..len]); CoreScriptWasm(CoreScript::new_p2pkh(key_hash_bytes))📝 Committable suggestion
🤖 Prompt for AI Agents