Merge pull request #15 from status-im/Finalizer

Use finalizers to avoid manual cleaning
This commit is contained in:
Yuriy Glukhov 2018-04-11 16:58:12 +03:00 committed by GitHub
commit 94ada1958f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 10 deletions

View File

@ -25,10 +25,6 @@ when not defined(native):
## Return current error message. ## Return current error message.
result = libsecp256k1ErrorMsg() result = libsecp256k1ErrorMsg()
proc shutdown*() {.inline.} =
## Clean all backend resources
shutdownLibsecp256k1()
proc signMessage*(seckey: PrivateKey, proc signMessage*(seckey: PrivateKey,
data: openarray[byte]): Signature {.inline.} = data: openarray[byte]): Signature {.inline.} =
## Sign message of arbitrary length `data` using private key `seckey`. ## Sign message of arbitrary length `data` using private key `seckey`.

View File

@ -62,9 +62,14 @@ proc errorCallback(message: cstring, data: pointer) {.cdecl.} =
let ctx = cast[EthKeysContext](data) let ctx = cast[EthKeysContext](data)
ctx.error = $message ctx.error = $message
proc shutdownLibsecp256k1(ekContext: EthKeysContext) =
# TODO: use destructor when finalizer are deprecated for destructors
if not isNil(ekContext.context):
secp256k1_context_destroy(ekContext.context)
proc newEthKeysContext(): EthKeysContext = proc newEthKeysContext(): EthKeysContext =
## Create new `EthKeysContext`. ## Create new `EthKeysContext`.
result = new EthKeysContext new(result, shutdownLibsecp256k1)
let flags = cuint(SECP256K1_CONTEXT_VERIFY or SECP256K1_CONTEXT_SIGN) let flags = cuint(SECP256K1_CONTEXT_VERIFY or SECP256K1_CONTEXT_SIGN)
result.context = secp256k1_context_create(flags) result.context = secp256k1_context_create(flags)
secp256k1_context_set_illegal_callback(result.context, illegalCallback, secp256k1_context_set_illegal_callback(result.context, illegalCallback,
@ -313,8 +318,3 @@ proc signRawMessage*(data: openarray[byte], seckey: PrivateKey,
nil, nil) != 1: nil, nil) != 1:
return(EthKeysStatus.Error) return(EthKeysStatus.Error)
return(EthKeysStatus.Success) return(EthKeysStatus.Success)
proc shutdownLibsecp256k1() =
if not isNil(ekContext):
secp256k1_context_destroy(ekContext.context)
ekContext = nil