Skip to content

Commit

Permalink
Optimize polkadot wallet generation speed
Browse files Browse the repository at this point in the history
  • Loading branch information
gaopengfei committed Jan 1, 2024
1 parent a047d70 commit 4eca208
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions cryptos/polkadot/polkadot.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
package polkadot

import (
"github.com/centrifuge/go-substrate-rpc-client/v4/signature"
"crypto/rand"
"crypto/sha512"
"github.com/ChainSafe/go-schnorrkel"
"github.com/tyler-smith/go-bip39"
"github.com/vedhavyas/go-subkey/v2"
"golang.org/x/crypto/pbkdf2"
"io"
"math"
"os"
"strings"
"sync"
"unsafe"
"vanity-generator/cryptos"
"vanity-generator/model"
)

var _ cryptos.Generator = (*PolkadotGenerator)(nil)

type PolkadotGenerator struct {
writer io.Writer
entropyPool *sync.Pool
}

func NewPolkadotGenerator() *PolkadotGenerator {
return &PolkadotGenerator{}
out, _ := os.Open(os.DevNull)
return &PolkadotGenerator{
writer: out,
entropyPool: &sync.Pool{
New: func() any {
addr := [128 / 8]byte{}
return addr[:]
},
},
}
}

func (g *PolkadotGenerator) Difficulty(prefix string, suffix string) (count int64) {
Expand All @@ -34,13 +53,22 @@ func (g *PolkadotGenerator) Difficulty(prefix string, suffix string) (count int6
}

func (g *PolkadotGenerator) DoSingle(prefix string, suffix string) *model.Wallet {
entropy, _ := bip39.NewEntropy(128)
mnemonic, _ := bip39.NewMnemonic(entropy)
keyringPair, _ := signature.KeyringPairFromSecret(mnemonic, 0)
if strings.HasPrefix(keyringPair.Address, prefix) && strings.HasSuffix(keyringPair.Address, suffix) {
entropy := g.entropyPool.Get().([]byte)
defer g.entropyPool.Put(entropy)

_, _ = rand.Read(entropy)

seed := pbkdf2.Key(entropy, []byte("mnemonic"), 2048, 64, sha512.New)
ms, _ := schnorrkel.NewMiniSecretKeyFromRaw(*(*[32]byte)(unsafe.Pointer(&seed[0])))

code := ms.Public().Encode()
address := subkey.SS58Encode(code[:], 0)

if strings.HasPrefix(address, prefix) && strings.HasSuffix(address, suffix) {
mnemonic, _ := bip39.NewMnemonic(entropy)
return &model.Wallet{
Private: mnemonic,
Public: keyringPair.Address,
Public: address,
}
}
return nil
Expand Down

0 comments on commit 4eca208

Please sign in to comment.