diff --git a/bindings/go/examples/generate_ed25519_address/main.go b/bindings/go/examples/generate_ed25519_address/main.go index acae6236a..9c25a14c0 100644 --- a/bindings/go/examples/generate_ed25519_address/main.go +++ b/bindings/go/examples/generate_ed25519_address/main.go @@ -16,13 +16,11 @@ func main() { panic(err) } publicKey := privateKey.PublicKey() + flaggedPublicKey := publicKey.ToFlaggedBytes() address := publicKey.DeriveAddress() - publicKeyBytes := publicKey.ToBytes() - - flaggedPublicKey := append([]byte{byte(sdk.SignatureSchemeEd25519)}, publicKeyBytes...) fmt.Println("Private Key:", privateKeyBech32) - fmt.Println("Public Key:", sdk.Base64Encode(publicKeyBytes)) + fmt.Println("Public Key:", sdk.Base64Encode(publicKey.ToBytes())) fmt.Println("Public Key With Flag:", sdk.Base64Encode(flaggedPublicKey)) fmt.Println("Address:", address.ToHex()) } diff --git a/bindings/go/iota_sdk_ffi/iota_sdk_ffi.go b/bindings/go/iota_sdk_ffi/iota_sdk_ffi.go index f4a17f430..578bcc69c 100644 --- a/bindings/go/iota_sdk_ffi/iota_sdk_ffi.go +++ b/bindings/go/iota_sdk_ffi/iota_sdk_ffi.go @@ -1091,7 +1091,7 @@ func uniffiCheckChecksums() { checksum := rustCall(func(_uniffiStatus *C.RustCallStatus) C.uint16_t { return C.uniffi_iota_sdk_ffi_checksum_method_ed25519publickey_scheme() }) - if checksum != 141 { + if checksum != 3041 { // If this happens try cleaning and rebuilding your project panic("iota_sdk_ffi: uniffi_iota_sdk_ffi_checksum_method_ed25519publickey_scheme: UniFFI API checksum mismatch") } @@ -1106,6 +1106,15 @@ func uniffiCheckChecksums() { } } { + checksum := rustCall(func(_uniffiStatus *C.RustCallStatus) C.uint16_t { + return C.uniffi_iota_sdk_ffi_checksum_method_ed25519publickey_to_flagged_bytes() + }) + if checksum != 62359 { + // If this happens try cleaning and rebuilding your project + panic("iota_sdk_ffi: uniffi_iota_sdk_ffi_checksum_method_ed25519publickey_to_flagged_bytes: UniFFI API checksum mismatch") + } + } + { checksum := rustCall(func(_uniffiStatus *C.RustCallStatus) C.uint16_t { return C.uniffi_iota_sdk_ffi_checksum_method_ed25519signature_to_bytes() }) @@ -2936,7 +2945,7 @@ func uniffiCheckChecksums() { checksum := rustCall(func(_uniffiStatus *C.RustCallStatus) C.uint16_t { return C.uniffi_iota_sdk_ffi_checksum_method_secp256k1publickey_scheme() }) - if checksum != 798 { + if checksum != 60874 { // If this happens try cleaning and rebuilding your project panic("iota_sdk_ffi: uniffi_iota_sdk_ffi_checksum_method_secp256k1publickey_scheme: UniFFI API checksum mismatch") } @@ -2951,6 +2960,15 @@ func uniffiCheckChecksums() { } } { + checksum := rustCall(func(_uniffiStatus *C.RustCallStatus) C.uint16_t { + return C.uniffi_iota_sdk_ffi_checksum_method_secp256k1publickey_to_flagged_bytes() + }) + if checksum != 46673 { + // If this happens try cleaning and rebuilding your project + panic("iota_sdk_ffi: uniffi_iota_sdk_ffi_checksum_method_secp256k1publickey_to_flagged_bytes: UniFFI API checksum mismatch") + } + } + { checksum := rustCall(func(_uniffiStatus *C.RustCallStatus) C.uint16_t { return C.uniffi_iota_sdk_ffi_checksum_method_secp256k1signature_to_bytes() }) @@ -3134,7 +3152,7 @@ func uniffiCheckChecksums() { checksum := rustCall(func(_uniffiStatus *C.RustCallStatus) C.uint16_t { return C.uniffi_iota_sdk_ffi_checksum_method_secp256r1publickey_scheme() }) - if checksum != 12227 { + if checksum != 48083 { // If this happens try cleaning and rebuilding your project panic("iota_sdk_ffi: uniffi_iota_sdk_ffi_checksum_method_secp256r1publickey_scheme: UniFFI API checksum mismatch") } @@ -3149,6 +3167,15 @@ func uniffiCheckChecksums() { } } { + checksum := rustCall(func(_uniffiStatus *C.RustCallStatus) C.uint16_t { + return C.uniffi_iota_sdk_ffi_checksum_method_secp256r1publickey_to_flagged_bytes() + }) + if checksum != 14205 { + // If this happens try cleaning and rebuilding your project + panic("iota_sdk_ffi: uniffi_iota_sdk_ffi_checksum_method_secp256r1publickey_to_flagged_bytes: UniFFI API checksum mismatch") + } + } + { checksum := rustCall(func(_uniffiStatus *C.RustCallStatus) C.uint16_t { return C.uniffi_iota_sdk_ffi_checksum_method_secp256r1signature_to_bytes() }) @@ -10869,9 +10896,11 @@ type Ed25519PublicKeyInterface interface { // // `hash(32-byte ed25519 public key)` DeriveAddress() *Address - // Return the flag for this signature scheme + // Returns the signature scheme for this public key. Scheme() SignatureScheme ToBytes() []byte + // Returns the bytes with signature scheme flag prepended. + ToFlaggedBytes() []byte } // An ed25519 public key. // @@ -10934,7 +10963,7 @@ func (_self *Ed25519PublicKey) DeriveAddress() *Address { })) } -// Return the flag for this signature scheme +// Returns the signature scheme for this public key. func (_self *Ed25519PublicKey) Scheme() SignatureScheme { _pointer := _self.ffiObject.incrementPointer("*Ed25519PublicKey") defer _self.ffiObject.decrementPointer() @@ -10956,6 +10985,18 @@ func (_self *Ed25519PublicKey) ToBytes() []byte { } })) } + +// Returns the bytes with signature scheme flag prepended. +func (_self *Ed25519PublicKey) ToFlaggedBytes() []byte { + _pointer := _self.ffiObject.incrementPointer("*Ed25519PublicKey") + defer _self.ffiObject.decrementPointer() + return FfiConverterBytesINSTANCE.Lift(rustCall(func(_uniffiStatus *C.RustCallStatus) RustBufferI { + return GoRustBuffer { + inner: C.uniffi_iota_sdk_ffi_fn_method_ed25519publickey_to_flagged_bytes( + _pointer,_uniffiStatus), + } + })) +} func (object *Ed25519PublicKey) Destroy() { runtime.SetFinalizer(object, nil) object.ffiObject.destroy() @@ -19382,9 +19423,11 @@ type Secp256k1PublicKeyInterface interface { // // `hash( 0x01 || 33-byte secp256k1 public key)` DeriveAddress() *Address - // Return the flag for this signature scheme + // Returns the signature scheme for this public key. Scheme() SignatureScheme ToBytes() []byte + // Returns the bytes with signature scheme flag prepended. + ToFlaggedBytes() []byte } // A secp256k1 signature. // @@ -19448,7 +19491,7 @@ func (_self *Secp256k1PublicKey) DeriveAddress() *Address { })) } -// Return the flag for this signature scheme +// Returns the signature scheme for this public key. func (_self *Secp256k1PublicKey) Scheme() SignatureScheme { _pointer := _self.ffiObject.incrementPointer("*Secp256k1PublicKey") defer _self.ffiObject.decrementPointer() @@ -19470,6 +19513,18 @@ func (_self *Secp256k1PublicKey) ToBytes() []byte { } })) } + +// Returns the bytes with signature scheme flag prepended. +func (_self *Secp256k1PublicKey) ToFlaggedBytes() []byte { + _pointer := _self.ffiObject.incrementPointer("*Secp256k1PublicKey") + defer _self.ffiObject.decrementPointer() + return FfiConverterBytesINSTANCE.Lift(rustCall(func(_uniffiStatus *C.RustCallStatus) RustBufferI { + return GoRustBuffer { + inner: C.uniffi_iota_sdk_ffi_fn_method_secp256k1publickey_to_flagged_bytes( + _pointer,_uniffiStatus), + } + })) +} func (object *Secp256k1PublicKey) Destroy() { runtime.SetFinalizer(object, nil) object.ffiObject.destroy() @@ -20215,9 +20270,11 @@ type Secp256r1PublicKeyInterface interface { // // `hash( 0x02 || 33-byte secp256r1 public key)` DeriveAddress() *Address - // Return the flag for this signature scheme + // Returns the signature scheme for this public key. Scheme() SignatureScheme ToBytes() []byte + // Returns the bytes with signature scheme flag prepended + ToFlaggedBytes() []byte } // A secp256r1 signature. // @@ -20281,7 +20338,7 @@ func (_self *Secp256r1PublicKey) DeriveAddress() *Address { })) } -// Return the flag for this signature scheme +// Returns the signature scheme for this public key. func (_self *Secp256r1PublicKey) Scheme() SignatureScheme { _pointer := _self.ffiObject.incrementPointer("*Secp256r1PublicKey") defer _self.ffiObject.decrementPointer() @@ -20303,6 +20360,18 @@ func (_self *Secp256r1PublicKey) ToBytes() []byte { } })) } + +// Returns the bytes with signature scheme flag prepended +func (_self *Secp256r1PublicKey) ToFlaggedBytes() []byte { + _pointer := _self.ffiObject.incrementPointer("*Secp256r1PublicKey") + defer _self.ffiObject.decrementPointer() + return FfiConverterBytesINSTANCE.Lift(rustCall(func(_uniffiStatus *C.RustCallStatus) RustBufferI { + return GoRustBuffer { + inner: C.uniffi_iota_sdk_ffi_fn_method_secp256r1publickey_to_flagged_bytes( + _pointer,_uniffiStatus), + } + })) +} func (object *Secp256r1PublicKey) Destroy() { runtime.SetFinalizer(object, nil) object.ffiObject.destroy() diff --git a/bindings/go/iota_sdk_ffi/iota_sdk_ffi.h b/bindings/go/iota_sdk_ffi/iota_sdk_ffi.h index 48e72114e..d5b86f186 100644 --- a/bindings/go/iota_sdk_ffi/iota_sdk_ffi.h +++ b/bindings/go/iota_sdk_ffi/iota_sdk_ffi.h @@ -1275,6 +1275,11 @@ RustBuffer uniffi_iota_sdk_ffi_fn_method_ed25519publickey_scheme(void* ptr, Rust RustBuffer uniffi_iota_sdk_ffi_fn_method_ed25519publickey_to_bytes(void* ptr, RustCallStatus *out_status ); #endif +#ifndef UNIFFI_FFIDEF_UNIFFI_IOTA_SDK_FFI_FN_METHOD_ED25519PUBLICKEY_TO_FLAGGED_BYTES +#define UNIFFI_FFIDEF_UNIFFI_IOTA_SDK_FFI_FN_METHOD_ED25519PUBLICKEY_TO_FLAGGED_BYTES +RustBuffer uniffi_iota_sdk_ffi_fn_method_ed25519publickey_to_flagged_bytes(void* ptr, RustCallStatus *out_status +); +#endif #ifndef UNIFFI_FFIDEF_UNIFFI_IOTA_SDK_FFI_FN_CLONE_ED25519SIGNATURE #define UNIFFI_FFIDEF_UNIFFI_IOTA_SDK_FFI_FN_CLONE_ED25519SIGNATURE void* uniffi_iota_sdk_ffi_fn_clone_ed25519signature(void* ptr, RustCallStatus *out_status @@ -3470,6 +3475,11 @@ RustBuffer uniffi_iota_sdk_ffi_fn_method_secp256k1publickey_scheme(void* ptr, Ru RustBuffer uniffi_iota_sdk_ffi_fn_method_secp256k1publickey_to_bytes(void* ptr, RustCallStatus *out_status ); #endif +#ifndef UNIFFI_FFIDEF_UNIFFI_IOTA_SDK_FFI_FN_METHOD_SECP256K1PUBLICKEY_TO_FLAGGED_BYTES +#define UNIFFI_FFIDEF_UNIFFI_IOTA_SDK_FFI_FN_METHOD_SECP256K1PUBLICKEY_TO_FLAGGED_BYTES +RustBuffer uniffi_iota_sdk_ffi_fn_method_secp256k1publickey_to_flagged_bytes(void* ptr, RustCallStatus *out_status +); +#endif #ifndef UNIFFI_FFIDEF_UNIFFI_IOTA_SDK_FFI_FN_CLONE_SECP256K1SIGNATURE #define UNIFFI_FFIDEF_UNIFFI_IOTA_SDK_FFI_FN_CLONE_SECP256K1SIGNATURE void* uniffi_iota_sdk_ffi_fn_clone_secp256k1signature(void* ptr, RustCallStatus *out_status @@ -3709,6 +3719,11 @@ RustBuffer uniffi_iota_sdk_ffi_fn_method_secp256r1publickey_scheme(void* ptr, Ru RustBuffer uniffi_iota_sdk_ffi_fn_method_secp256r1publickey_to_bytes(void* ptr, RustCallStatus *out_status ); #endif +#ifndef UNIFFI_FFIDEF_UNIFFI_IOTA_SDK_FFI_FN_METHOD_SECP256R1PUBLICKEY_TO_FLAGGED_BYTES +#define UNIFFI_FFIDEF_UNIFFI_IOTA_SDK_FFI_FN_METHOD_SECP256R1PUBLICKEY_TO_FLAGGED_BYTES +RustBuffer uniffi_iota_sdk_ffi_fn_method_secp256r1publickey_to_flagged_bytes(void* ptr, RustCallStatus *out_status +); +#endif #ifndef UNIFFI_FFIDEF_UNIFFI_IOTA_SDK_FFI_FN_CLONE_SECP256R1SIGNATURE #define UNIFFI_FFIDEF_UNIFFI_IOTA_SDK_FFI_FN_CLONE_SECP256R1SIGNATURE void* uniffi_iota_sdk_ffi_fn_clone_secp256r1signature(void* ptr, RustCallStatus *out_status @@ -6095,6 +6110,12 @@ uint16_t uniffi_iota_sdk_ffi_checksum_method_ed25519publickey_scheme(void #define UNIFFI_FFIDEF_UNIFFI_IOTA_SDK_FFI_CHECKSUM_METHOD_ED25519PUBLICKEY_TO_BYTES uint16_t uniffi_iota_sdk_ffi_checksum_method_ed25519publickey_to_bytes(void +); +#endif +#ifndef UNIFFI_FFIDEF_UNIFFI_IOTA_SDK_FFI_CHECKSUM_METHOD_ED25519PUBLICKEY_TO_FLAGGED_BYTES +#define UNIFFI_FFIDEF_UNIFFI_IOTA_SDK_FFI_CHECKSUM_METHOD_ED25519PUBLICKEY_TO_FLAGGED_BYTES +uint16_t uniffi_iota_sdk_ffi_checksum_method_ed25519publickey_to_flagged_bytes(void + ); #endif #ifndef UNIFFI_FFIDEF_UNIFFI_IOTA_SDK_FFI_CHECKSUM_METHOD_ED25519SIGNATURE_TO_BYTES @@ -7325,6 +7346,12 @@ uint16_t uniffi_iota_sdk_ffi_checksum_method_secp256k1publickey_scheme(void #define UNIFFI_FFIDEF_UNIFFI_IOTA_SDK_FFI_CHECKSUM_METHOD_SECP256K1PUBLICKEY_TO_BYTES uint16_t uniffi_iota_sdk_ffi_checksum_method_secp256k1publickey_to_bytes(void +); +#endif +#ifndef UNIFFI_FFIDEF_UNIFFI_IOTA_SDK_FFI_CHECKSUM_METHOD_SECP256K1PUBLICKEY_TO_FLAGGED_BYTES +#define UNIFFI_FFIDEF_UNIFFI_IOTA_SDK_FFI_CHECKSUM_METHOD_SECP256K1PUBLICKEY_TO_FLAGGED_BYTES +uint16_t uniffi_iota_sdk_ffi_checksum_method_secp256k1publickey_to_flagged_bytes(void + ); #endif #ifndef UNIFFI_FFIDEF_UNIFFI_IOTA_SDK_FFI_CHECKSUM_METHOD_SECP256K1SIGNATURE_TO_BYTES @@ -7457,6 +7484,12 @@ uint16_t uniffi_iota_sdk_ffi_checksum_method_secp256r1publickey_scheme(void #define UNIFFI_FFIDEF_UNIFFI_IOTA_SDK_FFI_CHECKSUM_METHOD_SECP256R1PUBLICKEY_TO_BYTES uint16_t uniffi_iota_sdk_ffi_checksum_method_secp256r1publickey_to_bytes(void +); +#endif +#ifndef UNIFFI_FFIDEF_UNIFFI_IOTA_SDK_FFI_CHECKSUM_METHOD_SECP256R1PUBLICKEY_TO_FLAGGED_BYTES +#define UNIFFI_FFIDEF_UNIFFI_IOTA_SDK_FFI_CHECKSUM_METHOD_SECP256R1PUBLICKEY_TO_FLAGGED_BYTES +uint16_t uniffi_iota_sdk_ffi_checksum_method_secp256r1publickey_to_flagged_bytes(void + ); #endif #ifndef UNIFFI_FFIDEF_UNIFFI_IOTA_SDK_FFI_CHECKSUM_METHOD_SECP256R1SIGNATURE_TO_BYTES diff --git a/bindings/kotlin/examples/GenerateEd25519Address.kt b/bindings/kotlin/examples/GenerateEd25519Address.kt index b0dc07cd8..5d102617c 100644 --- a/bindings/kotlin/examples/GenerateEd25519Address.kt +++ b/bindings/kotlin/examples/GenerateEd25519Address.kt @@ -9,12 +9,11 @@ fun main() { val privateKey = Ed25519PrivateKey.generate() val privateKeyBech32 = privateKey.toBech32() val publicKey = privateKey.publicKey() - val publicKeyBytes = publicKey.toBytes() - val flaggedPublicKey = byteArrayOf((publicKey.scheme().ordinal + 1).toByte()) + publicKeyBytes + val flaggedPublicKey = publicKey.toFlaggedBytes() val address = publicKey.deriveAddress() println("Private Key: ${privateKeyBech32}") - println("Public Key: ${base64Encode(publicKeyBytes)}") + println("Public Key: ${base64Encode(publicKey.toBytes())}") println("Public Key With Flag: ${base64Encode(flaggedPublicKey)}") println("Address: ${address.toHex()}") } diff --git a/bindings/kotlin/lib/iota_sdk/iota_sdk_ffi.kt b/bindings/kotlin/lib/iota_sdk/iota_sdk_ffi.kt index 3008c85b4..0674d7575 100644 --- a/bindings/kotlin/lib/iota_sdk/iota_sdk_ffi.kt +++ b/bindings/kotlin/lib/iota_sdk/iota_sdk_ffi.kt @@ -2449,6 +2449,12 @@ internal interface UniffiForeignFutureCompleteVoid : com.sun.jna.Callback { + + + + + + @@ -2637,6 +2643,8 @@ fun uniffi_iota_sdk_ffi_checksum_method_ed25519publickey_scheme( ): Short fun uniffi_iota_sdk_ffi_checksum_method_ed25519publickey_to_bytes( ): Short +fun uniffi_iota_sdk_ffi_checksum_method_ed25519publickey_to_flagged_bytes( +): Short fun uniffi_iota_sdk_ffi_checksum_method_ed25519signature_to_bytes( ): Short fun uniffi_iota_sdk_ffi_checksum_method_ed25519verifyingkey_public_key( @@ -3047,6 +3055,8 @@ fun uniffi_iota_sdk_ffi_checksum_method_secp256k1publickey_scheme( ): Short fun uniffi_iota_sdk_ffi_checksum_method_secp256k1publickey_to_bytes( ): Short +fun uniffi_iota_sdk_ffi_checksum_method_secp256k1publickey_to_flagged_bytes( +): Short fun uniffi_iota_sdk_ffi_checksum_method_secp256k1signature_to_bytes( ): Short fun uniffi_iota_sdk_ffi_checksum_method_secp256k1verifier_verify_simple( @@ -3091,6 +3101,8 @@ fun uniffi_iota_sdk_ffi_checksum_method_secp256r1publickey_scheme( ): Short fun uniffi_iota_sdk_ffi_checksum_method_secp256r1publickey_to_bytes( ): Short +fun uniffi_iota_sdk_ffi_checksum_method_secp256r1publickey_to_flagged_bytes( +): Short fun uniffi_iota_sdk_ffi_checksum_method_secp256r1signature_to_bytes( ): Short fun uniffi_iota_sdk_ffi_checksum_method_secp256r1verifier_verify_simple( @@ -4396,6 +4408,8 @@ fun uniffi_iota_sdk_ffi_fn_method_ed25519publickey_scheme(`ptr`: Pointer,uniffi_ ): RustBuffer.ByValue fun uniffi_iota_sdk_ffi_fn_method_ed25519publickey_to_bytes(`ptr`: Pointer,uniffi_out_err: UniffiRustCallStatus, ): RustBuffer.ByValue +fun uniffi_iota_sdk_ffi_fn_method_ed25519publickey_to_flagged_bytes(`ptr`: Pointer,uniffi_out_err: UniffiRustCallStatus, +): RustBuffer.ByValue fun uniffi_iota_sdk_ffi_fn_clone_ed25519signature(`ptr`: Pointer,uniffi_out_err: UniffiRustCallStatus, ): Pointer fun uniffi_iota_sdk_ffi_fn_free_ed25519signature(`ptr`: Pointer,uniffi_out_err: UniffiRustCallStatus, @@ -5264,6 +5278,8 @@ fun uniffi_iota_sdk_ffi_fn_method_secp256k1publickey_scheme(`ptr`: Pointer,uniff ): RustBuffer.ByValue fun uniffi_iota_sdk_ffi_fn_method_secp256k1publickey_to_bytes(`ptr`: Pointer,uniffi_out_err: UniffiRustCallStatus, ): RustBuffer.ByValue +fun uniffi_iota_sdk_ffi_fn_method_secp256k1publickey_to_flagged_bytes(`ptr`: Pointer,uniffi_out_err: UniffiRustCallStatus, +): RustBuffer.ByValue fun uniffi_iota_sdk_ffi_fn_clone_secp256k1signature(`ptr`: Pointer,uniffi_out_err: UniffiRustCallStatus, ): Pointer fun uniffi_iota_sdk_ffi_fn_free_secp256k1signature(`ptr`: Pointer,uniffi_out_err: UniffiRustCallStatus, @@ -5358,6 +5374,8 @@ fun uniffi_iota_sdk_ffi_fn_method_secp256r1publickey_scheme(`ptr`: Pointer,uniff ): RustBuffer.ByValue fun uniffi_iota_sdk_ffi_fn_method_secp256r1publickey_to_bytes(`ptr`: Pointer,uniffi_out_err: UniffiRustCallStatus, ): RustBuffer.ByValue +fun uniffi_iota_sdk_ffi_fn_method_secp256r1publickey_to_flagged_bytes(`ptr`: Pointer,uniffi_out_err: UniffiRustCallStatus, +): RustBuffer.ByValue fun uniffi_iota_sdk_ffi_fn_clone_secp256r1signature(`ptr`: Pointer,uniffi_out_err: UniffiRustCallStatus, ): Pointer fun uniffi_iota_sdk_ffi_fn_free_secp256r1signature(`ptr`: Pointer,uniffi_out_err: UniffiRustCallStatus, @@ -6363,12 +6381,15 @@ private fun uniffiCheckApiChecksums(lib: IntegrityCheckingUniffiLib) { if (lib.uniffi_iota_sdk_ffi_checksum_method_ed25519publickey_derive_address() != 37757.toShort()) { throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project") } - if (lib.uniffi_iota_sdk_ffi_checksum_method_ed25519publickey_scheme() != 141.toShort()) { + if (lib.uniffi_iota_sdk_ffi_checksum_method_ed25519publickey_scheme() != 3041.toShort()) { throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project") } if (lib.uniffi_iota_sdk_ffi_checksum_method_ed25519publickey_to_bytes() != 16656.toShort()) { throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project") } + if (lib.uniffi_iota_sdk_ffi_checksum_method_ed25519publickey_to_flagged_bytes() != 62359.toShort()) { + throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project") + } if (lib.uniffi_iota_sdk_ffi_checksum_method_ed25519signature_to_bytes() != 31911.toShort()) { throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project") } @@ -6978,12 +6999,15 @@ private fun uniffiCheckApiChecksums(lib: IntegrityCheckingUniffiLib) { if (lib.uniffi_iota_sdk_ffi_checksum_method_secp256k1publickey_derive_address() != 48490.toShort()) { throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project") } - if (lib.uniffi_iota_sdk_ffi_checksum_method_secp256k1publickey_scheme() != 798.toShort()) { + if (lib.uniffi_iota_sdk_ffi_checksum_method_secp256k1publickey_scheme() != 60874.toShort()) { throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project") } if (lib.uniffi_iota_sdk_ffi_checksum_method_secp256k1publickey_to_bytes() != 49170.toShort()) { throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project") } + if (lib.uniffi_iota_sdk_ffi_checksum_method_secp256k1publickey_to_flagged_bytes() != 46673.toShort()) { + throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project") + } if (lib.uniffi_iota_sdk_ffi_checksum_method_secp256k1signature_to_bytes() != 49705.toShort()) { throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project") } @@ -7044,12 +7068,15 @@ private fun uniffiCheckApiChecksums(lib: IntegrityCheckingUniffiLib) { if (lib.uniffi_iota_sdk_ffi_checksum_method_secp256r1publickey_derive_address() != 27344.toShort()) { throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project") } - if (lib.uniffi_iota_sdk_ffi_checksum_method_secp256r1publickey_scheme() != 12227.toShort()) { + if (lib.uniffi_iota_sdk_ffi_checksum_method_secp256r1publickey_scheme() != 48083.toShort()) { throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project") } if (lib.uniffi_iota_sdk_ffi_checksum_method_secp256r1publickey_to_bytes() != 21066.toShort()) { throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project") } + if (lib.uniffi_iota_sdk_ffi_checksum_method_secp256r1publickey_to_flagged_bytes() != 14205.toShort()) { + throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project") + } if (lib.uniffi_iota_sdk_ffi_checksum_method_secp256r1signature_to_bytes() != 64948.toShort()) { throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project") } @@ -16369,12 +16396,17 @@ public interface Ed25519PublicKeyInterface { fun `deriveAddress`(): Address /** - * Return the flag for this signature scheme + * Returns the signature scheme for this public key. */ fun `scheme`(): SignatureScheme fun `toBytes`(): kotlin.ByteArray + /** + * Returns the bytes with signature scheme flag prepended. + */ + fun `toFlaggedBytes`(): kotlin.ByteArray + companion object } @@ -16493,7 +16525,7 @@ open class Ed25519PublicKey: Disposable, AutoCloseable, Ed25519PublicKeyInterfac /** - * Return the flag for this signature scheme + * Returns the signature scheme for this public key. */override fun `scheme`(): SignatureScheme { return FfiConverterTypeSignatureScheme.lift( callWithPointer { @@ -16519,6 +16551,21 @@ open class Ed25519PublicKey: Disposable, AutoCloseable, Ed25519PublicKeyInterfac + /** + * Returns the bytes with signature scheme flag prepended. + */override fun `toFlaggedBytes`(): kotlin.ByteArray { + return FfiConverterByteArray.lift( + callWithPointer { + uniffiRustCall() { _status -> + UniffiLib.INSTANCE.uniffi_iota_sdk_ffi_fn_method_ed25519publickey_to_flagged_bytes( + it, _status) +} + } + ) + } + + + companion object { @@ -32734,12 +32781,17 @@ public interface Secp256k1PublicKeyInterface { fun `deriveAddress`(): Address /** - * Return the flag for this signature scheme + * Returns the signature scheme for this public key. */ fun `scheme`(): SignatureScheme fun `toBytes`(): kotlin.ByteArray + /** + * Returns the bytes with signature scheme flag prepended. + */ + fun `toFlaggedBytes`(): kotlin.ByteArray + companion object } @@ -32859,7 +32911,7 @@ open class Secp256k1PublicKey: Disposable, AutoCloseable, Secp256k1PublicKeyInte /** - * Return the flag for this signature scheme + * Returns the signature scheme for this public key. */override fun `scheme`(): SignatureScheme { return FfiConverterTypeSignatureScheme.lift( callWithPointer { @@ -32885,6 +32937,21 @@ open class Secp256k1PublicKey: Disposable, AutoCloseable, Secp256k1PublicKeyInte + /** + * Returns the bytes with signature scheme flag prepended. + */override fun `toFlaggedBytes`(): kotlin.ByteArray { + return FfiConverterByteArray.lift( + callWithPointer { + uniffiRustCall() { _status -> + UniffiLib.INSTANCE.uniffi_iota_sdk_ffi_fn_method_secp256k1publickey_to_flagged_bytes( + it, _status) +} + } + ) + } + + + companion object { @@ -34464,12 +34531,17 @@ public interface Secp256r1PublicKeyInterface { fun `deriveAddress`(): Address /** - * Return the flag for this signature scheme + * Returns the signature scheme for this public key. */ fun `scheme`(): SignatureScheme fun `toBytes`(): kotlin.ByteArray + /** + * Returns the bytes with signature scheme flag prepended + */ + fun `toFlaggedBytes`(): kotlin.ByteArray + companion object } @@ -34589,7 +34661,7 @@ open class Secp256r1PublicKey: Disposable, AutoCloseable, Secp256r1PublicKeyInte /** - * Return the flag for this signature scheme + * Returns the signature scheme for this public key. */override fun `scheme`(): SignatureScheme { return FfiConverterTypeSignatureScheme.lift( callWithPointer { @@ -34615,6 +34687,21 @@ open class Secp256r1PublicKey: Disposable, AutoCloseable, Secp256r1PublicKeyInte + /** + * Returns the bytes with signature scheme flag prepended + */override fun `toFlaggedBytes`(): kotlin.ByteArray { + return FfiConverterByteArray.lift( + callWithPointer { + uniffiRustCall() { _status -> + UniffiLib.INSTANCE.uniffi_iota_sdk_ffi_fn_method_secp256r1publickey_to_flagged_bytes( + it, _status) +} + } + ) + } + + + companion object { diff --git a/bindings/python/examples/generate_ed25519_address.py b/bindings/python/examples/generate_ed25519_address.py index f09d681be..fd502c666 100644 --- a/bindings/python/examples/generate_ed25519_address.py +++ b/bindings/python/examples/generate_ed25519_address.py @@ -8,12 +8,11 @@ def main(): private_key = Ed25519PrivateKey.generate() private_key_bech32 = private_key.to_bech32() public_key = private_key.public_key() - public_key_bytes = public_key.to_bytes() - flagged_public_key = bytes([public_key.scheme().value]) + public_key_bytes + flagged_public_key = public_key.to_flagged_bytes() address = public_key.derive_address() print(f"Private Key: {private_key_bech32}") - print(f"Public Key: {base64_encode(public_key_bytes)}") + print(f"Public Key: {base64_encode(public_key.to_bytes())}") print(f"Public Key With Flag: {base64_encode(flagged_public_key)}") print(f"Address: {address.to_hex()}") diff --git a/bindings/python/lib/iota_sdk_ffi.py b/bindings/python/lib/iota_sdk_ffi.py index 4e4dec822..be31d4003 100644 --- a/bindings/python/lib/iota_sdk_ffi.py +++ b/bindings/python/lib/iota_sdk_ffi.py @@ -623,10 +623,12 @@ def _uniffi_check_api_checksums(lib): raise InternalError("UniFFI API checksum mismatch: try cleaning and rebuilding your project") if lib.uniffi_iota_sdk_ffi_checksum_method_ed25519publickey_derive_address() != 37757: raise InternalError("UniFFI API checksum mismatch: try cleaning and rebuilding your project") - if lib.uniffi_iota_sdk_ffi_checksum_method_ed25519publickey_scheme() != 141: + if lib.uniffi_iota_sdk_ffi_checksum_method_ed25519publickey_scheme() != 3041: raise InternalError("UniFFI API checksum mismatch: try cleaning and rebuilding your project") if lib.uniffi_iota_sdk_ffi_checksum_method_ed25519publickey_to_bytes() != 16656: raise InternalError("UniFFI API checksum mismatch: try cleaning and rebuilding your project") + if lib.uniffi_iota_sdk_ffi_checksum_method_ed25519publickey_to_flagged_bytes() != 62359: + raise InternalError("UniFFI API checksum mismatch: try cleaning and rebuilding your project") if lib.uniffi_iota_sdk_ffi_checksum_method_ed25519signature_to_bytes() != 31911: raise InternalError("UniFFI API checksum mismatch: try cleaning and rebuilding your project") if lib.uniffi_iota_sdk_ffi_checksum_method_ed25519verifyingkey_public_key() != 55026: @@ -1033,10 +1035,12 @@ def _uniffi_check_api_checksums(lib): raise InternalError("UniFFI API checksum mismatch: try cleaning and rebuilding your project") if lib.uniffi_iota_sdk_ffi_checksum_method_secp256k1publickey_derive_address() != 48490: raise InternalError("UniFFI API checksum mismatch: try cleaning and rebuilding your project") - if lib.uniffi_iota_sdk_ffi_checksum_method_secp256k1publickey_scheme() != 798: + if lib.uniffi_iota_sdk_ffi_checksum_method_secp256k1publickey_scheme() != 60874: raise InternalError("UniFFI API checksum mismatch: try cleaning and rebuilding your project") if lib.uniffi_iota_sdk_ffi_checksum_method_secp256k1publickey_to_bytes() != 49170: raise InternalError("UniFFI API checksum mismatch: try cleaning and rebuilding your project") + if lib.uniffi_iota_sdk_ffi_checksum_method_secp256k1publickey_to_flagged_bytes() != 46673: + raise InternalError("UniFFI API checksum mismatch: try cleaning and rebuilding your project") if lib.uniffi_iota_sdk_ffi_checksum_method_secp256k1signature_to_bytes() != 49705: raise InternalError("UniFFI API checksum mismatch: try cleaning and rebuilding your project") if lib.uniffi_iota_sdk_ffi_checksum_method_secp256k1verifier_verify_simple() != 36777: @@ -1077,10 +1081,12 @@ def _uniffi_check_api_checksums(lib): raise InternalError("UniFFI API checksum mismatch: try cleaning and rebuilding your project") if lib.uniffi_iota_sdk_ffi_checksum_method_secp256r1publickey_derive_address() != 27344: raise InternalError("UniFFI API checksum mismatch: try cleaning and rebuilding your project") - if lib.uniffi_iota_sdk_ffi_checksum_method_secp256r1publickey_scheme() != 12227: + if lib.uniffi_iota_sdk_ffi_checksum_method_secp256r1publickey_scheme() != 48083: raise InternalError("UniFFI API checksum mismatch: try cleaning and rebuilding your project") if lib.uniffi_iota_sdk_ffi_checksum_method_secp256r1publickey_to_bytes() != 21066: raise InternalError("UniFFI API checksum mismatch: try cleaning and rebuilding your project") + if lib.uniffi_iota_sdk_ffi_checksum_method_secp256r1publickey_to_flagged_bytes() != 14205: + raise InternalError("UniFFI API checksum mismatch: try cleaning and rebuilding your project") if lib.uniffi_iota_sdk_ffi_checksum_method_secp256r1signature_to_bytes() != 64948: raise InternalError("UniFFI API checksum mismatch: try cleaning and rebuilding your project") if lib.uniffi_iota_sdk_ffi_checksum_method_secp256r1verifier_verify_simple() != 18491: @@ -3010,6 +3016,11 @@ class _UniffiForeignFutureStructVoid(ctypes.Structure): ctypes.POINTER(_UniffiRustCallStatus), ) _UniffiLib.uniffi_iota_sdk_ffi_fn_method_ed25519publickey_to_bytes.restype = _UniffiRustBuffer +_UniffiLib.uniffi_iota_sdk_ffi_fn_method_ed25519publickey_to_flagged_bytes.argtypes = ( + ctypes.c_void_p, + ctypes.POINTER(_UniffiRustCallStatus), +) +_UniffiLib.uniffi_iota_sdk_ffi_fn_method_ed25519publickey_to_flagged_bytes.restype = _UniffiRustBuffer _UniffiLib.uniffi_iota_sdk_ffi_fn_clone_ed25519signature.argtypes = ( ctypes.c_void_p, ctypes.POINTER(_UniffiRustCallStatus), @@ -5244,6 +5255,11 @@ class _UniffiForeignFutureStructVoid(ctypes.Structure): ctypes.POINTER(_UniffiRustCallStatus), ) _UniffiLib.uniffi_iota_sdk_ffi_fn_method_secp256k1publickey_to_bytes.restype = _UniffiRustBuffer +_UniffiLib.uniffi_iota_sdk_ffi_fn_method_secp256k1publickey_to_flagged_bytes.argtypes = ( + ctypes.c_void_p, + ctypes.POINTER(_UniffiRustCallStatus), +) +_UniffiLib.uniffi_iota_sdk_ffi_fn_method_secp256k1publickey_to_flagged_bytes.restype = _UniffiRustBuffer _UniffiLib.uniffi_iota_sdk_ffi_fn_clone_secp256k1signature.argtypes = ( ctypes.c_void_p, ctypes.POINTER(_UniffiRustCallStatus), @@ -5488,6 +5504,11 @@ class _UniffiForeignFutureStructVoid(ctypes.Structure): ctypes.POINTER(_UniffiRustCallStatus), ) _UniffiLib.uniffi_iota_sdk_ffi_fn_method_secp256r1publickey_to_bytes.restype = _UniffiRustBuffer +_UniffiLib.uniffi_iota_sdk_ffi_fn_method_secp256r1publickey_to_flagged_bytes.argtypes = ( + ctypes.c_void_p, + ctypes.POINTER(_UniffiRustCallStatus), +) +_UniffiLib.uniffi_iota_sdk_ffi_fn_method_secp256r1publickey_to_flagged_bytes.restype = _UniffiRustBuffer _UniffiLib.uniffi_iota_sdk_ffi_fn_clone_secp256r1signature.argtypes = ( ctypes.c_void_p, ctypes.POINTER(_UniffiRustCallStatus), @@ -7672,6 +7693,9 @@ class _UniffiForeignFutureStructVoid(ctypes.Structure): _UniffiLib.uniffi_iota_sdk_ffi_checksum_method_ed25519publickey_to_bytes.argtypes = ( ) _UniffiLib.uniffi_iota_sdk_ffi_checksum_method_ed25519publickey_to_bytes.restype = ctypes.c_uint16 +_UniffiLib.uniffi_iota_sdk_ffi_checksum_method_ed25519publickey_to_flagged_bytes.argtypes = ( +) +_UniffiLib.uniffi_iota_sdk_ffi_checksum_method_ed25519publickey_to_flagged_bytes.restype = ctypes.c_uint16 _UniffiLib.uniffi_iota_sdk_ffi_checksum_method_ed25519signature_to_bytes.argtypes = ( ) _UniffiLib.uniffi_iota_sdk_ffi_checksum_method_ed25519signature_to_bytes.restype = ctypes.c_uint16 @@ -8287,6 +8311,9 @@ class _UniffiForeignFutureStructVoid(ctypes.Structure): _UniffiLib.uniffi_iota_sdk_ffi_checksum_method_secp256k1publickey_to_bytes.argtypes = ( ) _UniffiLib.uniffi_iota_sdk_ffi_checksum_method_secp256k1publickey_to_bytes.restype = ctypes.c_uint16 +_UniffiLib.uniffi_iota_sdk_ffi_checksum_method_secp256k1publickey_to_flagged_bytes.argtypes = ( +) +_UniffiLib.uniffi_iota_sdk_ffi_checksum_method_secp256k1publickey_to_flagged_bytes.restype = ctypes.c_uint16 _UniffiLib.uniffi_iota_sdk_ffi_checksum_method_secp256k1signature_to_bytes.argtypes = ( ) _UniffiLib.uniffi_iota_sdk_ffi_checksum_method_secp256k1signature_to_bytes.restype = ctypes.c_uint16 @@ -8353,6 +8380,9 @@ class _UniffiForeignFutureStructVoid(ctypes.Structure): _UniffiLib.uniffi_iota_sdk_ffi_checksum_method_secp256r1publickey_to_bytes.argtypes = ( ) _UniffiLib.uniffi_iota_sdk_ffi_checksum_method_secp256r1publickey_to_bytes.restype = ctypes.c_uint16 +_UniffiLib.uniffi_iota_sdk_ffi_checksum_method_secp256r1publickey_to_flagged_bytes.argtypes = ( +) +_UniffiLib.uniffi_iota_sdk_ffi_checksum_method_secp256r1publickey_to_flagged_bytes.restype = ctypes.c_uint16 _UniffiLib.uniffi_iota_sdk_ffi_checksum_method_secp256r1signature_to_bytes.argtypes = ( ) _UniffiLib.uniffi_iota_sdk_ffi_checksum_method_secp256r1signature_to_bytes.restype = ctypes.c_uint16 @@ -26527,11 +26557,17 @@ def derive_address(self, ): raise NotImplementedError def scheme(self, ): """ - Return the flag for this signature scheme + Returns the signature scheme for this public key. """ raise NotImplementedError def to_bytes(self, ): + raise NotImplementedError + def to_flagged_bytes(self, ): + """ + Returns the bytes with signature scheme flag prepended. + """ + raise NotImplementedError # Ed25519PublicKey is a Rust-only trait - it's a wrapper around a Rust implementation. class Ed25519PublicKey(): @@ -26615,7 +26651,7 @@ def derive_address(self, ) -> "Address": def scheme(self, ) -> "SignatureScheme": """ - Return the flag for this signature scheme + Returns the signature scheme for this public key. """ return _UniffiConverterTypeSignatureScheme.lift( @@ -26635,6 +26671,19 @@ def to_bytes(self, ) -> "bytes": + def to_flagged_bytes(self, ) -> "bytes": + """ + Returns the bytes with signature scheme flag prepended. + """ + + return _UniffiConverterBytes.lift( + _uniffi_rust_call(_UniffiLib.uniffi_iota_sdk_ffi_fn_method_ed25519publickey_to_flagged_bytes,self._uniffi_clone_pointer(),) + ) + + + + + class _UniffiConverterTypeEd25519PublicKey: @@ -35535,11 +35584,17 @@ def derive_address(self, ): raise NotImplementedError def scheme(self, ): """ - Return the flag for this signature scheme + Returns the signature scheme for this public key. """ raise NotImplementedError def to_bytes(self, ): + raise NotImplementedError + def to_flagged_bytes(self, ): + """ + Returns the bytes with signature scheme flag prepended. + """ + raise NotImplementedError # Secp256k1PublicKey is a Rust-only trait - it's a wrapper around a Rust implementation. class Secp256k1PublicKey(): @@ -35624,7 +35679,7 @@ def derive_address(self, ) -> "Address": def scheme(self, ) -> "SignatureScheme": """ - Return the flag for this signature scheme + Returns the signature scheme for this public key. """ return _UniffiConverterTypeSignatureScheme.lift( @@ -35644,6 +35699,19 @@ def to_bytes(self, ) -> "bytes": + def to_flagged_bytes(self, ) -> "bytes": + """ + Returns the bytes with signature scheme flag prepended. + """ + + return _UniffiConverterBytes.lift( + _uniffi_rust_call(_UniffiLib.uniffi_iota_sdk_ffi_fn_method_secp256k1publickey_to_flagged_bytes,self._uniffi_clone_pointer(),) + ) + + + + + class _UniffiConverterTypeSecp256k1PublicKey: @@ -36372,11 +36440,17 @@ def derive_address(self, ): raise NotImplementedError def scheme(self, ): """ - Return the flag for this signature scheme + Returns the signature scheme for this public key. """ raise NotImplementedError def to_bytes(self, ): + raise NotImplementedError + def to_flagged_bytes(self, ): + """ + Returns the bytes with signature scheme flag prepended + """ + raise NotImplementedError # Secp256r1PublicKey is a Rust-only trait - it's a wrapper around a Rust implementation. class Secp256r1PublicKey(): @@ -36461,7 +36535,7 @@ def derive_address(self, ) -> "Address": def scheme(self, ) -> "SignatureScheme": """ - Return the flag for this signature scheme + Returns the signature scheme for this public key. """ return _UniffiConverterTypeSignatureScheme.lift( @@ -36481,6 +36555,19 @@ def to_bytes(self, ) -> "bytes": + def to_flagged_bytes(self, ) -> "bytes": + """ + Returns the bytes with signature scheme flag prepended + """ + + return _UniffiConverterBytes.lift( + _uniffi_rust_call(_UniffiLib.uniffi_iota_sdk_ffi_fn_method_secp256r1publickey_to_flagged_bytes,self._uniffi_clone_pointer(),) + ) + + + + + class _UniffiConverterTypeSecp256r1PublicKey: diff --git a/crates/iota-crypto/src/simple.rs b/crates/iota-crypto/src/simple.rs index 8830d74d3..12ba37c78 100644 --- a/crates/iota-crypto/src/simple.rs +++ b/crates/iota-crypto/src/simple.rs @@ -78,7 +78,7 @@ pub use keypair::{SimpleKeypair, SimpleVerifyingKey}; )] mod keypair { use iota_sdk_types::{ - MultisigMemberPublicKey, SignatureScheme, SimpleSignature, UserSignature, + MultisigMemberPublicKey, PublicKeyExt, SignatureScheme, SimpleSignature, UserSignature, }; use signature::{Signer, Verifier}; diff --git a/crates/iota-sdk-ffi/src/types/crypto/mod.rs b/crates/iota-sdk-ffi/src/types/crypto/mod.rs index 4cbbc167e..10796b359 100644 --- a/crates/iota-sdk-ffi/src/types/crypto/mod.rs +++ b/crates/iota-sdk-ffi/src/types/crypto/mod.rs @@ -7,7 +7,7 @@ pub mod zklogin; use std::sync::Arc; -use iota_types::SignatureScheme; +use iota_types::{PublicKeyExt, SignatureScheme}; use crate::{ error::Result, @@ -87,10 +87,15 @@ impl Ed25519PublicKey { self.0.derive_address().into() } - /// Return the flag for this signature scheme + /// Returns the signature scheme for this public key. pub fn scheme(&self) -> SignatureScheme { self.0.scheme() } + + /// Returns the bytes with signature scheme flag prepended. + pub fn to_flagged_bytes(&self) -> Vec { + self.0.to_flagged_bytes() + } } impl_crypto_object!( @@ -119,10 +124,15 @@ impl Secp256k1PublicKey { self.0.derive_address().into() } - /// Return the flag for this signature scheme + /// Returns the signature scheme for this public key. pub fn scheme(&self) -> SignatureScheme { self.0.scheme() } + + /// Returns the bytes with signature scheme flag prepended. + pub fn to_flagged_bytes(&self) -> Vec { + self.0.to_flagged_bytes() + } } impl_crypto_object!( @@ -151,10 +161,15 @@ impl Secp256r1PublicKey { self.0.derive_address().into() } - /// Return the flag for this signature scheme + /// Returns the signature scheme for this public key. pub fn scheme(&self) -> SignatureScheme { self.0.scheme() } + + /// Returns the bytes with signature scheme flag prepended + pub fn to_flagged_bytes(&self) -> Vec { + self.0.to_flagged_bytes() + } } impl_crypto_object!( diff --git a/crates/iota-sdk-types/src/crypto/ed25519.rs b/crates/iota-sdk-types/src/crypto/ed25519.rs index 15be29266..e85493b8e 100644 --- a/crates/iota-sdk-types/src/crypto/ed25519.rs +++ b/crates/iota-sdk-types/src/crypto/ed25519.rs @@ -4,6 +4,8 @@ //! Implementation of ed25519 public-key cryptogrophy. +use crate::crypto::{PublicKeyExt, SignatureScheme}; + /// An ed25519 public key. /// /// # BCS @@ -56,14 +58,25 @@ impl Ed25519PublicKey { pub const fn inner(&self) -> &[u8; Self::LENGTH] { &self.0 } +} - pub const fn as_bytes(&self) -> &[u8] { +impl PublicKeyExt for Ed25519PublicKey { + type FromBytesErr = std::array::TryFromSliceError; + + /// Returns the public key as bytes. + fn as_bytes(&self) -> &[u8] { &self.0 } - pub fn from_bytes>(bytes: T) -> Result { + /// Tries to create an Ed25519PublicKey from bytes. + fn from_bytes>(bytes: T) -> Result { <[u8; Self::LENGTH]>::try_from(bytes.as_ref()).map(Self) } + + /// Returns the signature scheme for this public key. + fn scheme(&self) -> SignatureScheme { + SignatureScheme::Ed25519 + } } impl std::str::FromStr for Ed25519PublicKey { diff --git a/crates/iota-sdk-types/src/crypto/mod.rs b/crates/iota-sdk-types/src/crypto/mod.rs index a1867df82..091131a46 100644 --- a/crates/iota-sdk-types/src/crypto/mod.rs +++ b/crates/iota-sdk-types/src/crypto/mod.rs @@ -151,3 +151,25 @@ impl_base64_helper!(Base64Array34, Base64Display34, Base64FromStr34, test34, 34) impl_base64_helper!(Base64Array48, Base64Display48, Base64FromStr48, test48, 48); impl_base64_helper!(Base64Array64, Base64Display64, Base64FromStr64, test64, 64); impl_base64_helper!(Base64Array96, Base64Display96, Base64FromStr96, test96, 96); + +pub trait PublicKeyExt: Sized { + type FromBytesErr; + + /// Returns the public key as bytes. + fn as_bytes(&self) -> &[u8]; + + /// Tries to create a PublicKey from bytes. + fn from_bytes>(bytes: T) -> Result; + + /// Returns the signature scheme for this public key. + fn scheme(&self) -> SignatureScheme; + + /// Returns the bytes with signature scheme flag prepended + fn to_flagged_bytes(&self) -> Vec { + let key_bytes = self.as_bytes(); + let mut bytes = Vec::with_capacity(1 + key_bytes.len()); + bytes.push(self.scheme().to_u8()); + bytes.extend_from_slice(key_bytes); + bytes + } +} diff --git a/crates/iota-sdk-types/src/crypto/secp256k1.rs b/crates/iota-sdk-types/src/crypto/secp256k1.rs index 03b8adcda..cad2366c7 100644 --- a/crates/iota-sdk-types/src/crypto/secp256k1.rs +++ b/crates/iota-sdk-types/src/crypto/secp256k1.rs @@ -4,6 +4,8 @@ //! Implementation of secp256k1 public-key cryptogrophy. +use crate::crypto::{PublicKeyExt, SignatureScheme}; + /// A secp256k1 public key. /// /// # BCS @@ -58,14 +60,25 @@ impl Secp256k1PublicKey { pub const fn inner(&self) -> &[u8; Self::LENGTH] { &self.0 } +} - pub const fn as_bytes(&self) -> &[u8] { +impl PublicKeyExt for Secp256k1PublicKey { + type FromBytesErr = std::array::TryFromSliceError; + + /// Returns the public key as bytes. + fn as_bytes(&self) -> &[u8] { &self.0 } - pub fn from_bytes>(bytes: T) -> Result { + /// Tries to create a Secp256k1PublicKey from bytes. + fn from_bytes>(bytes: T) -> Result { <[u8; Self::LENGTH]>::try_from(bytes.as_ref()).map(Self) } + + /// Returns the signature scheme for this public key. + fn scheme(&self) -> SignatureScheme { + SignatureScheme::Secp256k1 + } } impl std::str::FromStr for Secp256k1PublicKey { diff --git a/crates/iota-sdk-types/src/crypto/secp256r1.rs b/crates/iota-sdk-types/src/crypto/secp256r1.rs index 0537702de..153c48c03 100644 --- a/crates/iota-sdk-types/src/crypto/secp256r1.rs +++ b/crates/iota-sdk-types/src/crypto/secp256r1.rs @@ -4,6 +4,8 @@ //! Implementation of secp256r1 public-key cryptogrophy. +use crate::crypto::{PublicKeyExt, SignatureScheme}; + /// A secp256r1 public key. /// /// # BCS @@ -58,14 +60,25 @@ impl Secp256r1PublicKey { pub const fn inner(&self) -> &[u8; Self::LENGTH] { &self.0 } +} - pub const fn as_bytes(&self) -> &[u8] { +impl PublicKeyExt for Secp256r1PublicKey { + type FromBytesErr = std::array::TryFromSliceError; + + /// Returns the public key as bytes. + fn as_bytes(&self) -> &[u8] { &self.0 } - pub fn from_bytes>(bytes: T) -> Result { + /// Tries to create a Secp256r1PublicKey from bytes. + fn from_bytes>(bytes: T) -> Result { <[u8; Self::LENGTH]>::try_from(bytes.as_ref()).map(Self) } + + /// Returns the signature scheme for this public key. + fn scheme(&self) -> SignatureScheme { + SignatureScheme::Secp256r1 + } } impl std::str::FromStr for Secp256r1PublicKey { diff --git a/crates/iota-sdk-types/src/crypto/signature.rs b/crates/iota-sdk-types/src/crypto/signature.rs index 12d8caa3c..d1dc67fac 100644 --- a/crates/iota-sdk-types/src/crypto/signature.rs +++ b/crates/iota-sdk-types/src/crypto/signature.rs @@ -263,27 +263,6 @@ impl SignatureScheme { } } -impl Ed25519PublicKey { - /// Return the flag for this signature scheme - pub fn scheme(&self) -> SignatureScheme { - SignatureScheme::Ed25519 - } -} - -impl Secp256k1PublicKey { - /// Return the flag for this signature scheme - pub fn scheme(&self) -> SignatureScheme { - SignatureScheme::Secp256k1 - } -} - -impl Secp256r1PublicKey { - /// Return the flag for this signature scheme - pub fn scheme(&self) -> SignatureScheme { - SignatureScheme::Secp256r1 - } -} - impl super::ZkLoginPublicIdentifier { /// Return the flag for this signature scheme pub fn scheme(&self) -> SignatureScheme { diff --git a/crates/iota-sdk-types/src/hash.rs b/crates/iota-sdk-types/src/hash.rs index 899583235..54a66ed51 100644 --- a/crates/iota-sdk-types/src/hash.rs +++ b/crates/iota-sdk-types/src/hash.rs @@ -4,7 +4,7 @@ use blake2::Digest as DigestTrait; -use crate::{Address, Digest}; +use crate::{Address, Digest, PublicKeyExt}; type Blake2b256 = blake2::Blake2b; diff --git a/crates/iota-sdk-types/src/lib.rs b/crates/iota-sdk-types/src/lib.rs index ff0e632f7..63389f3ad 100644 --- a/crates/iota-sdk-types/src/lib.rs +++ b/crates/iota-sdk-types/src/lib.rs @@ -139,7 +139,7 @@ pub use crypto::{ Ed25519Signature, Intent, IntentAppId, IntentScope, IntentVersion, InvalidSignatureScheme, InvalidZkLoginAuthenticatorError, Jwk, JwkId, MultisigAggregatedSignature, MultisigCommittee, MultisigMember, MultisigMemberPublicKey, MultisigMemberSignature, PasskeyAuthenticator, - PasskeyPublicKey, Secp256k1PublicKey, Secp256k1Signature, Secp256r1PublicKey, + PasskeyPublicKey, PublicKeyExt, Secp256k1PublicKey, Secp256k1Signature, Secp256r1PublicKey, Secp256r1Signature, SignatureScheme, SimpleSignature, UserSignature, ZkLoginAuthenticator, ZkLoginClaim, ZkLoginInputs, ZkLoginProof, ZkLoginPublicIdentifier, }; diff --git a/crates/iota-sdk/examples/generate_ed25519_address.rs b/crates/iota-sdk/examples/generate_ed25519_address.rs index b12b0ea92..aa9337d2b 100644 --- a/crates/iota-sdk/examples/generate_ed25519_address.rs +++ b/crates/iota-sdk/examples/generate_ed25519_address.rs @@ -3,20 +3,18 @@ use base64ct::{Base64, Encoding}; use iota_crypto::{PrivateKeyExt, ed25519::Ed25519PrivateKey}; +use iota_types::PublicKeyExt; use rand::rngs::OsRng; fn main() { let private_key = Ed25519PrivateKey::generate(OsRng); let private_key_bech32 = private_key.to_bech32().unwrap(); let public_key = private_key.public_key(); + let flagged_public_key = Base64::encode_string(&public_key.to_flagged_bytes()); let address = public_key.derive_address(); - let mut flagged_public_key = vec![public_key.scheme().to_u8()]; - flagged_public_key.extend_from_slice(public_key.as_bytes()); - let encoded_public_key = Base64::encode_string(&flagged_public_key); - println!("Private Key: {private_key_bech32}"); println!("Public Key: {public_key}"); - println!("Public Key With Flag: {encoded_public_key}"); + println!("Public Key With Flag: {flagged_public_key}"); println!("Address: {address}"); }