错误无法在wave-crypto npm模块中找到模块cryptojs [英] Error cannot find module cryptojs in waves-crypto npm module

查看:181
本文介绍了错误无法在wave-crypto npm模块中找到模块cryptojs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用@ waves/waves-crypto从我的.ts文件中的'@ waves/waves-crypto'中导入*作为waveCrypto,但在npm模块本身中仍然出现错误.我正在尝试使用nativescript创建wave钱包,现在我正在尝试为用户创建地址,种子以及公共和私有密钥.这是login.ts,我在其中调用@ waves/waves-crypto

I am trying to use @waves/waves-crypto I have import * as wavesCrypto from '@waves/waves-crypto' in my .ts file but I am still getting error within the npm module itself. I am trying to create a waves wallet using nativescript and right now I am trying to create the address and seed and public and private key for the user. this is login.ts where im calling the @waves/waves-crypto

import { Component, ElementRef, ViewChild } from "@angular/core";
import { Router } from "@angular/router";
import { alert, prompt } from "tns-core-modules/ui/dialogs";
import { Page } from "tns-core-modules/ui/page";
import { Routes } from "@angular/router";
//import { publicKey, verifySignature, signBytes, address, keyPair, privateKey } from "../@waves/waves-crypto";
import * as wavesCrypto from '../@waves/waves-crypto';

import { User } from "../shared/user.model";
import { UserService } from "../shared/user.service";

@Component({
    selector: "app-login",
    moduleId: module.id,
    templateUrl: "./login.component.html",
    styleUrls: ['./login.component.css']
})
export class LoginComponent {
    isLoggingIn = true;
    user: User;
    @ViewChild("password") password: ElementRef;
    @ViewChild("confirmPassword") confirmPassword: ElementRef;
    @ViewChild("waves") waves: ElementRef;

    constructor(private page: Page, private userService: UserService, private router: Router) {
        this.page.actionBarHidden = true;
        this.user = new User();
        // this.user.email = "foo2@foo.com";
        // this.user.password = "foo";
        const seed = 'magicseed';
        const pubKey = wavesCrypto.publicKey(seed);
        const bytes = Uint8Array.from([1, 2, 3, 4]);
        const sig = wavesCrypto.signBytes(bytes, seed);
        const isValid = wavesCrypto.verifySignature(pubKey, bytes, sig)
    }

    wallet() {
        let walletAddress = wavesCrypto.address('seed', 'T');
        let keyPair = wavesCrypto.keyPair('seed');
        //publicKey('seed');
        //privateKey('seed');
        wavesCrypto.privateKey('seed');
        alert(walletAddress);
        console.log(walletAddress);
        console.log(keyPair);
    }

    toggleForm() {
        this.isLoggingIn = !this.isLoggingIn;
    }

    submit() {
        if (!this.user.email || !this.user.password) {
            this.alert("Please provide both an email address and password.");
            return;
        }

        if (this.isLoggingIn) {
            this.login();
        } else {
            this.register();
        }
    }

    login() {
        this.userService.login(this.user)
            .then(() => {
                this.router.navigate(["/home"]);
            })
            .catch(() => {
                this.alert("Unfortunately we could not find your account.");
            });
    }

    register() {
        if (this.user.password != this.user.confirmPassword) {
            this.alert("Your passwords do not match.");
            return;
        }
        this.userService.register(this.user)
            .then(() => {
                this.alert("Your account was successfully created.");
                this.isLoggingIn = true;
            })
            .catch(() => {
                this.alert("Unfortunately we were unable to create your account.");
            });
    }

    forgotPassword() {
        prompt({
            title: "Forgot Password",
            message: "Enter the email address you used to register for APP NAME to reset your password.",
            inputType: "email",
            defaultText: "",
            okButtonText: "Ok",
            cancelButtonText: "Cancel"
        }).then((data) => {
            if (data.result) {
                this.userService.resetPassword(data.text.trim())
                    .then(() => {
                        this.alert("Your password was successfully reset. Please check your email for instructions on choosing a new password.");
                    }).catch(() => {
                        this.alert("Unfortunately, an error occurred resetting your password.");
                    });
            }
        });
    }

    focusPassword() {
        this.password.nativeElement.focus();
    }
    focusConfirmPassword() {
        if (!this.isLoggingIn) {
            this.confirmPassword.nativeElement.focus();
        }
    }

    alert(message: string) {
        return alert({
            title: "APP NAME",
            okButtonText: "OK",
            message: message
        });
    }
}

推荐答案

我遇到了同样的问题,我在Github存储库上打开了下一个问题(您可以点击并点赞或评论),

I have the same problem and I opened the next issue on the Github repo (you can go and click like or comment), link here

在这个问题中,我解释了一种可用于验证签名的变通方法,您可以使用相同的代码段.

In the issue I explain a workaround that is working for me to validate a signature, you can use the same snippet.

首先手动导入所需的子模块:

First import manually the submodules needed:

import { default as axlsign } from '@waves/signature-generator/libs/axlsign';
import { default as convert } from '@waves/signature-generator/dist/utils/convert';
import { concatUint8Arrays } from '@waves/signature-generator/dist/utils/concat';
import { default as base58 } from '@waves/signature-generator/dist/libs/base58';

然后,您可以使用下面的代码来验证签名和公钥:

Then you can use the next code to validate the signature and publickey:

let prefix = "WavesWalletAuthentication";
let host = new URL(yourServerUrl).hostname;

let user = wavesAddressString;
let payload = theStringThatWasSigned;

let data = [prefix, host, payload]
    .map(d => convert.stringToByteArrayWithSize(d))
    .map(stringWithSize => Uint8Array.from(stringWithSize));
let dataBytes = concatUint8Arrays(...data);

let publicKeyBytes = base58.decode(publicKeyOnBase58Format);
let signatureBytes = base58.decode(signatureOnBase58Format);

let validSignature = axlsign.verify(publicKeyBytes, dataBytes, signatureBytes);
console.log("(login) validSignature?", validSignature);

这篇关于错误无法在wave-crypto npm模块中找到模块cryptojs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆