dashpay / platform

L2 solution for seriously fast decentralized applications for the Dash network
https://dashplatform.readme.io/docs/introduction-what-is-dash-platform
MIT License
67 stars 38 forks source link

Can't use `new IdentityPublicKey(1)` from `@dashevo/wasm-dpp` #2069

Open coolaj86 opened 3 weeks ago

coolaj86 commented 3 weeks ago

Using the latest version: @dashevo/wasm-dpp@1.0.2

Here's the failing code:

let Dpp = require("@dashevo/wasm-dpp");

// ...

let MAGIC_NUMBER_1 = 1; // TODO why 1?
let dppKey = new Dpp.IdentityPublicKey(MAGIC_NUMBER_1);

And here's how it crashes.

/Users/aj/Projects/Dash/DashPlatform.js/node_modules/@dashevo/wasm-dpp/dist/wasm/wasm_dpp.js:1314
    */constructor(platform_version){try{const retptr=wasm.__wbindgen_add_to_stack_pointer(-16);wasm.identitypublickey_new(retptr,platform_version);var r0=getInt32Memory0()[retptr/4+0];var r1=getInt32Memory0()[retptr/4+1];var r2=getInt32Memory0()[retptr/4+2];if(r2){throw takeObject(r1);}return IdentityPublicKey.__wrap(r0);}finally{wasm.__wbindgen_add_to_stack_pointer(16);}}/**
                                                                                                                                                                                                                                                                                                                                                 ^

TypeError: Cannot read properties of undefined (reading '__wbindgen_add_to_stack_pointer')
    at new IdentityPublicKey (/Users/aj/Projects/Dash/DashPlatform.js/node_modules/@dashevo/wasm-dpp/dist/wasm/wasm_dpp.js:1314:338)
    at main (/Users/aj/Projects/Dash/DashPlatform.js/demo.js:92:18)

Node.js v22.5.1
pshenmic commented 3 weeks ago
const { default: loadWasmDpp, DashPlatformProtocol } = require('@dashevo/wasm-dpp');

await loadWasmDpp();

Could you try this one at the start. Dash SDK library does that internally under the hood to initialize platform protocol

coolaj86 commented 2 weeks ago

That worked.

I would suggest having a single export with an init() method. This is a fairly common way to solve this problem:

let Dpp = require('@dashevo/wasm-dpp');

async function main() {
  void await Dpp.init();
  let identityKey = new Dpp.IdentityPublicKey(MAGIC_NUMBER_1);
}

main();

Or perhaps better yet, something like

let Dpp = require('@dashevo/wasm-dpp');

async function main() {
  let dpp = await Dpp.getInstance();
  let identityKey = new dpp.IdentityPublicKey(MAGIC_NUMBER_1);
}

main();

Then you don't have to check for some state variable all over the place to throw the proper error when something is called before the initialization process is ready.

Either way, something that causes the user to fall into the pit of success.