Skip to content
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

Add btc native segwit function and support mixed signature #27

Open
wants to merge 23 commits into
base: develop
Choose a base branch
from

Conversation

wyoyw
Copy link
Contributor

@wyoyw wyoyw commented Jul 13, 2021

1.Forked from the master library
2.Add btc native segwit function and support mixed signature

@wyoyw wyoyw requested a review from XuNeal July 13, 2021 15:57
api/src/api.rs Outdated
@@ -145,3 +145,14 @@ pub struct BtcForkWallet {
#[prost(string, tag = "4")]
pub enc_x_pub: std::string::String,
}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct BtcAddressParam {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

直接修改原先的 AddressParam 把里面的 is_seg_wit 字段改成 seg_wit 就可以了。 AddressParam 本来就是为了通用的 API 设计的,这样很多就是为了修改入参的代码也可以省了

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修改

@@ -126,6 +130,52 @@ pub fn sign_segwit_transaction(param: &BtcTxInput, sign_param: &SignParam) -> Re
encode_message(tx_sign_result)
}

pub fn sign_mixed_transaction(param: &BtcTxInput, sign_param: &SignParam) -> Result<Vec<u8>> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个方法和下面 sign_usdt_mixed_transaction 重叠代码非常多,看起来是可以合并到一起的

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

沿用了以前结构,如果合并就比较突兀,可以整体优化下

@@ -0,0 +1 @@
{"rustc_fingerprint":14399210194298430792,"outputs":{"4476964694761187371":["___\nlib___.rlib\nlib___.dylib\nlib___.dylib\nlib___.a\nlib___.dylib\n/Users/joe/.rustup/toolchains/stable-x86_64-apple-darwin\ndebug_assertions\nproc_macro\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_feature=\"sse3\"\ntarget_feature=\"ssse3\"\ntarget_os=\"macos\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"apple\"\nunix\n",""],"1164083562126845933":["rustc 1.39.0 (4560ea788 2019-11-04)\nbinary: rustc\ncommit-hash: 4560ea788cb760f0a34127156c78e2552949f734\ncommit-date: 2019-11-04\nhost: x86_64-apple-darwin\nrelease: 1.39.0\nLLVM version: 9.0\n",""],"1138116330425514636":["___\n",""],"10915535544655558915":["rustdoc [options] <input>\n\nOptions:\n -h, --help show this help message\n -V, --version print rustdoc's version\n -v, --verbose use verbose output\n -r, --input-format [rust]\n the input type of the specified file\n -w, --output-format [html]\n the output type to write\n -o, --output PATH where to place the output\n --crate-name NAME\n specify the name of this crate\n --crate-type [bin|lib|rlib|dylib|cdylib|staticlib|proc-macro]\n Comma separated list of types of crates\n for the compiler to emit\n -L, --library-path DIR\n directory to add to crate search path\n --cfg pass a --cfg to rustc\n --extern NAME=PATH\n pass an --extern to rustc\n --extern-html-root-url NAME=URL\n base URL to use for dependencies\n --plugin-path DIR\n removed\n -C, --codegen OPT[=VALUE]\n pass a codegen option to rustc\n --passes PASSES list of passes to also run, you might want to pass it\n multiple times; a value of `list` will print available\n passes\n --plugins PLUGINS\n removed\n --no-defaults don't run the default passes\n --document-private-items \n document private items\n --test run code examples as tests\n --test-args ARGS\n arguments to pass to the test runner\n --target TRIPLE target triple to document\n --markdown-css FILES\n CSS files to include via <link> in a rendered Markdown\n file\n --html-in-header FILES\n files to include inline in the <head> section of a\n rendered Markdown file or generated documentation\n --html-before-content FILES\n files to include inline between <body> and the content\n of a rendered Markdown file or generated documentation\n --html-after-content FILES\n files to include inline between the content and\n </body> of a rendered Markdown file or generated\n documentation\n --markdown-before-content FILES\n files to include inline between <body> and the content\n of a rendered Markdown file or generated documentation\n --markdown-after-content FILES\n files to include inline between the content and\n </body> of a rendered Markdown file or generated\n documentation\n --markdown-playground-url URL\n URL to send code snippets to\n --markdown-no-toc \n don't include table of contents\n -e, --extend-css PATH\n To add some CSS rules with a given file to generate\n doc with your own theme. However, your theme might\n break if the rustdoc's generated HTML changes, so be\n careful!\n -Z FLAG internal and debugging options (only on nightly build)\n --sysroot PATH Override the system root\n --playground-url URL\n URL to send code snippets to, may be reset by\n --markdown-playground-url or\n `#![doc(html_playground_url=...)]`\n --display-warnings \n to print code warnings when testing doc\n --crate-version VERSION\n crate version to print into documentation\n --sort-modules-by-appearance \n sort modules by where they appear in the program,\n rather than alphabetically\n --themes FILES additional themes which will be added to the generated\n docs\n --theme-checker FILES\n check if given theme is valid\n --resource-suffix PATH\n suffix to add to CSS and JavaScript files, e.g.,\n \"light.css\" will become \"light-suffix.css\"\n --edition EDITION\n edition to use when compiling rust code (default:\n 2015)\n --color auto|always|never\n Configure coloring of output:\n auto = colorize, if output goes to a tty (default);\n always = always colorize output;\n never = never colorize output\n --error-format human|json|short\n How errors and other messages are produced\n --json CONFIG Configure the structure of JSON diagnostics\n --disable-minification \n Disable minification applied on JS files\n -W, --warn OPT Set lint warnings\n -A, --allow OPT Set lint allowed\n -D, --deny OPT Set lint denied\n -F, --forbid OPT Set lint forbidden\n --cap-lints LEVEL\n Set the most restrictive lint level. More restrictive\n lints are capped at this level. By default, it is at\n `forbid` level.\n --index-page PATH\n Markdown file to be used as index page\n --enable-index-page \n To enable generation of the index page\n --static-root-path PATH\n Path string to force loading static files from in\n output pages. If not set, uses combinations of '../'\n to reach the documentation root.\n --disable-per-crate-search \n disables generating the crate selector on the search\n box\n --persist-doctests PATH\n Directory to persist doctest executables into\n --generate-redirect-pages \n Generate extra pages to support legacy URLs and tool\n links\n --show-coverage \n calculate percentage of public items with\n documentation\n --enable-per-target-ignores \n parse ignore-foo for ignoring doctests on a per-target\n basis\n --runtool The tool to run tests with when building for a different target than host\n \n --runtool-arg One (of possibly many) arguments to pass to the runtool\n \n --test-builder specified the rustc-like binary to use as the test\n builder\n\n",""],"7418947162089833526":["___\nlib___.rlib\nlib___.dylib\nlib___.dylib\nlib___.a\nlib___.dylib\n/Users/joe/.rustup/toolchains/stable-x86_64-apple-darwin\ndebug_assertions\nproc_macro\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_feature=\"sse3\"\ntarget_feature=\"ssse3\"\ntarget_os=\"macos\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"apple\"\nunix\n",""]},"successes":{}}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

target 目录需要从 github 中排除

let mut parent_path_and_pubkeys: HashMap<String, String> = HashMap::new();
let mut trans_type_flg = TransTypeFlg::BTC;
for utxo in utxos {
let parent_path =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

看到提出了一个 get_xpub_safe 方法,👍。不过这个函数还是超过 100 多行了,如果可以,将下面分成三部分似乎更好, 1. 判断 flag 和 path。2. 公私钥派生 3. 地址校验

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修改

let sign_source_val = &xpub_data[..194];
let sign_result = &xpub_data[194..];
let pub_key = &sign_source_val[..130];
let chain_code = &sign_source_val[130..];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

130 这个魔法数字出现频率太高了,最好定义为一个常量。以免有些地方手误写错

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修改

txouts.push(self.build_op_return_output(extra_data));
}

//8.output data serialize
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

//8.output data serialize 这个注释好像不太对

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修改

})
}

pub fn sign_mixed_transaction(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个函数快300行了,如果能分割成不同小函数读起来和后续代码重用应该都会方便很多。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

没有改..

};
let sign_result = transaction_req_data.sign_native_segwit_transaction(
Network::Testnet,
&"m/49'/1'/0'/".to_string(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

native segwit 的path 应该是 m'/84'/1'/0'。不过这个关系不大

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个是之前native开发时,用错了path,涉及测试数据,在链上有对应,就不改了。 后来支持混合签名用的84

}

#[test]
fn test_native_segwit_bech32_to_bech32_hash_change() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo hash -> has

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修改

}

#[test]
fn test_segwit_p2php2wpkh_to_bech32() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo p2php2wpkh -> p2sh_p2wpkh

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修改

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants