Coder Social home page Coder Social logo

not support for substrate chain about snap HOT 2 CLOSED

BurnWW avatar BurnWW commented on September 28, 2024
not support for substrate chain

from snap.

Comments (2)

Nick-1979 avatar Nick-1979 commented on September 28, 2024

@BurnWW do you have any suggestions how to get RPC urls to support more chains?

from snap.

BurnWW avatar BurnWW commented on September 28, 2024

@Nick-1979
I took the time to validate the feasibility of my idea, and I hope that instead of connecting to blockchain networks within Snap, we can directly use metadata for signing.
This is the code that I have successfully verified.

packages/snap/src/rpc/signJSON.ts

import { SignerPayloadJSON } from '@polkadot/types/types';
import type { SignerResult } from '@polkadot/api/types';
import { getApi } from '../util/getApi';
import { getKeyPair, getKeyPairFromMetadata } from '../util/getKeyPair';
import { checkAndUpdateMetaData, getSavedMeta, showConfirmTx } from '.';
import { metadataExpand } from '@polkadot/extension-chains';

export const signJSON = async (
  origin: string,
  payload: SignerPayloadJSON,
): Promise<SignerResult | undefined> => {
  try {
    const def = await getSavedMeta(payload.genesisHash);
    if (def) {
      const chain = metadataExpand(def!, false)
      chain.registry.signedExtensions

      const registry = chain.registry;
      registry.setSignedExtensions(payload.signedExtensions, chain.definition.userExtensions);
      const keyPair = await getKeyPairFromMetadata(chain);

      const { signature } = registry
        .createType('ExtrinsicPayload', payload, { version: payload.version })
        .sign(keyPair);

      return { id: 1, signature };
    }

    ......
  } catch (e) {
    console.info('Error while signing JSON:', e);
    return undefined;
  }
};
export const getKeyPairFromMetadata = async (
  chain: Chain
): Promise<KeyringPair> => {
  const prefix = chain.ss58Format

  const BIP44CoinNode = (await snap.request({
    method: 'snap_getBip44Entropy',
    params: {
      coinType: DEFAULT_COIN_TYPE,
    },
  })) as JsonBIP44CoinTypeNode;

  const seed = BIP44CoinNode?.privateKey?.slice(0, 32);
  const keyring = new Keyring({ ss58Format: prefix });
  const keyPair = keyring.addFromSeed(stringToU8a(seed));

  return keyPair;
};

from snap.

Related Issues (19)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.