Zondax / filecoin-signing-tools

Filecoin Signing Library
Apache License 2.0
106 stars 50 forks source link

BLS Signature verification #315

Closed ukstv closed 4 years ago

ukstv commented 4 years ago

Trying to employ the library to verify BLS signatures. Do you guys have an example of how to do that? I have tried to call verifySignature based on one of the tests supplied with the library (tests_basic.js), it reports Error verifying signature: bls error:

it('BLS test', () => {
        const tc = {
            "pk": "af2f1d46d7f618997f43fbc3b7e4d4fb6ca8bc290e161e1f8643f0d894509814da5a5ff8729fc8935086a334469a3702",
            "sk": "9c26a653e3f6feed763fee9e163a4863252fd3ca38bfeeaa83dab2799b5cf10c",
            "sig": "b6b660b9557b722f2b20770511aa1761836c4352313002030ab4229273eb3f9ea09ca8490176f361dabec4c64cc62e130ce1bb2d0a9fe143b4e844de20e311b842b465900f4c3f861d46755c2dcca5f2effb97c0da379fbf7a4e046f909a0fa5",
            "message": {
                "to": "t17uoq6tp427uzv7fztkbsnn64iwotfrristwpryy",
                "from": "t3v4xr2rwx6ymjs72d7pb3pzgu7nwkrpbjbylb4h4gipynrfcqtaknuws77bzj7setkcdkgncgti3qfa4n7seq",
                "nonce": 1,
                "value": "100000",
                "gaslimit": 25000,
                "gasfeecap": "1",
                "gaspremium": "1",
                "method": 0,
                "params": ""
            }
        }
        const signed_tx = filecoin_signer.transactionSign(tc.message, Buffer.from(tc.sk, "hex").toString("base64"));
        const signature = Buffer.from(signed_tx.signature.data, 'base64');
        const tx = filecoin_signer.transactionSerialize(tc.message)
        const v = filecoin_signer.verifySignature(signature.toString('hex'), tx)
        console.log('v', v)
    })

Kind of the same way I tried to verify signature made by Lotus, the same error. Check script below:

import { NodejsProvider } from "@filecoin-shipyard/lotus-client-provider-nodejs";
import { LotusRPC } from "@filecoin-shipyard/lotus-client-rpc";
import * as schema from "@filecoin-shipyard/lotus-client-schema";
import * as zondax from "@zondax/filecoin-signing-tools";
import * as uint8arrays from "uint8arrays";

const TODO_LOTUS_HOST = "ws://localhost:7777/0/node/v0";

async function main() {
  const provider = new NodejsProvider(TODO_LOTUS_HOST);
  const lotus = new LotusRPC(provider, { schema: schema.testnet.fullNode });
  const list = await lotus.walletList();
  const address = list.find((a) => a.startsWith("t3"));
  console.log("list", list);
  console.log("selected address", address);
  const message = {
    To: address,
    From: address,
    Value: "0",
    Method: 0,
    GasPrice: "1",
    GasLimit: 1000,
    GasFeeCap: "1",
    GasPremium: "1",
    Nonce: 0,
    Params: "Ynl0ZSBhcnJheQ==",
  };
  const lotusSignature = await lotus.walletSignMessage(address, message);
  console.log("lotusSignature", lotusSignature);
  const transaction = zondax.transactionSerialize(message);
  const signature = uint8arrays.fromString(
    lotusSignature.Signature.Data,
    "base64"
  );
  const signatureHex = uint8arrays.toString(signature, "base16");
  const verif = await zondax.verifySignature(signatureHex, transaction);
  console.log("verif", verif);
}

main();

What verifySignature expects as input? If there is a test case in the codebase that could serve as a clue, that would be cool too.

rllola commented 4 years ago

Hi! There is indeed an issue with the BLS verification signature. It wasn't correctly creating the BLS public key from the address. I have push a PR. I have added some tests for it too.

ukstv commented 4 years ago

Aww, sweet! Do I understand correctly, that (1) the signature from Lotus could be verified here, and (2, minor) I would have to transform from Lotus base32 signature to hex so that it is correctly verified?

rllola commented 4 years ago

Yes the lotus signature should be verified using this function and you can actually keep the base64 signature encoding. No need to convert to hex, both encoding should work.

Note: lotus signatures are returned as base64 not base32 if I am not mistaken.

ukstv commented 4 years ago

It's all base-whatever-all-over-the-place. I might be mistaken here. This is really awesome I do not have to convert the formats, thanks!!