示例#1
0
func TestBase58(t *testing.T) {
	// Encode tests
	for x, test := range stringTests {
		tmp := []byte(test.in)
		if res := base58.Encode(tmp); res != test.out {
			t.Errorf("Encode test #%d failed: got: %s want: %s",
				x, res, test.out)
			continue
		}
	}

	// Decode tests
	for x, test := range hexTests {
		b, err := hex.DecodeString(test.in)
		if err != nil {
			t.Errorf("hex.DecodeString failed failed #%d: got: %s", x, test.in)
			continue
		}
		if res := base58.Decode(test.out); bytes.Equal(res, b) != true {
			t.Errorf("Decode test #%d failed: got: %q want: %q",
				x, res, test.in)
			continue
		}
	}

	// Decode with invalid input
	for x, test := range invalidStringTests {
		if res := base58.Decode(test.in); string(res) != test.out {
			t.Errorf("Decode invalidString test #%d failed: got: %q want: %q",
				x, res, test.out)
			continue
		}
	}
}
示例#2
0
文件: dotp.go 项目: mdp/go-dotp
// GetPublicID create the public id from the pubkey
func GetPublicID(publicKey *[32]byte) string {
	address := make([]byte, 0, 33)
	publicKeyHash := sha512.Sum512(publicKey[:])
	address = append(address, publicKey[:]...)
	address = append(address, publicKeyHash[0])
	return b58.Encode(address)
}
示例#3
0
// String returns the extended key as a human-readable base58-encoded string.
func (k *ExtendedKey) String() string {
	if len(k.key) == 0 {
		return "zeroed extended key"
	}

	var childNumBytes [4]byte
	depthByte := byte(k.depth % 256)
	binary.BigEndian.PutUint32(childNumBytes[:], k.childNum)

	// The serialized format is:
	//   version (4) || depth (1) || parent fingerprint (4)) ||
	//   child num (4) || chain code (32) || key data (33) || checksum (4)
	serializedBytes := make([]byte, 0, serializedKeyLen+4)
	serializedBytes = append(serializedBytes, k.version...)
	serializedBytes = append(serializedBytes, depthByte)
	serializedBytes = append(serializedBytes, k.parentFP...)
	serializedBytes = append(serializedBytes, childNumBytes[:]...)
	serializedBytes = append(serializedBytes, k.chainCode...)
	if k.isPrivate {
		serializedBytes = append(serializedBytes, 0x00)
		serializedBytes = paddedAppend(32, serializedBytes, k.key)
	} else {
		serializedBytes = append(serializedBytes, k.pubKeyBytes()...)
	}

	checkSum := chainhash.DoubleHashB(serializedBytes)[:4]
	serializedBytes = append(serializedBytes, checkSum...)
	return base58.Encode(serializedBytes)
}
示例#4
0
func GenerateSin(pubkey []byte) string {

	// FIRST STEP - COMPLETE
	sha_256 := sha256.New()
	sha_256.Write(pubkey)

	// SECOND STEP - COMPLETE
	rip := ripemd160.New()
	rip.Write(sha_256.Sum(nil))

	// THIRD STEP - COMPLETE
	sin_version, _ := hex.DecodeString("0f02")
	pubPrefixed := append(sin_version, rip.Sum(nil)...)

	// FOURTH STEP - COMPLETE
	sha2nd := sha256.New()
	sha2nd.Write([]byte(pubPrefixed))

	sha3rd := sha256.New()
	sha3rd.Write([]byte(sha2nd.Sum(nil)))

	// // FIFTH STEP - COMPLETE
	checksum := sha3rd.Sum(nil)[0:4]

	// SIXTH STEP - COMPLETE
	pubWithChecksum := append(pubPrefixed, checksum...)

	// SIN
	sin := base58.Encode(pubWithChecksum)

	return sin
}
func (ek *ExtendedPublicKey) Serialize() {
	serialized := make([]byte, 78)
	//4 byte: version bytes (mainnet: 0x0488B21E public, 0x0488ADE4 private; testnet: 0x043587CF public, 0x04358394 private)
	copy(serialized[0:], ek.Version)
	//1 byte: depth: 0x00 for master nodes, 0x01 for level-1 derived keys, ....
	serialized[4] = ek.Depth
	//4 bytes: the fingerprint of the parent's key (0x00000000 if master key)
	copy(serialized[5:], ek.ParentFingerPrint)
	//4 bytes: child number. This is ser32(i) for i in xi = xpar/i, with xi the key being serialized. (0x00000000 if master key)
	binary.BigEndian.PutUint32(serialized[9:], ek.ChildNumber)
	//32 bytes: the chain code
	copy(serialized[13:], ek.Chaincode)
	//33 bytes: serP(K) for public keys
	copy(serialized[45:], ek.PublicKey)
	//add 32 checksum bits (derived from the double SHA-256 checksum)
	firstSha256 := sha256.New()
	firstSha256.Write(serialized)
	one := firstSha256.Sum(nil)
	secondSha256 := sha256.New()
	secondSha256.Write(one)
	checksum := secondSha256.Sum(nil)[:4]
	serializedChecksum := make([]byte, 82)
	copy(serializedChecksum[0:], serialized)
	copy(serializedChecksum[78:], checksum)
	//convert to the Base58 representation
	buf := base58.Encode(serializedChecksum)

	fmt.Println(buf)
}
示例#6
0
func BenchmarkBase58Encode(b *testing.B) {
	b.StopTimer()
	data := bytes.Repeat([]byte{0xff}, 5000)
	b.SetBytes(int64(len(data)))
	b.StartTimer()

	for i := 0; i < b.N; i++ {
		base58.Encode(data)
	}
}
示例#7
0
// This example demonstrates how to encode data using the modified base58
// encoding scheme.
func ExampleEncode() {
	// Encode example data with the modified base58 encoding scheme.
	data := []byte("Test data")
	encoded := base58.Encode(data)

	// Show the encoded data.
	fmt.Println("Encoded Data:", encoded)

	// Output:
	// Encoded Data: 25JnwSn7XKfNQ
}
示例#8
0
func generateSinFromPublicKey(key string) string {
	hexa := sha256ofHexString(key)
	hexa = ripemd160ofHexString(hexa)
	versionSinTypeEtc := "0F02" + hexa
	hexa = sha256ofHexString(versionSinTypeEtc)
	hexa = sha256ofHexString(hexa)
	checksum := hexa[0:8]
	hexa = versionSinTypeEtc + checksum
	byta, _ := hex.DecodeString(hexa)
	sin := base58.Encode(byta)
	return sin
}
示例#9
0
// Address returns bitcoin address represented by wallet w.
func (w *HDWallet) Address() string {
	x, y := expand(w.Key)
	four, _ := hex.DecodeString("04")
	padded_key := append(four, append(x.Bytes(), y.Bytes()...)...)
	var prefix []byte
	if bytes.Compare(w.Vbytes, TestPublic) == 0 || bytes.Compare(w.Vbytes, TestPrivate) == 0 {
		prefix, _ = hex.DecodeString("6F")
	} else {
		prefix, _ = hex.DecodeString("00")
	}
	addr_1 := append(prefix, hash160(padded_key)...)
	chksum := dblSha256(addr_1)
	return base58.Encode(append(addr_1, chksum[:4]...))
}
示例#10
0
// String creates the Wallet Import Format string encoding of a WIF structure.
// See DecodeWIF for a detailed breakdown of the format and requirements of
// a valid WIF string.
func (w *WIF) String() string {
	// Precalculate size.  Maximum number of bytes before base58 encoding
	// is one byte for the network, 32 bytes of private key, possibly one
	// extra byte if the pubkey is to be compressed, and finally four
	// bytes of checksum.
	encodeLen := 1 + btcec.PrivKeyBytesLen + 4
	if w.CompressPubKey {
		encodeLen++
	}

	a := make([]byte, 0, encodeLen)
	a = append(a, w.netID)
	// Pad and append bytes manually, instead of using Serialize, to
	// avoid another call to make.
	a = paddedAppend(btcec.PrivKeyBytesLen, a, w.PrivKey.D.Bytes())
	if w.CompressPubKey {
		a = append(a, compressMagic)
	}
	cksum := chainhash.DoubleHashB(a)[:4]
	a = append(a, cksum...)
	return base58.Encode(a)
}
示例#11
0
文件: generate.go 项目: mdp/go-dotp
var generateCmd = &cobra.Command{
	Use:   "generate",
	Short: "Generate a KeyPair based on a seed",
	Long: `Generate a KeyPair based on a seed. If the seed is in the form of a dAuth
	backup key, add the flag --dauth`,
	RunE: func(cmd *cobra.Command, args []string) error {
		var publicKey *[32]byte
		var secretKey *[32]byte
		if len(args) >= 1 {
			seed = args[0]
			if dAuth {
				seed = strings.Replace(seed, " ", "", -1)
				seed = strings.ToUpper(seed)
			}
			fmt.Printf("Creating key with a seed string '%s'\n", seed)
			publicKey, secretKey = dotp.DeriveKeyPair(seed)
		} else {
			publicKey, secretKey, _ = dotp.GenerateKeyPair()
		}
		recPubID := dotp.GetPublicID(publicKey)
		fmt.Printf("PublicID: %s\n", recPubID)
		fmt.Printf("PrivateKey: %s\n", b58.Encode(secretKey[:]))
		return nil
	},
}

func init() {
	RootCmd.AddCommand(generateCmd)
	generateCmd.Flags().BoolVarP(&dAuth, "dauth", "d", false, "Create a KeyPair from a dAuth backup seed")
}
示例#12
0
// String returns the base58-encoded string form of the wallet.
func (w *HDWallet) String() string {
	return base58.Encode(w.Serialize())
}