polkadot-js / extension

Simple browser extension for managing Polkadot and Substrate network accounts in a browser. Allows the signing of extrinsics using these accounts. Also provides a simple interface for compliant extensions for dapps.
Apache License 2.0
980 stars 415 forks source link

Method to return a signature back to the UI #380

Closed araa47 closed 4 years ago

araa47 commented 4 years ago

Hi, I currently need to sign a message and return this signature back to my UI.

With KeyRings I could initially do

const signature = Alice.sign(message, {withType: true})

how do i achieve a similar signature using the injected address?

Ive tried

const allInjected = await web3Enable('my cool dapp');
const allAccounts = await web3Accounts();
// get address 0 
const defaultAddress = allAccounts[0]['address'];
const injector = await web3FromAddress(defaultAddress);
const signature = injector.signer.sign(message, {withType: true})

but I get the following error

injector.signer.sign is not a function

jacogr commented 4 years ago

The extension implements the Signer interface, which looks like this -

export interface Signer {
  /**
   * @description signs an extrinsic payload from a serialized form
   */
  signPayload?: (payload: SignerPayloadJSON) => Promise<SignerResult>;

  /**
   * @description signs a raw payload, only the bytes data as supplied
   */
  signRaw?: (raw: SignerPayloadRaw) => Promise<SignerResult>;

  /**
   * @description Receives an update for the extrinsic signed by a `signer.sign`
   */
  update?: (id: number, status: H256 | ISubmittableResult) => void;
}

The signRaw is what you are looking for. Passing through (data as hex) { address: string, data: string, type: 'payload' | 'bytes' }

araa47 commented 4 years ago

Im able to call the method signRaw and pass it the payload, however the polkadot extension seems to be stuck after I enter my password. It does not seem to return anything. How can i work on debugging this problem? Ive tried re-installing the extension and setting up a new wallet but ran into the same problem.

jacogr commented 4 years ago

Check the logs, ensure you have the correct password, ensure the encoding is correct, example of use in the wild -

araa47 commented 4 years ago

Thanks, seems like I'm able to generate a signature without issues now. However I am having troubles verifying this generated signature. It seems to be different from what I generate when I use keyRing. I noticed the length of the signature when converted to Uint8Array was different, so I assumed when using signer.signRaw, I need to add the type info on my own. However this does not seem to solve my issue and I still have problems verifying my signature generated in this method.

Working signature + verification using keyring (@polkadot/api)

Signature Generation using js script and keyring (keyRingPair = Alice/Bob)

// takes an offer and returns a signature
async signOffer(keyRingPair, offer) {
  const encodedOffer = offer.toU8a();
  const signature = keyRingPair.sign(encodedOffer, { withType: true });
  return signature;
}

Signature Verification

// takes an offer type 
asyn verify(offer, signature, address){
 const encoded_offer = offer.toU8a();
 const hexsig = signature.toHex();
 const isValid = UtilCrypto.signatureVerify(encoded_offer, hexsig, address);
}

Polkadot Extension Version

Signature Generation


async signOffer(injector, offer, address){
    // offer is a custom type on my chain 
    const encodedData = offer.toU8a();
    const { signature } = await injector.signer.signRaw({
      address: address,
      data: stringToHex(encodedData),
      type: 'bytes'
      });
    // convert to u8a array 
    const encodedSig = parrotSwap.parrot.util.hexToU8a(signature);
    // add type information to this signature so length is 65 
    const encodedSigWithType = u8aConcat(new Uint8Array([0]), encodedSig);
    return encodedSigWithType
}

Signature Verification

Same as above

Error

Invalid Signature 
jacogr commented 4 years ago

Yes, it is a straight 64-byte signature.

const encodedSigWithType = u8aConcat(new Uint8Array([0]), encodedSig);

The extension signatures are sr25519 - https://github.com/polkadot-js/common/blob/master/packages/keyring/src/pair/index.ts#L23

polkadot-js-bot commented 3 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue if you think you have a related problem or query.