From 2a9ac92db9e69d9a1e303ed37e3fa692337bc22b Mon Sep 17 00:00:00 2001 From: Michal Iskierko Date: Tue, 25 Oct 2022 11:32:26 +0200 Subject: [PATCH] Expose 33-bytes compression functions Issue #8001 --- mobile/status.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/mobile/status.go b/mobile/status.go index fbcae19cf..23fc03939 100644 --- a/mobile/status.go +++ b/mobile/status.go @@ -19,6 +19,7 @@ import ( abi_spec "github.com/status-im/status-go/abi-spec" "github.com/status-im/status-go/api" "github.com/status-im/status-go/api/multiformat" + "github.com/status-im/status-go/eth-node/crypto" "github.com/status-im/status-go/eth-node/types" "github.com/status-im/status-go/exportlogs" "github.com/status-im/status-go/extkeys" @@ -29,6 +30,7 @@ import ( "github.com/status-im/status-go/params" "github.com/status-im/status-go/profiling" protocol "github.com/status-im/status-go/protocol" + "github.com/status-im/status-go/protocol/common" identityUtils "github.com/status-im/status-go/protocol/identity" "github.com/status-im/status-go/protocol/identity/alias" "github.com/status-im/status-go/protocol/identity/colorhash" @@ -734,6 +736,32 @@ func ValidateMnemonic(mnemonic string) string { return makeJSONResponse(err) } +// DecompressPublicKey decompresses 33-byte compressed format to uncompressed 65-byte format. +func DecompressPublicKey(key string) string { + decoded, err := types.DecodeHex(key) + if err != nil { + return makeJSONResponse(err) + } + const compressionBytesNumber = 33 + if len(decoded) != compressionBytesNumber { + return makeJSONResponse(errors.New("key is not 33 bytes long")) + } + pubKey, err := crypto.DecompressPubkey(decoded) + if err != nil { + return makeJSONResponse(err) + } + return types.EncodeHex(crypto.FromECDSAPub(pubKey)) +} + +// CompressPublicKey compresses uncompressed 65-byte format to 33-byte compressed format. +func CompressPublicKey(key string) string { + pubKey, err := common.HexToPubkey(key) + if err != nil { + return makeJSONResponse(err) + } + return types.EncodeHex(crypto.CompressPubkey(pubKey)) +} + // SerializePublicKey compresses an uncompressed multibase encoded multicodec identified EC public key // For details on usage see specs https://specs.status.im/spec/2#public-key-serialization func MultiformatSerializePublicKey(key, outBase string) string {