主页 > imtoken钱包app下载 > 助记词生成种子和公钥

助记词生成种子和公钥

imtoken钱包app下载 2023-09-04 05:11:06

2019年独角兽企业招聘Python工程师标准>>>

hot3.png

HD钱包其实就是管理着一堆Seed和keys。 记住 32byte 私钥是反人类的事情。 因此,就有了助记符(Mnemonics)。 本文讨论从助记符生成种子比特币私钥助记词有几个,反之亦然。

首先借用Mastering Bitcoin书上的几张图来讲解HD钱包

fd5ad728308b3372bb74cdc1d2ece4afacc.jpg

下面是改造框图

4e4899eaf14bb4c2edcac512933023a15c4.jpg

284391a8f3afb36fd499cf9edf39d6acd52.jpg

图片借用至今比特币私钥助记词有几个,下面是代码

如何产生初始熵(entropy)

比特币私钥在哪_比特币私钥助记词有几个_比特币 私钥

func NewEntropy(bitSize int) ([]byte, error) {
	err := validateEntropyBitSize(bitSize)
	if err != nil {
		return nil, err
	}
	entropy := make([]byte, bitSize/8)
	_, err = rand.Read(entropy)

比特币私钥在哪_比特币私钥助记词有几个_比特币 私钥

return entropy, err }

validateEntropyBitsize : bitSize 只能是 128, 256, 512.必须是 8 的倍数

如何生成校验和

func addChecksum(data []byte) []byte {
	// Get first byte of sha256
	hasher := sha256.New()
	hasher.Write(data)

比特币私钥在哪_比特币私钥助记词有几个_比特币 私钥

hash := hasher.Sum(nil) firstChecksumByte := hash[0] // len() is in bytes so we divide by 4 checksumBitLength := uint(len(data) / 4) // For each bit of check sum we want we shift the data one the left // and then set the (new) right most bit equal to checksum bit at that index

比特币私钥在哪_比特币私钥助记词有几个_比特币 私钥

// staring from the left dataBigInt := new(big.Int).SetBytes(data) for i := uint(0); i < checksumBitLength; i++ { // Bitshift 1 left dataBigInt.Mul(dataBigInt, bigTwo) // Set rightmost bit if leftmost checksum bit is set if uint8(firstChecksumByte&(1<<(7-i))) > 0 {

比特币 私钥_比特币私钥助记词有几个_比特币私钥在哪

dataBigInt.Or(dataBigInt, bigOne) } } return dataBigInt.Bytes() }

剩下的就是把bits分成12段,然后查字典,不用说了

字典在这里

测试向量在这里

在这里验证