This repository has been archived by the owner on Jul 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
signer.go
67 lines (58 loc) · 1.64 KB
/
signer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package sui
import (
"crypto/ed25519"
"encoding/hex"
"fmt"
"github.com/coming-chat/go-aptos/crypto/derivation"
"github.com/cosmos/go-bip39"
xc "github.com/jumpcrypto/crosschain"
"golang.org/x/crypto/sha3"
)
// Signer for Sui
type Signer struct {
PrivateKey ed25519.PrivateKey
PublicKey ed25519.PublicKey
Address string
}
var _ xc.Signer = &Signer{}
// NewSigner creates a new Aptos Signer
func NewSigner(asset xc.ITask) (xc.Signer, error) {
return &Signer{}, nil
}
// ImportPrivateKey imports an Sui private key
// Private key may be hex (32 bytes / 64 characters) or a mnemonic.
func (signer Signer) ImportPrivateKey(privateKeyString string) (xc.PrivateKey, error) {
var seed []byte
var err error
if len(privateKeyString) == 64 {
seed, err = hex.DecodeString(privateKeyString)
if err != nil {
return []byte{}, err
}
} else {
rootSeed, err := bip39.NewSeedWithErrorChecking(privateKeyString, "")
if err != nil {
return nil, err
}
key, err := derivation.DeriveForPath("m/44'/784'/0'/0'/0'", rootSeed)
if err != nil {
return nil, err
}
seed = key.Key
}
privateKey := ed25519.NewKeyFromSeed(seed)
publicKey := privateKey.Public().(ed25519.PublicKey)
// indicate ed25519 sig scheme
tmp := []byte{0}
tmp = append(tmp, publicKey...)
addrBytes := sha3.Sum256(tmp)
// address length is 40
address := "0x" + hex.EncodeToString(addrBytes[:])[:40]
fmt.Println("Sui address = ", address)
return seed, nil
}
// Sign an Aptos tx
func (signer Signer) Sign(privateKeyBz xc.PrivateKey, data xc.TxDataToSign) (xc.TxSignature, error) {
privateKey := ed25519.NewKeyFromSeed(privateKeyBz)
return ed25519.Sign(privateKey, data), nil
}