From 78b8c041534d31e1208a47070b7043a4b6ecc01a Mon Sep 17 00:00:00 2001 From: Vishal Menon Date: Tue, 18 Oct 2022 23:35:04 +0530 Subject: [PATCH] Updated string parse of policy to use pk( instead of just pk as this can collide with certain keys --- .DS_Store | Bin 18436 -> 8196 bytes .gitignore | 3 +- .vscode/dryrun.log | 4 +- .vscode/targets.log | 803 ++++++++++++++++++------------------------ Cargo.toml | 2 +- src/key/derivation.rs | 16 +- src/wallet/policy.rs | 4 +- src/wallet/sync.rs | 22 +- 8 files changed, 390 insertions(+), 464 deletions(-) diff --git a/.DS_Store b/.DS_Store index 55dd025d0a8a6076881592061382fff974155417..91cbcee5553fb83c6bf1521342841e48216b8037 100644 GIT binary patch delta 487 zcmZpfz}Vs-!N9=4=v10w$iM(%asV-i7T}&(C^k_~a`9847^nnLtBIfQA(_WK8B)me=HBa6wX?29(cXNJX`T z=_=5Yd6TP^j5j+=STbp|0S!n58ki0=x+JN*xB%+#?MVeWnZ+dr2GY}j6ulEWiPO@m3s`^zvea!co_skL5=zrnq7qc7L9pN>wc|9k>e^N8G>fVz z5=#(2p+BLl+3*wC!IGcQJ)6QgcbsU(Y2Te2Tou%rXy(m~-#hQUd+x`3Z)PGQ(Z+r! z7FidO3U;O!-oR~KRFT%PA8q`d`-!cDI}!Hpv6oR^q%A`kNlUin?up$0y&E%V5_hy+ z-jSYkGwr{JevD*C-j^j=mPM%}t;v1alY!jEu_XuBdxPzEZ_q}0bUu1lr1a!(-Ec=_ z7JC?X6}SyqHE@3rnXet=re%DxjcZ}7ncbr{Y72`)A%Q{yg#-!-yp9rJ?8a6(o213D zkU$}ULITGUpns@hXSzO450g|~2M!!Q^LGxRb;CJ^!g{YdPS?ljVUlW5l+mI{lcLdh ziV-Enalgv!NY}^dVUi}rB}$4*G_IKw7jBW(j?`}-Pj%e>r%%){W zh^c`v_QABgDhWv+3j}*2OER^erKpQSY4kYK2>*M2(I6EPT2b+8S-k=ZX zipdlrE5x%6VRi8+V{}4HFz<|*O31w@+gV}KU^BVq^r6q5>_YIPY+f3oJWdIIRv)VH zG0_A?RGLgqTozj)fkFa>1PTefED6*ATt9->rTha`J;StNA{&X;ppkU)49(a z=k5Z%kEiB{xY;s{`x%$d8XjnX8c1g{_ss7THO%38{l1PTI+MXfk<+cot;Ao5)ZzJ`I(fG=fCh@a6u9h`_pC40k;hj z`XG$)zmM9UTaqJz=zMA{a_^{C#(Jj zYOSOHYd~l3S#twBCp0nc#Co4~^EDQ;elnc9{zT3z==0R%uX>W64u<-lu767M*U5{o ze!j*tHTmmgD1ZGi>TZJVsmY)EADyQDDaoHpORoBpsr%IAulk?ve|UMEzJ*>V=u=du zpQe1DjkyYI4P$bWogXvtj@C4v6LB9V{YtQ%s!v`_L!PQ5UQDC!%DWHKel&oXH}h?7 z-pLXDbx@x4Pi{ajxf?Ft7qJi?>^pgPV;cHNdv{|R`bpm1n3jHmcQ>Y?pR{);CZlnf zBl7;Mi+4U^3S*M@{Fr>8z=tY9Oy%8&$wx(DO!DriIK3~ht zd-CnJ5#wm@ZcHP_k@pPdX;%g2l7j1JkzRZ8_y4DbFUl1XC?xPdkU&(vx_Na24UJyV zSbl3)#eNGrH|9I!2{xSTr+7&9Q#@SoQ@oGIzkN1NZ!zVDe1}O&6kcm_q{)|jFa1Nn z(Es^h%(WM0FP~ywZN(nMiSo1Du_tM#5qlUX+?ZDE!T7+9X^meoK5%2^7#BJ*U$goD EKUr1HA^-pY diff --git a/.gitignore b/.gitignore index f19ba6e..b92b691 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ releases *.profraw .vscode *.tar.gz -*.tar \ No newline at end of file +*.tar +.DS_Store \ No newline at end of file diff --git a/.vscode/dryrun.log b/.vscode/dryrun.log index ea568b7..1876db4 100644 --- a/.vscode/dryrun.log +++ b/.vscode/dryrun.log @@ -1,9 +1,9 @@ make --dry-run --always-make --keep-going --print-directory -make: Entering directory '/home/i5hi/StackmateNetwork/stackmate-core' +make: Entering directory `/Users/i5hi/Code/stackmate/backend/stackmate-core' echo echo " Available actions in "stackmate-core":" echo sed -n 's/^##//p' makefile | column -t -s ':' | sed -e 's/^/ /' echo -make: Leaving directory '/home/i5hi/StackmateNetwork/stackmate-core' +make: Leaving directory `/Users/i5hi/Code/stackmate/backend/stackmate-core' diff --git a/.vscode/targets.log b/.vscode/targets.log index 871d942..ddd20e8 100644 --- a/.vscode/targets.log +++ b/.vscode/targets.log @@ -1,313 +1,170 @@ make all --print-data-base --no-builtin-variables --no-builtin-rules --question -# GNU Make 4.3 - -# Built for x86_64-pc-linux-gnu - -# Copyright (C) 1988-2020 Free Software Foundation, Inc. - -# License GPLv3+: GNU GPL version 3 or later - -# This is free software: you are free to change and redistribute it. - -# There is NO WARRANTY, to the extent permitted by law. - +# GNU Make 3.81 +# Copyright (C) 2006 Free Software Foundation, Inc. +# This is free software; see the source for copying conditions. +# There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. +# This program built for i386-apple-darwin11.3.0 -# Make data base, printed on Wed Aug 3 17:02:28 2022 - + +# Make data base, printed on Wed Aug 31 14:04:09 2022 # Variables - - -# environment - -GDK_BACKEND = x11 - -# environment - -LC_ALL = C - -# environment - -GNOME_SHELL_SESSION_MODE = pop - -# environment - -SSH_AGENT_LAUNCHER = gnome-keyring - -# environment - -GJS_DEBUG_OUTPUT = stderr - -# environment - -VSCODE_IPC_HOOK_EXTHOST = /run/user/1000/vscode-ipc-3e90a4fc-ec84-4359-a188-53f218f09e66.sock - -# environment - -MANDATORY_PATH = /usr/share/gconf/pop.mandatory.path - -# environment - -VSCODE_CWD = /home/i5hi - -# environment - -WINDOWPATH = 2 - -# default - -MAKE_COMMAND := make - -# environment - -QT_ACCESSIBILITY = 1 - # automatic - -@D = $(patsubst %/,%,$(dir $@)) - -# environment - -VSCODE_HANDLES_UNCAUGHT_ERRORS = true - -# default - -.VARIABLES := - -# environment - -PWD = /home/i5hi/StackmateNetwork/stackmate-core - + $@ + @echo "[DONE] $@" + init: # Phony target (prerequisite of .PHONY). # Implicit rule search has not been done. # File does not exist. # File has not been updated. -# recipe to execute (from 'makefile', line 24): +# commands to execute (from `makefile', line 24): rustup target add aarch64-apple-ios x86_64-apple-ios #rustup target add armv7-apple-ios armv7s-apple-ios i386-apple-ios ## deprecated rustup target add aarch64-linux-android armv7-linux-androideabi i686-linux-android x86_64-linux-android @if [ $$(uname) == "Darwin" ] ; then cargo install cargo-lipo ; fi cargo install cbindgen + # Not a target: -src/config.rs: +src/wallet/custom.rs: # Implicit rule search has been done. -# Last modified 2022-08-03 16:38:41.647477314 +# Last modified 2022-04-21 19:05:54 # File has been updated. # Successfully updated. - -all: ios android bindings -# Command line target. -# Implicit rule search has been done. -# File does not exist. -# File has been updated. -# Needs to be updated (-q is set). # variable set hash-table stats: -# Load=0/32=0%, Rehash=0, Collisions=0/4=0% +# Load=0/32=0%, Rehash=0, Collisions=0/0=0% -help: makefile -# Phony target (prerequisite of .PHONY). +target/aarch64-linux-android/release/libstackmate.so: src/bip392/mod.rs src/config.rs src/e.rs src/key/derivation.rs src/key/ec.rs src/key/encoding.rs src/key/encryption.rs src/key/mod.rs src/key/seed.rs src/lib.rs src/network/fees.rs src/network/height.rs src/network/mod.rs src/wallet/address.rs src/wallet/coldcard.rs src/wallet/custom.rs src/wallet/history.rs src/wallet/mod.rs src/wallet/policy.rs src/wallet/psbt.rs src/wallet/sync.rs src/wallet/utxo.rs ndk-home # Implicit rule search has not been done. -# File does not exist. +# Modification time never checked. # File has not been updated. -# recipe to execute (from 'makefile', line 15): - @echo - @echo " Available actions in "$(PROJECTNAME)":" - @echo - @sed -n 's/^##//p' $< | column -t -s ':' | sed -e 's/^/ /' - @echo +# commands to execute (from `makefile', line 52): + cargo build --target aarch64-linux-android --release + @echo "[DONE] $@" + # Not a target: -src/network/tor.rs: +src/wallet/coldcard.rs: # Implicit rule search has been done. -# Last modified 2022-06-07 22:48:36.774147822 +# Last modified 2022-08-04 07:47:16 # File has been updated. # Successfully updated. +# variable set hash-table stats: +# Load=0/32=0%, Rehash=0, Collisions=0/0=0% -test: -# Phony target (prerequisite of .PHONY). +target/armv7-linux-androideabi/release/libstackmate.so: src/bip392/mod.rs src/config.rs src/e.rs src/key/derivation.rs src/key/ec.rs src/key/encoding.rs src/key/encryption.rs src/key/mod.rs src/key/seed.rs src/lib.rs src/network/fees.rs src/network/height.rs src/network/mod.rs src/wallet/address.rs src/wallet/coldcard.rs src/wallet/custom.rs src/wallet/history.rs src/wallet/mod.rs src/wallet/policy.rs src/wallet/psbt.rs src/wallet/sync.rs src/wallet/utxo.rs ndk-home # Implicit rule search has not been done. -# File does not exist. +# Modification time never checked. # File has not been updated. -# recipe to execute (from 'makefile', line 96): - cargo test +# commands to execute (from `makefile', line 56): + cargo build --target armv7-linux-androideabi --release + @echo "[DONE] $@" + # Not a target: -makefile: + +src/wallet/utxo.rs: # Implicit rule search has been done. -# Last modified 2022-06-04 05:17:49.920481758 +# Last modified 2022-08-04 07:47:16 # File has been updated. # Successfully updated. +# variable set hash-table stats: +# Load=0/32=0%, Rehash=0, Collisions=0/0=0% # Not a target: - -src/wallet/proof_of_payment.rs: +src/config.rs: # Implicit rule search has been done. -# Last modified 2022-06-21 18:21:06.854543569 +# Last modified 2022-08-04 07:47:16 # File has been updated. # Successfully updated. - -target/aarch64-linux-android/release/libstackmate.so: src/bip392/mod.rs src/config.rs src/e.rs src/key/derivation.rs src/key/ec.rs src/key/encoding.rs src/key/encryption.rs src/key/mod.rs src/key/seed.rs src/lib.rs src/network/fees.rs src/network/height.rs src/network/mod.rs src/network/tor.rs src/wallet/address.rs src/wallet/coldcard.rs src/wallet/custom.rs src/wallet/history.rs src/wallet/mod.rs src/wallet/policy.rs src/wallet/proof_of_payment.rs src/wallet/psbt.rs src/wallet/recover.rs src/wallet/sync.rs src/wallet/utxo.rs ndk-home -# Implicit rule search has not been done. -# Modification time never checked. -# File has not been updated. -# recipe to execute (from 'makefile', line 52): - cargo build --target aarch64-linux-android --release - @echo "[DONE] $@" - -ndk-home: -# Phony target (prerequisite of .PHONY). -# Implicit rule search has not been done. -# Implicit/static pattern stem: '' -# File does not exist. -# File has been updated. -# Needs to be updated (-q is set). -# automatic -# @ := ndk-home -# automatic -# * := -# automatic -# < := -# automatic -# + := -# automatic -# % := -# automatic -# ^ := -# automatic -# ? := -# automatic -# | := # variable set hash-table stats: -# Load=8/32=25%, Rehash=0, Collisions=6/12=50% -# recipe to execute (from 'makefile', line 69): - @if [ ! -d "${ANDROID_NDK_HOME}" ] ; then \ - echo "Error: Please, set the ANDROID_NDK_HOME env variable to point to your NDK folder" ; \ - exit 1 ; \ - fi +# Load=0/32=0%, Rehash=0, Collisions=0/0=0% # Not a target: -src/wallet/sync.rs: +src/key/derivation.rs: # Implicit rule search has been done. -# Last modified 2022-08-03 16:39:50.295331429 +# Last modified 2022-08-04 07:47:16 # File has been updated. # Successfully updated. +# variable set hash-table stats: +# Load=0/32=0%, Rehash=0, Collisions=0/0=0% # Not a target: -src/wallet/address.rs: +src/lib.rs: # Implicit rule search has been done. -# Last modified 2022-08-03 14:46:00.942081689 +# Last modified 2022-08-04 12:59:12 # File has been updated. # Successfully updated. +# variable set hash-table stats: +# Load=0/32=0%, Rehash=0, Collisions=0/0=0% # Not a target: -src/key/ec.rs: +src/key/encryption.rs: # Implicit rule search has been done. -# Last modified 2022-06-07 22:39:21.202541416 +# Last modified 2022-08-04 11:52:07 # File has been updated. # Successfully updated. +# variable set hash-table stats: +# Load=0/32=0%, Rehash=0, Collisions=0/0=0% -# Not a target: -.SUFFIXES: -# Implicit rule search has not been done. -# Modification time never checked. -# File has not been updated. - -# Not a target: - -src/key/mod.rs: +ios: target/universal/release/libstackmate.a # Implicit rule search has been done. -# Last modified 2022-05-28 20:57:37.657255199 +# File does not exist. # File has been updated. -# Successfully updated. - -.PHONY: help init ndk-home clean test -# Implicit rule search has not been done. -# Modification time never checked. -# File has not been updated. - -target/armv7-linux-androideabi/release/libstackmate.so: src/bip392/mod.rs src/config.rs src/e.rs src/key/derivation.rs src/key/ec.rs src/key/encoding.rs src/key/encryption.rs src/key/mod.rs src/key/seed.rs src/lib.rs src/network/fees.rs src/network/height.rs src/network/mod.rs src/network/tor.rs src/wallet/address.rs src/wallet/coldcard.rs src/wallet/custom.rs src/wallet/history.rs src/wallet/mod.rs src/wallet/policy.rs src/wallet/proof_of_payment.rs src/wallet/psbt.rs src/wallet/recover.rs src/wallet/sync.rs src/wallet/utxo.rs ndk-home -# Implicit rule search has not been done. -# Modification time never checked. -# File has not been updated. -# recipe to execute (from 'makefile', line 56): - cargo build --target armv7-linux-androideabi --release - @echo "[DONE] $@" - -target/bindings.h: src/bip392/mod.rs src/config.rs src/e.rs src/key/derivation.rs src/key/ec.rs src/key/encoding.rs src/key/encryption.rs src/key/mod.rs src/key/seed.rs src/lib.rs src/network/fees.rs src/network/height.rs src/network/mod.rs src/network/tor.rs src/wallet/address.rs src/wallet/coldcard.rs src/wallet/custom.rs src/wallet/history.rs src/wallet/mod.rs src/wallet/policy.rs src/wallet/proof_of_payment.rs src/wallet/psbt.rs src/wallet/recover.rs src/wallet/sync.rs src/wallet/utxo.rs -# Implicit rule search has not been done. -# Modification time never checked. -# File has not been updated. -# recipe to execute (from 'makefile', line 78): - cbindgen $^ -c cbindgen.toml | grep -v \#include | uniq > $@ - @echo "[DONE] $@" +# Needs to be updated (-q is set). +# variable set hash-table stats: +# Load=0/32=0%, Rehash=0, Collisions=0/3=0% # Not a target: -src/wallet/utxo.rs: +src/e.rs: # Implicit rule search has been done. -# Last modified 2022-08-03 14:46:59.83034858 +# Last modified 2022-04-11 17:54:32 # File has been updated. # Successfully updated. +# variable set hash-table stats: +# Load=0/32=0%, Rehash=0, Collisions=0/0=0% # files hash-table stats: -# Load=44/1024=4%, Rehash=0, Collisions=3/244=1% +# Load=41/1024=4%, Rehash=0, Collisions=14/211=7% # VPATH Search Paths -# No 'vpath' search paths. +# No `vpath' search paths. -# No general ('VPATH' variable) search path. +# No general (`VPATH' variable) search path. -# strcache buffers: 1 (0) / strings = 98 / storage = 1342 B / avg = 13 B -# current buf: size = 8162 B / used = 1342 B / count = 98 / avg = 13 B +# # of strings in strcache: 1 +# # of strcache buffers: 1 +# strcache size: total = 4096 / max = 4096 / min = 4096 / avg = 4096 +# strcache free: total = 4087 / max = 4087 / min = 4087 / avg = 4087 -# strcache performance: lookups = 287 / hit rate = 65% -# hash-table stats: -# Load=98/8192=1%, Rehash=0, Collisions=0/287=0% -# Finished Make data base on Wed Aug 3 17:02:28 2022 +# Finished Make data base on Wed Aug 31 14:04:09 2022 diff --git a/Cargo.toml b/Cargo.toml index fccbbdf..557c1b2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ edition = "2018" keywords = ["bitcoin", "rust-c-ffi"] license = "MIT" name = "stackmate" -version = "0.12.2" +version = "0.13.0" [dependencies] base32 = "0.4.0" diff --git a/src/key/derivation.rs b/src/key/derivation.rs index a0bd129..21b90b1 100644 --- a/src/key/derivation.rs +++ b/src/key/derivation.rs @@ -2,6 +2,7 @@ use crate::e::{ErrorKind, S5Error}; use bitcoin::network::constants::Network; use bitcoin::secp256k1::Secp256k1; use bitcoin::util::bip32::{DerivationPath, ExtendedPrivKey, ExtendedPubKey}; +use bitcoin::secp256k1::{KeyPair, PublicKey, SecretKey}; use serde::{Deserialize, Serialize}; use std::ffi::CString; use std::fmt::Display; @@ -36,6 +37,7 @@ impl ChildKeys { account: u64, ) -> Result { let secp = Secp256k1::new(); + let root = match ExtendedPrivKey::from_str(master_xprv) { Ok(xprv) => xprv, Err(_) => return Err(S5Error::new(ErrorKind::Key, "Invalid Master Key.")), @@ -72,7 +74,19 @@ impl ChildKeys { Err(e) => return Err(S5Error::new(ErrorKind::Key, &e.to_string())), }; let keypair = child_xprv.to_keypair(&secp); - + let public_key = PublicKey::from_keypair(&keypair); + // ENFORCE EVEN PARITY! + let parity = public_key.to_string().remove(1); + let keypair = if parity == '3' { + let mut seckey = SecretKey::from_keypair(&keypair); + seckey.negate_assign(); + let key_pair = KeyPair::from_secret_key(&secp, seckey); + key_pair + } + else{ + keypair + }; + let schnorr_xprv = ExtendedPrivKey::decode(&keypair.secret_bytes()).unwrap(); let schnorr_xpub = ExtendedPubKey::decode(&keypair.public_key().serialize()).unwrap(); diff --git a/src/wallet/policy.rs b/src/wallet/policy.rs index 3010e5d..c7f6032 100644 --- a/src/wallet/policy.rs +++ b/src/wallet/policy.rs @@ -81,11 +81,11 @@ pub fn compile(policy: &str, script_type: ScriptType) -> Result Err(e) => return Err(S5Error::new(ErrorKind::Internal, &e.to_string())), }; let descriptor = match script_type { - ScriptType::WPKH => policy.replace("pk", "wpkh"), + ScriptType::WPKH => policy.replace("pk(", "wpkh("), ScriptType::SH => Descriptor::new_sh(legacy_policy).unwrap().to_string(), ScriptType::WSH => Descriptor::new_wsh(segwit_policy).unwrap().to_string(), ScriptType::SHWSH => Descriptor::new_sh_wsh(segwit_policy).unwrap().to_string(), - ScriptType::TR => policy.replace("pk", "tr"), + ScriptType::TR => policy.replace("pk(", "tr("), }; Ok(descriptor.split('#').collect::>()[0].to_string()) } diff --git a/src/wallet/sync.rs b/src/wallet/sync.rs index 48e6547..9987771 100644 --- a/src/wallet/sync.rs +++ b/src/wallet/sync.rs @@ -32,7 +32,8 @@ pub fn sqlite(config: WalletConfig) -> Result<(), S5Error> { mod tests { use super::*; use std::{env, path::Path}; - use crate::config::{DEFAULT_TESTNET_NODE}; + use crate::key::derivation; + use crate::config::{DEFAULT_TESTNET_NODE,DEFAULT_MAINNET_NODE}; use std::fs; use secp256k1::rand::{thread_rng,Rng}; @@ -52,4 +53,23 @@ mod tests { fs::remove_file(Path::new(&db_path)) .expect("File delete failed"); } + #[test] + fn test_sqlite_tr() { + let root = "xprv9s21ZrQH143K2zHQpUcYuESCAFQaMfu5MoiAVwfi1fc8vzdztF6VFQssNqzGWFmvAsU9PQ2E5aqLxpYyEw349PLDhU1Tnhf7Kjsnge2wM7J"; + let child = derivation::to_hardened_account(root, derivation::DerivationPurpose::Taproot, 0).unwrap(); + let descriptor = format!("tr([{}/{}]{}/*)",child.fingerprint,child.hardened_path.replace("m/",""),child.xprv); + // assert_eq!(descriptor,descriptor_exptected); + print!("{descriptor}"); + let mut rng = thread_rng(); + let random: u16 = rng.gen(); + let db_path: String = env::var("CARGO_MANIFEST_DIR").unwrap() + &random.to_string() + ".db"; + let config = WalletConfig::new(&descriptor, DEFAULT_MAINNET_NODE, None,Some(db_path.clone())).unwrap(); + let status = sqlite(config); + assert_eq!( + (), + status.unwrap() + ); + fs::remove_file(Path::new(&db_path)) + .expect("File delete failed"); + } }