Skip to content

Commit

Permalink
Setup go lib (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenlu authored Apr 9, 2024
1 parent 1221e2a commit d3a6a10
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 0 deletions.
67 changes: 67 additions & 0 deletions lightspark-crypto-go/internal/lightspark_crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,15 @@ func uniffiCheckChecksums() {
panic("lightspark_crypto: uniffi_lightspark_crypto_checksum_func_decrypt_ecies: UniFFI API checksum mismatch")
}
}
{
checksum := rustCall(func(uniffiStatus *C.RustCallStatus) C.uint16_t {
return C.uniffi_lightspark_crypto_checksum_func_derive_and_tweak_pubkey(uniffiStatus)
})
if checksum != 44901 {
// If this happens try cleaning and rebuilding your project
panic("lightspark_crypto: uniffi_lightspark_crypto_checksum_func_derive_and_tweak_pubkey: UniFFI API checksum mismatch")
}
}
{
checksum := rustCall(func(uniffiStatus *C.RustCallStatus) C.uint16_t {
return C.uniffi_lightspark_crypto_checksum_func_encrypt_ecies(uniffiStatus)
Expand Down Expand Up @@ -1473,6 +1482,8 @@ func (err CryptoError) Unwrap() error {
var ErrCryptoErrorSecp256k1Error = fmt.Errorf("CryptoErrorSecp256k1Error")
var ErrCryptoErrorRustSecp256k1Error = fmt.Errorf("CryptoErrorRustSecp256k1Error")
var ErrCryptoErrorInvalidPublicKeyScriptError = fmt.Errorf("CryptoErrorInvalidPublicKeyScriptError")
var ErrCryptoErrorKeyDerivationError = fmt.Errorf("CryptoErrorKeyDerivationError")
var ErrCryptoErrorKeyTweakError = fmt.Errorf("CryptoErrorKeyTweakError")

// Variant structs
type CryptoErrorSecp256k1Error struct {
Expand Down Expand Up @@ -1529,6 +1540,42 @@ func (self CryptoErrorInvalidPublicKeyScriptError) Is(target error) bool {
return target == ErrCryptoErrorInvalidPublicKeyScriptError
}

type CryptoErrorKeyDerivationError struct {
message string
}

func NewCryptoErrorKeyDerivationError() *CryptoError {
return &CryptoError{
err: &CryptoErrorKeyDerivationError{},
}
}

func (err CryptoErrorKeyDerivationError) Error() string {
return fmt.Sprintf("KeyDerivationError: %s", err.message)
}

func (self CryptoErrorKeyDerivationError) Is(target error) bool {
return target == ErrCryptoErrorKeyDerivationError
}

type CryptoErrorKeyTweakError struct {
message string
}

func NewCryptoErrorKeyTweakError() *CryptoError {
return &CryptoError{
err: &CryptoErrorKeyTweakError{},
}
}

func (err CryptoErrorKeyTweakError) Error() string {
return fmt.Sprintf("KeyTweakError: %s", err.message)
}

func (self CryptoErrorKeyTweakError) Is(target error) bool {
return target == ErrCryptoErrorKeyTweakError
}

type FfiConverterTypeCryptoError struct{}

var FfiConverterTypeCryptoErrorINSTANCE = FfiConverterTypeCryptoError{}
Expand All @@ -1552,6 +1599,10 @@ func (c FfiConverterTypeCryptoError) Read(reader io.Reader) error {
return &CryptoError{&CryptoErrorRustSecp256k1Error{message}}
case 3:
return &CryptoError{&CryptoErrorInvalidPublicKeyScriptError{message}}
case 4:
return &CryptoError{&CryptoErrorKeyDerivationError{message}}
case 5:
return &CryptoError{&CryptoErrorKeyTweakError{message}}
default:
panic(fmt.Sprintf("Unknown error code %d in FfiConverterTypeCryptoError.Read()", errorID))
}
Expand All @@ -1566,6 +1617,10 @@ func (c FfiConverterTypeCryptoError) Write(writer io.Writer, value *CryptoError)
writeInt32(writer, 2)
case *CryptoErrorInvalidPublicKeyScriptError:
writeInt32(writer, 3)
case *CryptoErrorKeyDerivationError:
writeInt32(writer, 4)
case *CryptoErrorKeyTweakError:
writeInt32(writer, 5)
default:
_ = variantValue
panic(fmt.Sprintf("invalid error value `%v` in FfiConverterTypeCryptoError.Write", value))
Expand Down Expand Up @@ -2181,6 +2236,18 @@ func DecryptEcies(cipherText []uint8, privateKeyBytes []uint8) ([]uint8, error)
}
}

func DeriveAndTweakPubkey(pubkey string, derivationPath string, addTweak *[]uint8, mulTweak *[]uint8) ([]uint8, error) {
_uniffiRV, _uniffiErr := rustCallWithError(FfiConverterTypeCryptoError{}, func(_uniffiStatus *C.RustCallStatus) RustBufferI {
return C.uniffi_lightspark_crypto_fn_func_derive_and_tweak_pubkey(FfiConverterStringINSTANCE.Lower(pubkey), FfiConverterStringINSTANCE.Lower(derivationPath), FfiConverterOptionalSequenceUint8INSTANCE.Lower(addTweak), FfiConverterOptionalSequenceUint8INSTANCE.Lower(mulTweak), _uniffiStatus)
})
if _uniffiErr != nil {
var _uniffiDefaultValue []uint8
return _uniffiDefaultValue, _uniffiErr
} else {
return FfiConverterSequenceUint8INSTANCE.Lift(_uniffiRV), _uniffiErr
}
}

func EncryptEcies(msg []uint8, publicKeyBytes []uint8) ([]uint8, error) {
_uniffiRV, _uniffiErr := rustCallWithError(FfiConverterTypeCryptoError{}, func(_uniffiStatus *C.RustCallStatus) RustBufferI {
return C.uniffi_lightspark_crypto_fn_func_encrypt_ecies(FfiConverterSequenceUint8INSTANCE.Lower(msg), FfiConverterSequenceUint8INSTANCE.Lower(publicKeyBytes), _uniffiStatus)
Expand Down
12 changes: 12 additions & 0 deletions lightspark-crypto-go/internal/lightspark_crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,14 @@ RustBuffer uniffi_lightspark_crypto_fn_func_decrypt_ecies(
RustCallStatus* out_status
);

RustBuffer uniffi_lightspark_crypto_fn_func_derive_and_tweak_pubkey(
RustBuffer pubkey,
RustBuffer derivation_path,
RustBuffer add_tweak,
RustBuffer mul_tweak,
RustCallStatus* out_status
);

RustBuffer uniffi_lightspark_crypto_fn_func_encrypt_ecies(
RustBuffer msg,
RustBuffer public_key_bytes,
Expand Down Expand Up @@ -594,6 +602,10 @@ uint16_t uniffi_lightspark_crypto_checksum_func_decrypt_ecies(
RustCallStatus* out_status
);

uint16_t uniffi_lightspark_crypto_checksum_func_derive_and_tweak_pubkey(
RustCallStatus* out_status
);

uint16_t uniffi_lightspark_crypto_checksum_func_encrypt_ecies(
RustCallStatus* out_status
);
Expand Down
Binary file modified lightspark-crypto-go/libs/darwin/amd64/liblightspark_crypto.a
Binary file not shown.
Binary file modified lightspark-crypto-go/libs/darwin/arm64/liblightspark_crypto.a
Binary file not shown.
Binary file modified lightspark-crypto-go/libs/linux/amd64/liblightspark_crypto.a
Binary file not shown.
Binary file modified lightspark-crypto-go/libs/linux/arm64/liblightspark_crypto.a
Binary file not shown.
4 changes: 4 additions & 0 deletions lightspark-crypto-go/signing.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ func GenerateMultiSigAddress(network BitcoinNetwork, publicKey1 []byte, publicKe
return internal.GenerateMultisigAddress(ffiNetwork, publicKey1, publicKey2)
}

func DeriveAndTweakPubkey(pubkey string, derivationPath string, addTweak *[]uint8, mulTweak *[]uint8) ([]uint8, error) {
return internal.DeriveAndTweakPubkey(pubkey, derivationPath, addTweak, mulTweak)
}

func getLightsparkSigner(seedBytes []byte, network BitcoinNetwork) (*internal.LightsparkSigner, error) {
seed := internal.NewSeed(seedBytes)
defer seed.Destroy()
Expand Down

0 comments on commit d3a6a10

Please sign in to comment.