简体   繁体   中英

Add tweetNaCL-js crypto library to CouchDB validate_doc function

I am trying to validate a document using public key crypto ( tweetnacl ). I know that you can add commonjs modules to validate functions but haven't been able to.

{
 "_id": "_design/validate_update",
 "language": "javascript",
 "validate_doc_update": 
           "function(newDoc, oldDoc, userCtx){     
                verify=require('lib/validation').sign.detached.verify;
                if(verify(newDoc.message, new.Doc.signature, oldDoc.publicKey)){
                     return true;
                }
               }",
 "lib": {
     "validation": "exports.nacl=(function(nacl){..... })"
 }
}

When I do this I get the error:

  Module require('lib/validation') raised error (new TypeError("func.apply is not a function", "/usr/local/share/couchdb/server/main.js", 1181))

I suppose I have to somehow change the tweetnacl code to be interpreted as a commonjs module?

Seems like the initialisation of the NaCl lib as commonjs module is failing because its an anonymous function - try to give NaCl exports as context:

"lib": {
 "validation": "(function(nacl){..... })(exports)"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM