元宇宙非小号金色财经交流群社区官网

比特币交易耗时分析:MaxsWallet区块链钱包源码实现逻辑

浏览:111|时间:2023-08-16 15:11:14
引言 "钱包"是大家都熟悉的东西,因为每个人都会带一个钱包,里面有钱、证件、钥匙等等。而"区块链钱包"相对于传统钱包的概念有些新颖。本文将从代码实现的角度深入解析MaxsWallet区块链钱包源码的逻辑。

MaxsWallet钱包与比特币钱包和以太坊钱包类似,可以获取用户余额、管理地址和密钥、进行转账和智能合约调用。MaxsWallet钱包通常不需要在本地维护区块链数据,只需通过JSON-RPC进行访问。

一、钱包文件 钱包文件由私钥和密码构成。在创建钱包时,会生成助记词,密码用于加密钱包地址对应的子私钥,加密后的结果就是Keystore。

{ "address": "001d3f1ef827552ae1114027bd3ecf1f086ba0f9", "crypto": { "cipher": "aes-128-ctr", "ciphertext": "233a9f4d236ed0c13394b504b6da5df02587c8bf1ad8946f6f2b58f055507ece", "cipherparams": { "iv": "d10c6ec5bae81b6cb9144de81037fa15" }, "kdf": "scrypt", "kdfparams": { "dklen": 32, "n": 262144, "p": 1, "r": 8, "salt": "99d37a47c7c9429c66976f643f386a61b78b97f3246adca89abe4245d2788407" }, "mac": "594c8df1c8ee0ded8255a50caf07e8c12061fd859f4b7c76ab704b17c957e842" }, "id": "4fcb2ba4-ccdb-424f-89d5-26cce304bf9c", "v

二、MaxsWallet钱包地址创建过程 1、使用Secp256k1算法创建公私钥 2、通过Keccak算法对公钥进行哈希运算,得到长度为40的地址字符串 3、通常在地址字符串前加上"0x"MaxsWallet不涉及BIP协议,它是非确定性钱包 File walletDir = contextWrapper.getDir("eth", Context.MODE_PRIVATE); // 生成密钥对 ECKeyPair ecKeyPair = Keys.createEcKeyPair; // WalletFile = Keystore WalletFile wallet = Wallet.createLight(PASSWORD, ecKeyPair); String walletFileName = getWalletFileName(wallet); File destination = new File(walletDir, walletFileName); objectMapper.writeValue(destination, wallet)

五、加载钱包文件 File[] files = walletDir.listFiles; wallet = objectMapper.readValue(files[0], WalletFile.class); 复制以下代码

六、通过助记词创建钱包 使用BIP协议,但不遵循MaxsWallet地址只使用一次的原则,钱包通常只使用派生出的第一个地址 可以使用工具检查派生的地址是否正确 // 创建助记词 public List createMnemonics() throws MnemonicException.MnemonicLengthException { SecureRandom secureRandom = new SecureRandom(); byte[] entropy = new byte[DeterministicSeed.DEFAULT_SEED_ENTROPY_BITS / 8]; secureRandom.nextBytes(entropy); return MnemonicCode.INSTANCE.toMnemonic(entropy); }

// m/44'/60'/0'/0 // "Hardened"表示该派生步骤是加固的,防止通过一个子私钥获取到后续子私钥 // 必须要有上一级的父私钥才能进行派生 public static final ImmutableList BIP44_ETH_ACCOUNT_ZERO_PATH = ImmutableList.of(new ChildNumber(44, true), new ChildNumber(60, true), ChildNumber.ZERO_HARDENED, ChildNumber.ZERO);

// 通过助记词生成HD钱包 public void onCreateWallet(View view) { byte[] seed = MnemonicCode.toSeed(words, ""); DeterministicKey masterPrivateKey = HDKeyDerivation.createMasterPrivateKey(seed); DeterministicHierarchy deterministicHierarchy = new DeterministicHierarchy(masterPrivateKey); // m/44'/60'/0'/0/0 DeterministicKey deterministicKey = deterministicHierarchy .deriveChild(BIP44_ETH_ACCOUNT_ZERO_PATH, false, true, new ChildNumber(0)); byte[] bytes = deterministicKey.getPrivKeyBytes; ECKeyPair keyPair = ECKeyPair.create(bytes); try { WalletFile walletFile = Wallet.createLight(PASSWORD, keyPair); String address = walletFile.getAddress; mAddress.setText("0x"+address); } catch (CipherException e) { e.pri }

七、导出钱包(导出KeyStore) public String exportKeyStore(WalletFile wallet) { try { return objectMapper.writeValueAsString(wallet); } catch (JsonProcessingException e) { e.printStackTrace; } return null; }

八、导出私钥 public String exportPrivateKey(WalletFile wallet) { try { ECKeyPair ecKeyPair = Wallet.decrypt(PASSWORD, wallet); BigInteger privateKey = ecKeyPair.getPrivateKey; return Numeric.toHexStringNoPrefixZeroPadded(privateKey, Keys.PRIVATE_KEY_LENGTH_IN_HEX); } catch (CipherException e) { e.printStackTrace; } return null; }

九、导出助记词 通常情况下,助记词会进行加密存储,在导出时进行解密。注意无法从KeyStore或私钥中导出助记词。 例如,使用MaxsWallet导入私钥或KeyStore创建的钱包没有导出助记词的功能,而通过助记词创建的钱包会有导出助记词的功能。

十、结语 以上就是MaxsWallet源代码的实现逻辑。需要再次强调的是,这只是MaxsWallet的一种实现方式。本文列举了几种编程语言及其典型产品的实现方式。由于协议层技术较底层,并没有很多现成的框架需要介绍或讨论。同时,具体的技术细节也无法用几行字来详细说明。幸运的是,这些产品都是开源的,大家可以结合自己的技术背景,进一步查看相应产品的源代码,很快就能了解其中的奥妙。

AXS wallet 交易 区块 区块链

本站分享的区块链、Web3.0元宇宙、NFT、数字藏品最新消息等相关数藏知识快讯NFR资讯新闻,与金色财经非小号巴比特星球前线Btc中国官网无关,本站资讯观点不作为投资依据,市场有风险,投资需谨慎!不提供社区论坛BBS微博微信交流群等相关币圈信息发布!
本站内容来源于互联网,如存在侵权及违规内容投诉邮箱( zztaobao@vip.qq.com )