Merge pull request #15 from status-im/Finalizer
Use finalizers to avoid manual cleaning
This commit is contained in:
commit
94ada1958f
|
@ -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`.
|
||||||
|
|
|
@ -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
|
|
||||||
|
|
Loading…
Reference in New Issue