/// <inheritdoc /> public void ImportMemberKey(string password, string mnemonic) { Guard.NotEmpty(password, nameof(password)); Guard.NotEmpty(mnemonic, nameof(mnemonic)); // Get the extended key. ExtKey extendedKey; try { extendedKey = HdOperations.GetExtendedKey(mnemonic); } catch (NotSupportedException ex) { this.logger.LogTrace("Exception occurred: {0}", ex.ToString()); this.logger.LogTrace("(-)[EXCEPTION]"); if (ex.Message == "Unknown") { throw new WalletException("Please make sure you enter valid mnemonic words."); } throw; } // Create a wallet file. string encryptedSeed = extendedKey.PrivateKey.GetEncryptedBitcoinSecret(password, this.network).ToWif(); this.Wallet.EncryptedSeed = encryptedSeed; this.SaveWallet(); this.logger.LogTrace("(-)"); }
/// <summary> /// Initializes the cross-chain transfer tests. /// </summary> /// <param name="network">The network to run the tests for.</param> public CrossChainTestBase(Network network = null, Network counterChainNetwork = null) { this.network = network ?? FederatedPegNetwork.NetworksSelector.Regtest(); this.counterChainNetwork = counterChainNetwork ?? Networks.Stratis.Regtest(); this.federatedPegOptions = new FederatedPegOptions(counterChainNetwork); NetworkRegistration.Register(this.network); this.loggerFactory = Substitute.For <ILoggerFactory>(); this.nodeLifetime = new NodeLifetime(); this.logger = Substitute.For <ILogger>(); this.signals = Substitute.For <ISignals>(); this.asyncProvider = new AsyncProvider(this.loggerFactory, this.signals, this.nodeLifetime); this.loggerFactory.CreateLogger(null).ReturnsForAnyArgs(this.logger); this.dateTimeProvider = DateTimeProvider.Default; this.opReturnDataReader = new OpReturnDataReader(this.loggerFactory, this.federatedPegOptions); this.blockRepository = Substitute.For <IBlockRepository>(); this.fullNode = Substitute.For <IFullNode>(); this.withdrawalTransactionBuilder = Substitute.For <IWithdrawalTransactionBuilder>(); this.federationWalletManager = Substitute.For <IFederationWalletManager>(); this.federationWalletSyncManager = Substitute.For <IFederationWalletSyncManager>(); this.FederationWalletTransactionHandler = Substitute.For <IFederationWalletTransactionHandler>(); this.walletFeePolicy = Substitute.For <IWalletFeePolicy>(); this.connectionManager = Substitute.For <IConnectionManager>(); this.dBreezeSerializer = new DBreezeSerializer(this.network.Consensus.ConsensusFactory); this.ibdState = Substitute.For <IInitialBlockDownloadState>(); this.wallet = null; this.federationGatewaySettings = Substitute.For <IFederationGatewaySettings>(); this.ChainIndexer = new ChainIndexer(this.network); this.federationGatewaySettings.TransactionFee.Returns(new Money(0.01m, MoneyUnit.BTC)); // Generate the keys used by the federation members for our tests. this.federationKeys = new[] { "ensure feel swift crucial bridge charge cloud tell hobby twenty people mandate", "quiz sunset vote alley draw turkey hill scrap lumber game differ fiction", "exchange rent bronze pole post hurry oppose drama eternal voice client state" }.Select(m => HdOperations.GetExtendedKey(m)).ToArray(); SetExtendedKey(0); this.fundingTransactions = new List <Transaction>(); this.blockDict = new Dictionary <uint256, Block>(); this.blockDict[this.network.GenesisHash] = this.network.GetGenesis(); this.blockRepository.GetBlocks(Arg.Any <List <uint256> >()).ReturnsForAnyArgs((x) => { var hashes = x.ArgAt <List <uint256> >(0); var blocks = new List <Block>(); for (int i = 0; i < hashes.Count; i++) { blocks.Add(this.blockDict.TryGetValue(hashes[i], out Block block) ? block : null); } return(blocks); }); }
public void ShouldProduceCorrectExtPubKey() { // This test is a replicate of 'should produce correct extpubkey' test in Angular (City Hub). var passphrase = ""; var recoveryPhrase = "mystery problem faith negative member bottom concert bundle asthma female process twelve"; var walletPassword = "******"; var network = new CityMain(); var chainCode = new byte[] { 166, 209, 155, 88, 182, 124, 193, 127, 139, 220, 152, 1, 213, 145, 245, 80, 118, 188, 53, 211, 33, 37, 158, 40, 118, 207, 42, 83, 219, 233, 188, 161 }; // Intentionally blank lines to be // line-compatible with JavaScript unit test. // The master node is made without network information, always same no matter network. ExtKey masterNode = HdOperations.GetExtendedKey(recoveryPhrase, passphrase); ExtPubKey extPubKey = masterNode.Neuter(); // masterNode in C# and JavaScript should both have the same chaincode at this step, verify: Assert.Equal(chainCode, masterNode.ChainCode); Assert.Equal(chainCode, extPubKey.ChainCode); // Get the private key in WIF format and verify. var xprv = masterNode.GetWif(network).ToWif(); Assert.Equal("xprv9s21ZrQH143K3ignAgXxaBbyVbrCTuUJSHNrMwdTa7n4i1zpFsiWdRCerTWrKaZXVehZFbXcFtwnmndrzC1AVs1BueiycVSxXjMyhXHpBqx", xprv); // Get the public key in WIF format and verify. var xpub = extPubKey.GetWif(network).ToWif(); Assert.Equal("xpub661MyMwAqRbcGCmFGi4xwKYi3dggsNC9oWJTAL358TK3apKxoR2mBDX8hkD1cUePJyzkkNWffsfZEzkExFXN1sNfJoVw161LfQyCuNVDadK", xpub); // Get the "root" address. var address = masterNode.PrivateKey.PubKey.GetAddress(network); // Ensure that the generated address is a City Chain address and not Bitcoin. Assert.Equal("CQtq75vu4bAceku6FmenWBh35i1Y4oskdu", address.ToString()); var publicNode = masterNode.Derive(new KeyPath("m/44'/1926'/0'/0/0")); var changeNode = masterNode.Derive(new KeyPath("m/44'/1926'/0'/1/0")); Assert.Equal("CPtzM2XwLCVS3L6BFK1xYsCcGqgrgsxHrP", publicNode.PrivateKey.PubKey.GetAddress(network).ToString()); Assert.Equal("CY35ZGxzZBYHKyNV4KWKunYLHsTWejVSdR", changeNode.PrivateKey.PubKey.GetAddress(network).ToString()); // Get the first account in the HD wallet, this is same as the level stored in the wallet files. var accountNode = masterNode.Derive(new KeyPath("m/44'/1926'/0'")); var accountExtPubKey = accountNode.Neuter().GetWif(network).ToWif(); Assert.Equal("xpub6BwCLtuvjt6TZ495sJruY1UWPXg6ME9HA92ro75YDHvGpPKY6kQ6ifp6DEszRpJGMtdBvWBaSn4gQDTz4Ctm5m1BMLeFUh3F19mTXA4s3bE", accountExtPubKey); // Create a wallet file. string encryptedSeed = masterNode.PrivateKey.GetEncryptedBitcoinSecret(walletPassword, network).ToWif(); Assert.Equal("6PYW8DRnFZSu3CVC3NfghKFSozZE8gmf76GmsGrrA9ciWbv6F6HhVSKkEQ", encryptedSeed); }
public void pubKeysDerivedFromExtendedPrivateAndPublicKeysMatch() { string password = "******"; string passphrase = password; string mnemonic = "chalk call anger chase endless level slow sleep coast left sand enter save bind curious puzzle stadium volume mixture shuffle hurry gas borrow believe"; ExtKey extendedKey = HdOperations.GetExtendedKey(mnemonic, passphrase); string encryptedSeed = extendedKey.PrivateKey.GetEncryptedBitcoinSecret(password, this.Network).ToWif(); Key privateKey = HdOperations.DecryptSeed(encryptedSeed, password, this.Network); string accountHdPath = HdOperations.GetAccountHdPath(COINTYPE, 0); string path = HdOperations.CreateHdPath(COINTYPE, 0, false, 0); ExtPubKey accountExtPubKey = HdOperations.GetExtendedPublicKey(privateKey, extendedKey.ChainCode, accountHdPath); var subjectPubKey = HdOperations.GeneratePublicKey(accountExtPubKey.ToString(this.Network), 0, false, this.Network); var subjectPrivKey = HdOperations.GetExtendedPrivateKey(privateKey, extendedKey.ChainCode, path, this.Network); Assert.Equal(subjectPubKey.ScriptPubKey, subjectPrivKey.PrivateKey.PubKey.ScriptPubKey); }