Error with wave-crypto module when calling it to create a seed and address

a 夏天 提交于 2019-12-11 01:03:44

问题


I can not find crypto-js within the moduleand while trying to create a user wallet onclick and seed and public and private key.

I get this error message:

Error calllin module function Error can not find module "crypto-js", relatve to app/tns_modules/

Here is my code:

    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 { 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 = publicKey(seed);
        const bytes = Uint8Array.from([1, 2, 3, 4]);
        const sig = signBytes(bytes, seed);
        const isValid = verifySignature(pubKey, bytes, sig)
    }

    wallet() {
        let walletAddress = address('seed', 'T');
        keyPair('seed');
        publicKey('seed');
        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
        });
    }
}

回答1:


It seems like you have manually copied the library. Reinstall @waves/waves-crypto via npm, and import it from node_modules like this:

import * as wavesCrypto from '@waves/waves-crypto'



回答2:


Check my answer on same problem here.

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);


来源:https://stackoverflow.com/questions/55527862/error-with-wave-crypto-module-when-calling-it-to-create-a-seed-and-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!