diff --git a/src/eckey.cc b/src/eckey.cc index 8c99e20..a6f1681 100644 --- a/src/eckey.cc +++ b/src/eckey.cc @@ -108,6 +108,8 @@ void Key::Init(Handle target) GetPrivate, SetPrivate); s_ct->InstanceTemplate()->SetAccessor(String::New("public"), GetPublic, SetPublic); + s_ct->InstanceTemplate()->SetAccessor(String::New("compressed"), + GetCompressed, SetCompressed); // Methods NODE_SET_PROTOTYPE_METHOD(s_ct, "verifySignature", VerifySignature); @@ -126,6 +128,7 @@ void Key::Init(Handle target) Key::Key() : lastError(NULL), + isCompressed(true), hasPrivate(false), hasPublic(false) { @@ -240,6 +243,28 @@ Key::SetPrivate(Local property, Local value, const AccessorInfo& key->hasPrivate = true; } +void +Key::SetCompressed(Local property, Local value, const AccessorInfo& info) +{ + if (!value->IsBoolean()) { + ThrowException(Exception::Error(String::New("compressed must be boolean"))); + return; + } + + Key* key = node::ObjectWrap::Unwrap(info.Holder()); + + key->isCompressed = value->BooleanValue(); +} + +Handle +Key::GetCompressed(Local property, const AccessorInfo& info) +{ + HandleScope scope; + Key* key = node::ObjectWrap::Unwrap(info.Holder()); + + return scope.Close(Boolean::New(key->isCompressed)); +} + Handle Key::GetPublic(Local property, const AccessorInfo& info) { @@ -250,6 +275,10 @@ Key::GetPublic(Local property, const AccessorInfo& info) return scope.Close(Null()); } + // Set compressed/uncompressed (we prefer compressed) + EC_KEY_set_conv_form(key->ec, key->isCompressed ? + POINT_CONVERSION_COMPRESSED : POINT_CONVERSION_UNCOMPRESSED); + // Export public int pub_size = i2o_ECPublicKey(key->ec, NULL); if (!pub_size) { diff --git a/src/eckey.h b/src/eckey.h index 00a0ae1..8322ff1 100644 --- a/src/eckey.h +++ b/src/eckey.h @@ -16,6 +16,7 @@ private: const char *lastError; EC_KEY *ec; + bool isCompressed; bool hasPrivate; bool hasPublic; @@ -70,6 +71,12 @@ public: static void SetPublic(Local property, Local value, const AccessorInfo& info); + static Handle + GetCompressed(Local property, const AccessorInfo& info); + + static void + SetCompressed(Local property, Local value, const AccessorInfo& info); + static Handle RegenerateSync(const Arguments& args);