1

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?

vkefallinos
  • 717
  • 1
  • 9
  • 24

1 Answers1

1

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)"
}
Ingo Radatz
  • 1,225
  • 8
  • 9