Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

oneFromBoc is not working because isomorphic-webcrypto is not defined in nextjs #122

Open
samyarkd opened this issue Dec 29, 2023 · 4 comments

Comments

@samyarkd
Copy link

The oneFromBoc is not working as expected and returns an error

⨯ TypeError: Cannot read properties of undefined (reading 'digest')
    at sha256 (webpack-internal:///(rsc)/../../node_modules/tonweb/src/utils/Utils.js:19:32)
    at Cell.hash (webpack-internal:///(rsc)/../../node_modules/tonweb/src/boc/Cell.js:130:37)
    at async Cell.getRepr (webpack-internal:///(rsc)/../../node_modules/tonweb/src/boc/Cell.js:118:28)
    at async Cell.hash (webpack-internal:///(rsc)/../../node_modules/tonweb/src/boc/Cell.js:130:44)
    at async bocToTransactionHash (webpack-internal:///(rsc)/../../libs/shared/src/lib/ton.ts:30:84)
const BN = require("bn.js");
const nacl = require("tweetnacl");
const ethunit = require("ethjs-unit");

const isCryptoAvailable = typeof self !== 'undefined' && self.crypto && self.crypto.subtle;

let myCrypto = null;

if (isCryptoAvailable) { // web
    // nothing to do
} else { // nodejs or react-native
    myCrypto = require('isomorphic-webcrypto');
}

/**
 * @param bytes {Uint8Array}
 * @return  {Promise<ArrayBuffer>}
 */
function sha256(bytes) {
    if (isCryptoAvailable) { // web
        return crypto.subtle.digest("SHA-256", bytes);
    } else {  // nodejs or react-native
        return myCrypto.subtle.digest({name:"SHA-256"}, bytes);
    }
}

the sha256 is where the error happens and it's mostlikely that is happening here

} else { // nodejs or react-native
    myCrypto = require('isomorphic-webcrypto');
}

Solution

we could fix it using an alternative that does the same but works in both nodejs and browser

@samyarkd
Copy link
Author

samyarkd commented Jan 1, 2024

here is a reproduction of the error (sandbox)

it looks like isomorphic-webcrypto does not work with NextJs server side

go to localhost:3000/api and check the terminal output for the error

in Nextjs we can just do const crypto = require('crypto') but I'm not sure if it's going to work in other environments too

Solution

I cloned the repo and used globalThis to fix this problem but based on the MDN docs it's supported in node 19 and higher

@samyarkd samyarkd changed the title oneFromBoc is not working because Crypto is not defined oneFromBoc is not working because isomorphic-webcrypto is not defined in nextjs Jan 1, 2024
@di-sukharev
Copy link

same here

@di-sukharev
Copy link

FAILED_TO_CALL TypeError: Cannot read properties of undefined (reading 'digest')
    at sha256 (webpack-internal:///(rsc)/./node_modules/tonweb/src/utils/Utils.js:18:32)
    at Cell.hash (webpack-internal:///(rsc)/./node_modules/tonweb/src/boc/Cell.js:130:37)
    at async Cell.getRepr (webpack-internal:///(rsc)/./node_modules/tonweb/src/boc/Cell.js:118:28)
    at async Cell.hash (webpack-internal:///(rsc)/./node_modules/tonweb/src/boc/Cell.js:130:44)
    at async WalletV3ContractR2.createStateInit (webpack-internal:///(rsc)/./node_modules/tonweb/src/contract/index.js:43:31)
    at async WalletV3ContractR2.getAddress (webpack-internal:///(rsc)/./node_modules/tonweb/src/contract/index.js:18:29)

"tonweb": "^0.0.64",
"tonweb-mnemonic": "^1.0.1",
"next": "^14.0.3",

@895268679long
Copy link

May I ask how to convert mnemonics into public and private keys?

di-sukharev added a commit to di-sukharev/tonweb that referenced this issue Apr 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants