Comments (6)
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.
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.
@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.
@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.
@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.
@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)
- [email protected]
- 0x4e51f877a5fdf94cc4bb11f4c1331b43596ed071 HOT 1
- 2023-08-21T11:28:32.9259909Z http.https://github.com/.extraheader
- Tp
- when start wallet-cli.jar, Using any of the three base commands doesn't work? HOT 7
- Don't build ("Could not determine java version from '21'.") HOT 3
- Sing wallet HOT 2
- Importing a 128 character privately HOT 6
- A
- TDRJ2J8mCSvb5aWRCLicCvSaw5dw7UH1oN
- zukov44444@@@A
- BTTCwallet2
- CMBs Throw backs
- https://github.com/tronprotocol/wallet-cli HOT 1
- Sending tron to exodus using nemonic new home: HOT 2
- I need withdrawal 10,000 or all of it
- Tron Tech HOT 1
- Block and unable to withdraw HOT 2
- TS1RPuPr7MHtoDse7mo8x8o3YNyWhKtJ2e
- Aung Thu Soe HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wallet-cli.