Skip to content

Commit

Permalink
feat: update starknet pri, pub, addr same with braavos.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhangguiguang committed Jun 30, 2023
1 parent 21e0051 commit dfc3e9a
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 46 deletions.
52 changes: 39 additions & 13 deletions core/starknet/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,45 @@ import (
"errors"
"math/big"

"github.com/btcsuite/btcd/btcutil/hdkeychain"
"github.com/btcsuite/btcd/chaincfg"
hexTypes "github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/coming-chat/go-aptos/crypto/derivation"
"github.com/coming-chat/wallet-SDK/core/base"
"github.com/dontpanicdao/caigo"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/crypto"
"github.com/tyler-smith/go-bip39"
)

type Account struct {
privateKey *big.Int
}

func grindKey(seed []byte) (*big.Int, error) {
// order := caigo.Curve.N
// max := big.NewInt(0).Exp(big.NewInt(2), big.NewInt(256), nil)
// limit := big.NewInt(0).Sub(max, big.NewInt(0).Mod(max, order))
limit := caigo.Curve.N
func grindKey(keySeed []byte) (*big.Int, error) {
keyValueLimit := caigo.Curve.N
sha256EcMaxDigest := big.NewInt(0).Exp(big.NewInt(2), big.NewInt(256), nil)
maxAllowedVal := big.NewInt(0).Sub(sha256EcMaxDigest, big.NewInt(0).Mod(sha256EcMaxDigest, keyValueLimit))

for i := 0; i < 100000; i++ {
bb := append(seed, big.NewInt(int64(i)).Bytes()...)
key := sha256.Sum256(bb)
kb := big.NewInt(0).SetBytes(key[:])
if kb.Cmp(limit) == -1 {
return kb, nil
key := hashKeyWithIndex(keySeed, i)
if key.Cmp(maxAllowedVal) == -1 {
return big.NewInt(0).Mod(key, keyValueLimit), nil
}
}
return nil, errors.New("grindKey is broken: tried 100k vals")
}

func hashKeyWithIndex(seed []byte, i int) *big.Int {
var payload []byte
if i == 0 {
payload = append(seed, 0)
} else {
payload = append(seed, big.NewInt(int64(i)).Bytes()...)
}
hash := sha256.Sum256(payload)
return big.NewInt(0).SetBytes(hash[:])
}

func IsValidPrivateKey(key string) bool {
_, err := AccountWithPrivateKey(key)
return err == nil
Expand All @@ -43,11 +54,26 @@ func NewAccountWithMnemonic(mnemonic string) (*Account, error) {
if err != nil {
return nil, err
}
key, err := derivation.DeriveForPath("m/44'/9004'/0'/0", seed)
masterKey, err := hdkeychain.NewMaster(seed, &chaincfg.MainNetParams)
if err != nil {
return nil, err
}
path, err := accounts.ParseDerivationPath("m/44'/9004'/0'/0/0")
if err != nil {
return nil, err
}
key := masterKey
for _, n := range path {
key, err = key.DeriveNonStandard(n)
if err != nil {
return nil, err
}
}
privateKey, err := key.ECPrivKey()
if err != nil {
return nil, err
}
prikey, err := grindKey(key.Key)
prikey, err := grindKey(crypto.FromECDSA(privateKey.ToECDSA()))
if err != nil {
return nil, err
}
Expand Down
13 changes: 11 additions & 2 deletions core/starknet/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ func TestAccount(t *testing.T) {
require.Equal(t, account.PublicKey(), account2.PublicKey())
require.Equal(t, account.Address(), account2.Address())

require.Equal(t, account.Address(), "0x6c3642d74e5053a135f9ab59ff71d14992c2525c6e65c68363bf8f1fce58d49")

t.Log(prikey)
t.Log(account.PublicKeyHex())
t.Log(account.Address())

require.Equal(t, account.Address(), "0x8debaf4740ac184b2e879d4d3fd773f2c7f5d453b795212d4098899a73fc19")
}

func TestAccount_ImportPrivateKey(t *testing.T) {
Expand All @@ -67,6 +67,15 @@ func TestAccount_ImportPrivateKey(t *testing.T) {
require.Equal(t, accountHex.Address(), "0x7d090c124f2cac618e5b53ad97cdb204debc61e9fc63f94d63f4f75a183ceef")
}

func TestGrindKey(t *testing.T) {
prikey := "86F3E7293141F20A8BAFF320E8EE4ACCB9D4A4BF2B4D295E8CEE784DB46E0519"
seed, ok := big.NewInt(0).SetString(prikey, 16)
require.True(t, ok)
res, err := grindKey(seed.Bytes())
require.Nil(t, err)
require.Equal(t, res.Text(16), "5c8c8683596c732541a59e03007b2d30dbbbb873556fe65b5fb63c16688f941")
}

func TestAccount22(t *testing.T) {
gw := gateway.NewClient(gateway.WithChain(env))

Expand Down
23 changes: 1 addition & 22 deletions core/starknet/address_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,34 +37,13 @@ func EncodePublicKeyToAddress(publicKey string) (string, error) {
}

func encodePublicKeyToAddressArgentX(publicKey string) (string, error) {
txn, err := newDeployAccountTransactionForArgentX(publicKey, 0)
txn, err := newDeployAccountTransaction(publicKey, 0)
if err != nil {
return "", err
}
return types.BigToHex(txn.ContractAddress), nil
}

// func encodePublicKeyToAddressBraavos(publicKey string) (string, error) {
// pub, err := new(felt.Felt).SetString(publicKey)
// if err != nil {
// return "", base.ErrInvalidPublicKey
// }
// callerAddress, _ := new(felt.Felt).SetString("0x0000000000000000000000000000000000000000")
// classHash, _ := new(felt.Felt).SetString("0x03131fa018d520a037686ce3efddeab8f28895662f019ca3ca18a626650f7d1e")
// data1, _ := new(felt.Felt).SetString("0x5aa23d5bb71ddaa783da7ea79d405315bafa7cf0387a74f4593578c3e9e6570")
// data2, _ := new(felt.Felt).SetString("0x2dd76e7ad84dbed81c314ffe5e7a7cacfb8f4836f01af4e913f275f89a3de1a")
// data3, _ := new(felt.Felt).SetString("0x1")
// data4 := pub
// callData := []*felt.Felt{
// data1,
// data2,
// data3,
// data4,
// }
// address := core.ContractAddress(callerAddress, classHash, pub, callData)
// return address.String(), nil
// }

// Warning: starknet cannot support decode address to public key
func DecodeAddressToPublicKey(address string) (string, error) {
return "", base.ErrUnsupportedFunction
Expand Down
6 changes: 3 additions & 3 deletions core/starknet/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func (c *Chain) SendSignedTransaction(signedTxn base.SignedTransaction) (hash *b
// now resend the original txn
txn.invokeTxn.details = types.ExecuteDetails{
Nonce: big.NewInt(1),
MaxFee: big.NewInt(0).SetUint64(1e14 + random(1e12)),
MaxFee: big.NewInt(0).SetUint64(1e14 + random(1e11)),
}
resp, err = caigoAccount.Execute(context.Background(), txn.invokeTxn.calls, txn.invokeTxn.details)
if err != nil {
Expand Down Expand Up @@ -362,14 +362,14 @@ func (c *Chain) BuildDeployAccountTransaction(publicKey string, maxFee string) (
var feeInt *big.Int
var ok bool
if maxFee == "" {
feeInt = big.NewInt(0).SetUint64(1e15 + random(1e13))
feeInt = big.NewInt(0).SetUint64(1e15 + random(1e12))
} else {
if feeInt, ok = big.NewInt(0).SetString(maxFee, 10); !ok {
return nil, base.ErrInvalidAmount
}
}

txn, err := newDeployAccountTransactionForArgentX(publicKey, c.network)
txn, err := newDeployAccountTransaction(publicKey, c.network)
if err != nil {
return nil, err
}
Expand Down
46 changes: 40 additions & 6 deletions core/starknet/transaction_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,56 @@ func (txn *DeployAccountTransaction) SignedTransactionWithAccount(account base.A
}, nil
}

func newDeployAccountTransactionForArgentX(pubkey string, network Network) (*DeployAccountTransaction, error) {
// var newDeployAccountTransaction = newDeployAccountTransactionForArgentX
var newDeployAccountTransaction = newDeployAccountTransactionForBraavos

// func newDeployAccountTransactionForArgentX(pubkey string, network Network) (*DeployAccountTransaction, error) {
// pubData, err := hexTypes.HexDecodeString(pubkey)
// if err != nil {
// return nil, base.ErrInvalidPublicKey
// }
// pubkeyInt := big.NewInt(0).SetBytes(pubData)

// txn := DeployAccountTransaction{
// ClassHash: types.HexToBN("0x25ec026985a3bf9d0cc1fe17326b245dfdc3ff89b8fde106542a3ea56c5a918"),
// ContractAddressSalt: pubkeyInt,
// ConstructorCallData: []*big.Int{
// types.HexToBN("0x33434ad846cdd5f23eb73ff09fe6fddd568284a0fb7d1be20ee482f044dabe2"),
// types.HexToBN("0x79dc0da7c54b95f10aa182ad0a46400db63156920adb65eca2654c0945a463"),
// types.HexToBN("0x2"),
// pubkeyInt,
// types.HexToBN("0x0"),
// },
// Version: big.NewInt(1),
// MaxFee: big.NewInt(2e14), // 0.0002
// Nonce: big.NewInt(0),

// Network: network,
// }

// callerAddress := big.NewInt(0)
// txn.ContractAddress, err = txn.ComputeContractAddress(callerAddress)
// if err != nil {
// return nil, err
// }
// return &txn, nil
// }

func newDeployAccountTransactionForBraavos(pubkey string, network Network) (*DeployAccountTransaction, error) {
pubData, err := hexTypes.HexDecodeString(pubkey)
if err != nil {
return nil, base.ErrInvalidPublicKey
}
pubkeyInt := big.NewInt(0).SetBytes(pubData)

txn := DeployAccountTransaction{
ClassHash: types.HexToBN("0x25ec026985a3bf9d0cc1fe17326b245dfdc3ff89b8fde106542a3ea56c5a918"),
ClassHash: types.HexToBN("0x03131fa018d520a037686ce3efddeab8f28895662f019ca3ca18a626650f7d1e"),
ContractAddressSalt: pubkeyInt,
ConstructorCallData: []*big.Int{
types.HexToBN("0x33434ad846cdd5f23eb73ff09fe6fddd568284a0fb7d1be20ee482f044dabe2"),
types.HexToBN("0x79dc0da7c54b95f10aa182ad0a46400db63156920adb65eca2654c0945a463"),
types.HexToBN("0x2"),
types.HexToBN("0x5aa23d5bb71ddaa783da7ea79d405315bafa7cf0387a74f4593578c3e9e6570"),
types.HexToBN("0x2dd76e7ad84dbed81c314ffe5e7a7cacfb8f4836f01af4e913f275f89a3de1a"),
types.HexToBN("0x1"),
pubkeyInt,
types.HexToBN("0x0"),
},
Version: big.NewInt(1),
MaxFee: big.NewInt(2e14), // 0.0002
Expand Down

0 comments on commit dfc3e9a

Please sign in to comment.