oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
73.02k stars 2.66k forks source link

Issues with monero-ts and ethereumjs-wallet #7559

Open zigazajc007 opened 9 months ago

zigazajc007 commented 9 months ago

What version of Bun is running?

1.0.15 and 1.0.16+7541f4ce4

What platform is your computer?

Fedora Linux 6.6.4-200.fc39.x86_64 x86_64

What steps can reproduce the bug?

import * as moneroTs from "monero-ts";

export default class Monero{

    static async getAddress(mnemonic: string, accountIndex: number, addressIndex: number) : Promise<{ publicKey: string; privateKey: string; }>{

        const account = await moneroTs.createWalletKeys({ seed: mnemonic, networkType: moneroTs.MoneroNetworkType.MAINNET });
        const address = await account.createSubaddress(addressIndex);

        const publicKey = address.address;
        const privateKey = await account.getPrivateSpendKey()

        return {
            publicKey: publicKey,
            privateKey: privateKey
        };
    }

}

console.log(await Monero.getAddress("ankle check squeeze bright caution lemon snap limit employ reflect vague supreme syrup describe right unit web toward cluster suit glue hip huge find", 0, 0));

What is the expected behavior?

Return public and private keys and not crash:

What do you see instead?

error: script "start" exited with code 11 (SIGSEGV)

or

Segmentation fault (core dumped)

Additional information

For monero-ts success rate is 0% while with ethereumjs-wallet for generating ethereum wallets the success rate is around 20%.

Jarred-Sumner commented 9 months ago

debug build logs (for any contributors):

ASSERTION FAILED: cell->isObject()
/Users/jarred/Code/bun/src/bun.js/WebKit/WebKitBuild/Debug/JavaScriptCore/PrivateHeaders/JavaScriptCore/JSObject.h(1442) : JSObject *JSC::asObject(JSCell *)
1   0x107370458 WTFCrash
2   0x106da75f0 Bun::Process_functionDlopen(JSC::JSGlobalObject*, JSC::CallFrame*)
3   0x106e10fbc JSC::asObject(JSC::JSCell*)
4   0x1081b1fb8 JSC::JSCell::putInline(JSC::JSGlobalObject*, JSC::PropertyName, JSC::JSValue, JSC::PutPropertySlot&)
5   0x108cec7b0 JSC::JSValue::putInline(JSC::JSGlobalObject*, JSC::PropertyName, JSC::JSValue, JSC::PutPropertySlot&)
6   0x108aa9cf0 llint_slow_path_put_by_id
7   0x107566854 llint_entry
8   0x107559d08 vmEntryToJavaScript
9   0x108928024 JSC::Interpreter::executeCallImpl(JSC::VM&, JSC::JSObject*, JSC::CallData const&, JSC::JSValue, JSC::ArgList const&)
10  0x108928114 JSC::Interpreter::executeCall(JSC::JSObject*, JSC::CallData const&, JSC::JSValue, JSC::ArgList const&)
11  0x108cbba2c JSC::call(JSC::JSGlobalObject*, JSC::JSValue, JSC::CallData const&, JSC::JSValue, JSC::ArgList const&)
12  0x108cbbaf0 JSC::call(JSC::JSGlobalObject*, JSC::JSValue, JSC::CallData const&, JSC::JSValue, JSC::ArgList const&, WTF::NakedPtr<JSC::Exception>&)
13  0x108cbbd9c JSC::profiledCall(JSC::JSGlobalObject*, JSC::ProfilingReason, JSC::JSValue, JSC::CallData const&, JSC::JSValue, JSC::ArgList const&, WTF::NakedPtr<JSC::Exception>&)
14  0x106f70784 WebCore::JSEventListener::handleEvent(WebCore::ScriptExecutionContext&, WebCore::Event&)
15  0x106f20320 WebCore::EventTarget::innerInvokeEventListeners(WebCore::Event&, WTF::Vector<WTF::RefPtr<WebCore::RegisteredEventListener, WTF::RawPtrTraits<WebCore::RegisteredEventListener>, WTF::DefaultRefDerefTraits<WebCore::RegisteredEventListener>>, 1ul, WTF::CrashOnOverflow, 2ul, WTF::FastMalloc>, WebCore::EventTarget::EventInvokePhase)
16  0x106f1fee4 WebCore::EventTarget::fireEventListeners(WebCore::Event&, WebCore::EventTarget::EventInvokePhase)
17  0x106f1fd6c WebCore::EventTarget::dispatchEvent(WebCore::Event&)
18  0x106ff4e2c WebWorker__dispatchError
19  0x10674c20c src.bun.js.web_worker.WebWorker.onUnhandledRejection
20  0x1051fe3ec src.bun.js.javascript.VirtualMachine.onUnhandledError
21  0x1051fe2f8 Bun__reportUnhandledError
22  0x106dfe33c WebCore::reportException(JSC::JSGlobalObject*, JSC::Exception*, WebCore::CachedScript*, bool, WebCore::ExceptionDetails*)
23  0x106f707b4 WebCore::JSEventListener::handleEvent(WebCore::ScriptExecutionContext&, WebCore::Event&)
24  0x106f20320 WebCore::EventTarget::innerInvokeEventListeners(WebCore::Event&, WTF::Vector<WTF::RefPtr<WebCore::RegisteredEventListener, WTF::RawPtrTraits<WebCore::RegisteredEventListener>, WTF::DefaultRefDerefTraits<WebCore::RegisteredEventListener>>, 1ul, WTF::CrashOnOverflow, 2ul, WTF::FastMalloc>, WebCore::EventTarget::EventInvokePhase)
25  0x106f1fee4 WebCore::EventTarget::fireEventListeners(WebCore::Event&, WebCore::EventTarget::EventInvokePhase)
26  0x106f1fd6c WebCore::EventTarget::dispatchEvent(WebCore::Event&)
27  0x106ff4e2c WebWorker__dispatchError
28  0x10674c20c src.bun.js.web_worker.WebWorker.onUnhandledRejection
29  0x1051fe3ec src.bun.js.javascript.VirtualMachine.onUnhandledError
30  0x106a82ed4 src.bun.js.web_worker.WebWorker.spin
31  0x106933240 src.bun.js.javascript.OpaqueWrap__anon_316288__struct_329561.callback
fish: Job 1, 'bun-debug $argv' terminated by signal SIGSEGV (Address boundary error)
Jarred-Sumner commented 9 months ago

I have a partial fix, it's caused by an exception being thrown inside the Worker's error handler, causing it to recursively throw errors until the process runs out of memory. The real fix is that for reasons not yet clear, the web-worker package does not work

zigazajc007 commented 9 months ago

The issue with ethereumjs-wallet is a little different.

This code works every time:

import bip39 from 'bip39';
import { hdkey } from 'ethereumjs-wallet';

export default class Ethereum{

    static getAddress(mnemonic: string, accountIndex: number, addressIndex: number) : { publicKey: string, privateKey: string }{
        const node = hdkey.fromMasterSeed(bip39.mnemonicToSeedSync(mnemonic));

        const account = node.derivePath(`m/44'/60'/0'/0/${accountIndex}`);
        const address = account.deriveChild(addressIndex);

        return {
            publicKey: address.getWallet().getAddressString(),
            privateKey: account.getWallet().getPrivateKeyString()
        };
    }

}

console.log(Ethereum.getAddress("ankle check squeeze bright caution lemon snap limit employ reflect vague supreme syrup describe right unit web toward cluster suit glue hip huge find",0,0));

but if I separate bip39 to another file, it doesn't work every time. (Segmentation fault (core dumped) most of the time) image

crypto.ts:

import * as ecc from 'tiny-secp256k1';
import BIP32Factory, { BIP32Interface } from 'bip32';
import bip39 from 'bip39';

export default class Crypto{

    static generateMnemonic(): string{
        return bip39.generateMnemonic(256);
    }

    static getSeed(mnemonic: string): Buffer{
        return bip39.mnemonicToSeedSync(mnemonic);
    }

    static getBIP32(seed: Buffer): BIP32Interface{
        const bip32 = BIP32Factory(ecc);
        return bip32.fromSeed(seed);
    }

}

ethereum.ts

import Crypto from './crypto';
import { hdkey } from 'ethereumjs-wallet';

export default class Ethereum{

    static getAddress(mnemonic: string, accountIndex: number, addressIndex: number) : { publicKey: string, privateKey: string }{
        const seed = Crypto.getSeed(mnemonic);
        const node = hdkey.fromMasterSeed(seed);

        const account = node.derivePath(`m/44'/60'/0'/0/${accountIndex}`);
        const address = account.deriveChild(addressIndex);

        return {
            publicKey: address.getWallet().getAddressString(),
            privateKey: account.getWallet().getPrivateKeyString()
        };
    }

}

console.log(Ethereum.getAddress("ankle check squeeze bright caution lemon snap limit employ reflect vague supreme syrup describe right unit web toward cluster suit glue hip huge find",0,0));

both crypto.ts and ethereum.ts are located in the same folder and execution command is: bun ethereum.ts

Jarred-Sumner commented 8 months ago

In #7980, the crash is fixed. But there are still some errors due to bugs in our node:worker_threads implementation. Specifically, some cases where we are returning web worker types instead of node worker_threads types. These bugs need to be fixed, but another path forward is https://github.com/developit/web-worker/pull/43