Giter Club home page Giter Club logo

Comments (6)

zychappy avatar zychappy commented on August 30, 2024

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);

from wallet-cli.

derekneely avatar derekneely commented on August 30, 2024

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.

from wallet-cli.

zychappy avatar zychappy commented on August 30, 2024

@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)

from wallet-cli.

derekneely avatar derekneely commented on August 30, 2024

@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.

from wallet-cli.

derekneely avatar derekneely commented on August 30, 2024

@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)

from wallet-cli.

zychappy avatar zychappy commented on August 30, 2024

@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...)

from wallet-cli.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.