コード例 #1
0
ファイル: PrivateKeyTest.go プロジェクト: ThePiachu/GoBitTest
func generateFromChecksum(in PrivateKeyDat, WIF string) PrivateKeyDat {
	var answer PrivateKeyDat
	answer.Error = in.Error

	answer.Priv2WIF = in.Priv2WIF
	answer.WIF2Priv = in.WIF2Priv
	answer.WIFChecksum = []string{}
	answer.Minikey = in.Minikey

	hex := mymath.Base582Hex(WIF)
	if mymath.Hex2Base58(hex) != mymath.Base58(WIF) {
		if answer.Error == "" {
			answer.Error = "Invalid Base58 encoding"
		}
		answer.WIFChecksum = []string{WIF, "", "", "", "", "", ""}
		return answer
	}
	answer.WIFChecksum = append(answer.WIFChecksum, WIF)
	answer.WIFChecksum = append(answer.WIFChecksum, mymath.Hex2Str(hex))
	answer.WIFChecksum = append(answer.WIFChecksum, mymath.Hex2Str(hex[0:len(hex)-4]))
	sha := mymath.SingleSHA(hex[0 : len(hex)-4])
	answer.WIFChecksum = append(answer.WIFChecksum, mymath.Hex2Str(sha))
	sha = mymath.SingleSHA(sha)
	answer.WIFChecksum = append(answer.WIFChecksum, mymath.Hex2Str(sha))
	answer.WIFChecksum = append(answer.WIFChecksum, mymath.Hex2Str(sha[0:4]))
	answer.WIFChecksum = append(answer.WIFChecksum, mymath.Hex2Str(hex[len(hex)-4:]))

	return answer
}
コード例 #2
0
ファイル: PrivateKeyTest.go プロジェクト: ThePiachu/GoBitTest
func generateFromPrivate(in PrivateKeyDat, Private string) PrivateKeyDat {
	var answer PrivateKeyDat
	answer.Error = in.Error

	answer.Priv2WIF = []string{}
	answer.WIF2Priv = in.WIF2Priv
	answer.WIFChecksum = in.WIFChecksum
	answer.Minikey = in.Minikey

	curve := bitelliptic.S256()
	if bitecdsa.CheckIsOnCurve(curve, mymath.Str2Big(Private)) == false {
		if answer.Error == "" {
			answer.Error = "Private key is not on curve"
		}
		answer.Priv2WIF = []string{Private, "", "", "", "", "", ""}
		return answer
	}
	priv := append([]byte{0x80}, mymath.Str2Hex(Private)...)
	answer.Priv2WIF = append(answer.Priv2WIF, Private)
	answer.Priv2WIF = append(answer.Priv2WIF, mymath.Hex2Str(priv))
	sha := mymath.SingleSHA(priv)
	answer.Priv2WIF = append(answer.Priv2WIF, mymath.Hex2Str(sha))
	sha = mymath.SingleSHA(sha)
	answer.Priv2WIF = append(answer.Priv2WIF, mymath.Hex2Str(sha))
	answer.Priv2WIF = append(answer.Priv2WIF, mymath.Hex2Str(sha[0:4]))
	priv = append(priv, sha[0:4]...)
	answer.Priv2WIF = append(answer.Priv2WIF, mymath.Hex2Str(priv))
	answer.Priv2WIF = append(answer.Priv2WIF, string(mymath.Hex2Base58(priv)))

	return answer
}
コード例 #3
0
ファイル: PrivateKeyTest.go プロジェクト: ThePiachu/GoBitTest
func generateFromWIF(in PrivateKeyDat, WIF string) PrivateKeyDat {
	var answer PrivateKeyDat
	answer.Error = in.Error

	answer.Priv2WIF = in.Priv2WIF
	answer.WIF2Priv = []string{}
	answer.WIFChecksum = in.WIFChecksum
	answer.Minikey = in.Minikey

	hex := mymath.Base582Hex(WIF)
	if mymath.Hex2Base58(hex) != mymath.Base58(WIF) {
		if answer.Error == "" {
			answer.Error = "Invalid Base58 encoding"
		}
		answer.WIF2Priv = []string{WIF, "", "", ""}
		return answer
	}
	answer.WIF2Priv = append(answer.WIF2Priv, WIF)
	answer.WIF2Priv = append(answer.WIF2Priv, mymath.Hex2Str(hex))
	answer.WIF2Priv = append(answer.WIF2Priv, mymath.Hex2Str(hex[0:len(hex)-4]))
	answer.WIF2Priv = append(answer.WIF2Priv, mymath.Hex2Str(hex[1:len(hex)-4]))

	return answer
}
コード例 #4
0
ファイル: AddressTest.go プロジェクト: ThePiachu/GoBitTest
func addressTest(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	if r.FormValue("Random") != "" {
		var address AddressDat
		curve := bitelliptic.S256()
		priv, x, y, _ := curve.GenerateKey(rand.Reader)
		pub := append(append([]byte{0x04}, mymath.Big2HexWithMinimumLength(x, 32)...), mymath.Big2HexWithMinimumLength(y, 32)...)
		//pub=mymath.Str2Hex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f")

		st2 := mymath.SingleSHA(pub)
		st3 := mymath.Ripemd(st2)
		st4 := append([]byte{0x00}, st3[:]...)
		st5 := mymath.SingleSHA(st4)
		st6 := mymath.SingleSHA(st5)
		st7 := st6[0:4]
		st8 := append(st4[:], st7[:]...)
		st9 := mymath.Hex2Base58(st8)

		address.HashString = []string{
			mymath.Hex2Str(priv),
			mymath.Hex2Str(pub),
			mymath.Hex2Str(st2),
			mymath.Hex2Str(st3),
			mymath.Hex2Str(st4),
			mymath.Hex2Str(st5),
			mymath.Hex2Str(st6),
			mymath.Hex2Str(st7),
			mymath.Hex2Str(st8),
			string(st9)}
		AddressTestTemplate.Execute(w, address)

	} else if r.FormValue("private") != "" {
		var err error
		var address AddressDat
		curve := bitelliptic.S256()
		privs, err := bitecdsa.GenerateFromPrivateKey(mymath.Str2Big(r.FormValue("private")), curve)
		if err != nil {
			if err.Error() == "Private key is not on curve" {
				address = DefaultAddress
				address.Error = "Private key is not on curve"
				AddressTestTemplate.Execute(w, address)
			} else {
				c.Errorf("Err ", err)
				return
			}
		} else {
			priv := mymath.Big2Hex(privs.D)
			x := privs.PublicKey.X
			y := privs.PublicKey.Y
			//priv, x, y, _:=curve.GenerateKey(rand.Reader)
			pub := append(append([]byte{0x04}, mymath.Big2HexWithMinimumLength(x, 32)...), mymath.Big2HexWithMinimumLength(y, 32)...)

			st2 := mymath.SingleSHA(pub)
			st3 := mymath.Ripemd(st2)
			st4 := append([]byte{0x00}, st3[:]...)
			st5 := mymath.SingleSHA(st4)
			st6 := mymath.SingleSHA(st5)
			st7 := st6[0:4]
			st8 := append(st4[:], st7[:]...)
			st9 := mymath.Hex2Base58(st8)

			address.HashString = []string{
				mymath.Hex2Str(priv),
				mymath.Hex2Str(pub),
				mymath.Hex2Str(st2),
				mymath.Hex2Str(st3),
				mymath.Hex2Str(st4),
				mymath.Hex2Str(st5),
				mymath.Hex2Str(st6),
				mymath.Hex2Str(st7),
				mymath.Hex2Str(st8),
				string(st9)}
			AddressTestTemplate.Execute(w, address)
		}

	} else if r.FormValue("public") != "" {
		var address AddressDat

		pub := mymath.Str2Hex(r.FormValue("public"))
		if len(pub) != 65 {
			address.Error = "Invalid public key length"
		} else if pub[0] != 0x04 {
			address.Error = "Invalid public key format"
		}

		st2 := mymath.SingleSHA(pub)
		st3 := mymath.Ripemd(st2)
		st4 := append([]byte{0x00}, st3[:]...)
		st5 := mymath.SingleSHA(st4)
		st6 := mymath.SingleSHA(st5)
		st7 := st6[0:4]
		st8 := append(st4[:], st7[:]...)
		st9 := mymath.Hex2Base58(st8)

		address.HashString = []string{
			"",
			mymath.Hex2Str(pub),
			mymath.Hex2Str(st2),
			mymath.Hex2Str(st3),
			mymath.Hex2Str(st4),
			mymath.Hex2Str(st5),
			mymath.Hex2Str(st6),
			mymath.Hex2Str(st7),
			mymath.Hex2Str(st8),
			string(st9)}
		AddressTestTemplate.Execute(w, address)
	} else if r.FormValue("RIPEMD") != "" {
		var address AddressDat

		st3 := mymath.Str2Hex(r.FormValue("RIPEMD"))
		if len(st3) != 20 {
			address.Error = "Invalid RIPEMD-160 length"
		}

		st4 := append([]byte{0x00}, st3[:]...)
		st5 := mymath.SingleSHA(st4)
		st6 := mymath.SingleSHA(st5)
		st7 := st6[0:4]
		st8 := append(st4[:], st7[:]...)
		st9 := mymath.Hex2Base58(st8)

		address.HashString = []string{
			"", "", "",
			mymath.Hex2Str(st3),
			mymath.Hex2Str(st4),
			mymath.Hex2Str(st5),
			mymath.Hex2Str(st6),
			mymath.Hex2Str(st7),
			mymath.Hex2Str(st8),
			string(st9)}
		AddressTestTemplate.Execute(w, address)
	} else if r.FormValue("RIPEMDWithHash") != "" {
		var address AddressDat

		st4 := mymath.Str2Hex(r.FormValue("RIPEMDWithHash"))
		if len(st4) != 21 {
			address.Error = "Invalid RIPEMD-160 length"
		}
		st3 := st4[1:]
		st5 := mymath.SingleSHA(st4)
		st6 := mymath.SingleSHA(st5)
		st7 := st6[0:4]
		st8 := append(st4[:], st7[:]...)
		st9 := mymath.Hex2Base58(st8)

		address.HashString = []string{
			"", "", "",
			mymath.Hex2Str(st3),
			mymath.Hex2Str(st4),
			mymath.Hex2Str(st5),
			mymath.Hex2Str(st6),
			mymath.Hex2Str(st7),
			mymath.Hex2Str(st8),
			string(st9)}
		AddressTestTemplate.Execute(w, address)
	} else if r.FormValue("FullAddress") != "" {
		var address AddressDat

		st8 := mymath.Str2Hex(r.FormValue("FullAddress"))
		if len(st8) != 25 {
			address.Error = "Invalid address length"
			c.Debugf("%d", len(st8))
		}

		st3 := st8[1:21]
		st4 := append([]byte{0x00}, st3[:]...)
		st5 := mymath.SingleSHA(st4)
		st6 := mymath.SingleSHA(st5)
		st7 := st6[0:4]
		//st8:=append(st4[:], st7[:]...)
		st9 := mymath.Hex2Base58(st8)
		if len(st8) == 25 {
			if st7[3] != st8[24] || st7[2] != st8[23] || st7[1] != st8[22] || st7[0] != st8[21] {
				address.Error = "Invalid checksum"
			}
		}

		address.HashString = []string{
			"", "", "",
			mymath.Hex2Str(st3),
			mymath.Hex2Str(st4),
			mymath.Hex2Str(st5),
			mymath.Hex2Str(st6),
			mymath.Hex2Str(st7),
			mymath.Hex2Str(st8),
			string(st9)}
		AddressTestTemplate.Execute(w, address)
	} else if r.FormValue("Base58") != "" {
		var address AddressDat

		st9 := r.FormValue("Base58")
		st8 := mymath.Base582Hex(st9)
		if len(st8) != 25 {
			address.Error = "Invalid address length"
			c.Debugf("%d", len(st8))
		}

		st3 := st8[1:21]
		st4 := append([]byte{0x00}, st3[:]...)
		st5 := mymath.SingleSHA(st4)
		st6 := mymath.SingleSHA(st5)
		st7 := st6[0:4]
		//st8:=append(st4[:], st7[:]...)
		//st9:=mymath.Hex2Base58(st8)
		if len(st8) == 25 {
			if st7[3] != st8[24] || st7[2] != st8[23] || st7[1] != st8[22] || st7[0] != st8[21] {
				address.Error = "Invalid checksum"
			}
		}

		address.HashString = []string{
			"", "", "",
			mymath.Hex2Str(st3),
			mymath.Hex2Str(st4),
			mymath.Hex2Str(st5),
			mymath.Hex2Str(st6),
			mymath.Hex2Str(st7),
			mymath.Hex2Str(st8),
			string(st9)}
		AddressTestTemplate.Execute(w, address)
	} else {
		AddressTestTemplate.Execute(w, DefaultAddress)
	}

}