Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

question about ECkey with golang #227

Closed
zychappy opened this issue Oct 10, 2018 · 6 comments
Closed

question about ECkey with golang #227

zychappy opened this issue Oct 10, 2018 · 6 comments

Comments

@zychappy
Copy link

a priv-pub keypairs generated with "crypto/elliptic" (golang) ,
but this private key in ECkeyDemo.java is used to produce a different pubkey

 public static void main(String[] args) throws CipherException {
	  String privateKey = "0fae33b7613032934d1b69f854e2e4d91812a3d0e6445a1a6a40821d8d8778a6";
    String address = private2Address(ByteArray.fromHexString(privateKey));
    System.out.println("base58Address: " + address);
  System.out.println("================================================================\r\n");

    address = private2Address(null);
    System.out.println("base58Address: " + address);

  }
# the result
Private Key: 0fae33b7613032934d1b69f854e2e4d91812a3d0e6445a1a6a40821d8d8778a6
Public Key: 04cd786f4565fe10d740933e93224c269cdff6cd5432c72dae4ed198d3ae43315f374354bdb5264f2467105543ff196dc2c61a32dbf5fc8a483ae0517e0baeb160

golang:

Private Key: 0fae33b7613032934d1b69f854e2e4d91812a3d0e6445a1a6a40821d8d8778a6
Public Key: 170293c5795f01234f79c208815c6f46c64bddd07af30dfa825bd4bce92567c94273bc93207ac5e20e94d963b0b57213aefdffb7dec44ef0522be9e51d046087

how can i fix it?

@zychappy
Copy link
Author

private to pub uses the
ECPoint.class ,PO[0] = 0x04;// why?

byte[] PO = new byte[X.length + Y.length + 1];
        PO[0] = 0x04;
        System.arraycopy(X, 0, PO, 1, X.length);
        System.arraycopy(Y, 0, PO, X.length + 1, Y.length);

@derekneely
Copy link

Have you had any luck in sorting this out? I'm trying to sort out how to issue an address via Go as well but having little luck getting the private key generated to match the private key. I believe my logic of pulling, adding, hashing, and adding more bytes is all correct but the initial pub/priv key generation is where I'm struggling.

@zychappy
Copy link
Author

zychappy commented Mar 8, 2019

@derekneely
here is my way:

privKey *secp256k1.PrivateKey
pubKey  *secp256k1.PublicKey
px:=paddedAppend(32, 0, pubKey.X.Bytes())//make sure px,py is a [32]byte,if byte() is not enough len ,add ZERO
py:=paddedAppend(32, 0, pubKey.Y.Bytes())
addresspk:=append(px,py)
hash:=sha3.NewLegacyKeccak256(addresspk)
address := hash[len(hash)-20:]//only need last 20 bytes
a:=prefix+address//prefix mainnet:0x41
b:=sh256X2(a)// 2times sha256
checksum:=a[:4]
base58Addr:=base58.Encode(a+checksum)

@derekneely
Copy link

@zychappy thank you for the replay. would you have a little more context on this with regards to how your generating those priv/public keys? I think that is ultimately where i'm getting stuck. And I think I understand what 'paddedAppend' is doing but that is a function of your own correct?

Thank you again for your response and helping me work through this.

@derekneely
Copy link

@zychappy thank you so much for you guidance. I took what you gave and was able to work with it. I was so close in my initial implementation. The missing key (no pun intended), for me, was the 'NewLegacyKecak256'. Attached below is the code I wound up with lined up against your code (should anyone else run across this) along with a bunch of logging so you can see what is going on along the way. Thanks again.

package main

import (
	"crypto/sha256"
	"fmt"
	"github.com/ethereum/go-ethereum/crypto"
	"github.com/sasaxie/go-client-api/common/base58"
	"golang.org/x/crypto/sha3"
)

func main() {
key, _ := crypto.GenerateKey()
	priv := key.D.Bytes() // privKey *secp256k1.PrivateKey
	pubX := key.X.Bytes() // pubKey  *secp256k1.PublicKey
	pubY := key.Y.Bytes()
	// px:=paddedAppend(32, 0, pubKey.X.Bytes())//make sure px,py is a [32]byte,if byte() is not enough len ,add ZERO
	// py:=paddedAppend(32, 0, pubKey.Y.Bytes())
	pub := append(pubX,pubY...)
	// hash := sha3.NewLegacyKeccak256(addresspk)
	hash := sha3.NewLegacyKeccak256() // the missing piece
	hash.Write(pub)
	hashed := hash.Sum(nil)
	address := hashed[len(hashed)-20:]//only need last 20 bytes
	// a:=prefix+address//prefix mainnet:0x41
	addr41 := append([]byte{0x41}, address...)
	// b:=sh256X2(a)// 2times sha256
	h2561 := sha256.Sum256(addr41)
	h2562 := sha256.Sum256(h2561[:])
	// checksum:=a[:4]
	checksum:=h2562[:4]
	// base58Addr:=base58.Encode(a+checksum)
	naddr := append(addr41, checksum...)
	b58 := base58.Encode(naddr)

	fmt.Println("Public key X: (" + fmt.Sprintf("%d", len(pubX)) + ") " + fmt.Sprintf("%x", pubX))
	fmt.Println("Public key Y: (" + fmt.Sprintf("%d", len(pubY)) + ") " + fmt.Sprintf("%x", pubY))
	fmt.Println("Public key xored: (" + fmt.Sprintf("%d", len(pub)) + ") " + fmt.Sprintf("%x", pub))
	fmt.Println("Private key: (" + fmt.Sprintf("%d", len(priv)) + ") " + fmt.Sprintf("%x", priv))
	fmt.Println("hashed: (" + fmt.Sprintf("%d", len(hashed)) + ") " + fmt.Sprintf("%x", hashed))
	fmt.Println("address: (" + fmt.Sprintf("%d", len(address)) + ") " + fmt.Sprintf("%x", address))
	fmt.Println("addr41: (" + fmt.Sprintf("%d", len(addr41)) + ") " + fmt.Sprintf("%x", addr41))
	fmt.Println("h2562: (" + fmt.Sprintf("%d", len(h2562)) + ") " + fmt.Sprintf("%x", h2562))
	fmt.Println("checksum: (" + fmt.Sprintf("%d", len(naddr)) + ") " + fmt.Sprintf("%x", naddr))
	fmt.Println("b58: (" + fmt.Sprintf("%d", len(b58)) + ") " + b58)

@zychappy
Copy link
Author

zychappy commented Mar 9, 2019

@derekneely
it is a bug! I l have made a mistake!!!--->pub := append(pubX,pubY...)
MAKE SURE pub key is a [64]byte
better practice like below, you can check

func paddedAppend(size int, dst, src []byte) []byte {
	for i := 0; i < size-len(src); i++ {
		dst = append(dst, 0)
	}
	return append(dst, src...)
}
.......
priv := key.D.Bytes() // privKey *secp256k1.PrivateKey
	pubX := key.X.Bytes() // pubKey  *secp256k1.PublicKey
	pubY := key.Y.Bytes()
pub := make([]byte, 0, 64)
	px := make([]byte, 0, 32)
	py := make([]byte, 0, 32)
	px = paddedAppend(32, px, pubX)
	py = paddedAppend(32, py, pubY)
	pub = append(px, py...)

@zychappy zychappy closed this as completed Mar 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants