Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add SetPrivateKey to go wrapper #198

Merged
merged 1 commit into from
Dec 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion bindings/go/csdk/csdk_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"encoding/hex"
"fmt"
"strings"
"sync"
"time"
"unsafe"

Expand All @@ -39,6 +40,7 @@ type CSDK struct {
groupID *C.char
keyPair unsafe.Pointer
privateKeyBytes []byte
keyPairMutex sync.Mutex
}

var contextCache = cache.New(5*time.Minute, 10*time.Minute)
Expand Down Expand Up @@ -255,6 +257,8 @@ func NewSDKByConfigFile(configFile string, groupID string, privateKey []byte) (*
}

func (csdk *CSDK) Close() {
csdk.keyPairMutex.Lock()
defer csdk.keyPairMutex.Unlock()
C.bcos_sdk_stop(csdk.sdk)
C.bcos_sdk_destroy(csdk.sdk)
C.bcos_sdk_c_free(unsafe.Pointer(csdk.groupID))
Expand All @@ -274,6 +278,24 @@ func (csdk *CSDK) PrivateKeyBytes() []byte {
return csdk.privateKeyBytes
}

// SetPrivateKey set private key
func (csdk *CSDK) SetPrivateKey(privateKeyBytes []byte) error {
cryptoType := C_SDK_ECDSA_CRYPTO
if csdk.smCrypto {
cryptoType = C_SDK_SM_CRYPTO
}
keyPair := C.bcos_sdk_create_keypair_by_private_key(cryptoType, unsafe.Pointer(&privateKeyBytes[0]), C.uint(len(privateKeyBytes)))
if keyPair == nil {
message := C.bcos_sdk_get_last_error_msg()
return fmt.Errorf("SetPrivateKey bcos_sdk_create_keypair_by_private_key failed with error: %s", C.GoString(message))
}
csdk.keyPairMutex.Lock()
defer csdk.keyPairMutex.Unlock()
C.bcos_sdk_destroy_keypair(csdk.keyPair)
csdk.keyPair = keyPair
return nil
}

func (csdk *CSDK) SMCrypto() bool {
return csdk.smCrypto
}
Expand Down Expand Up @@ -518,11 +540,13 @@ func (csdk *CSDK) CreateAndSendTransaction(chanData *CallbackChan, to string, da
if block_limit < 0 {
return nil, fmt.Errorf("group not exist, group: %s", C.GoString(csdk.groupID))
}

csdk.keyPairMutex.Lock()
C.bcos_sdk_create_signed_transaction_ver_extra_data(csdk.keyPair, csdk.groupID, csdk.chainID, cTo, cData, nil, block_limit, 0, cExtraData, &tx_hash, &signed_tx)
if C.bcos_sdk_is_last_opr_success() == 0 {
csdk.keyPairMutex.Unlock()
return nil, fmt.Errorf("bcos_sdk_create_signed_transaction, error: %s", C.GoString(C.bcos_sdk_get_last_error_msg()))
}
csdk.keyPairMutex.Unlock()
txHash, err := hex.DecodeString(strings.TrimPrefix(C.GoString(tx_hash), "0x"))
if err != nil {
return nil, err
Expand Down Expand Up @@ -578,6 +602,8 @@ func (csdk *CSDK) CreateEncodedTransactionDataV1(blockLimit int64, to string, in
func (csdk *CSDK) CreateEncodedSignature(hash []byte) ([]byte, error) {
hexHash := hex.EncodeToString(hash)
cHexHash := C.CString(hexHash)
csdk.keyPairMutex.Lock()
defer csdk.keyPairMutex.Unlock()
signatureHex := C.bcos_sdk_sign_transaction_data_hash(csdk.keyPair, cHexHash)
defer C.bcos_sdk_c_free(unsafe.Pointer(signatureHex))
if C.bcos_sdk_is_last_opr_success() == 0 {
Expand Down
Loading