@polkadot/types/types#SignerPayloadJSON TypeScript Examples

The following examples show how to use @polkadot/types/types#SignerPayloadJSON. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: methods.ts    From metamask-snap-polkadot with Apache License 2.0 6 votes vote down vote up
async function sign(
  this: MetamaskPolkadotSnap,
  method: "signPayloadJSON" | "signPayloadRaw",
  payload: SignerPayloadJSON | SignerPayloadRaw
): Promise<{signature: string}> {
  return (
    await sendSnapMethod({
      method,
      params: {
        payload
      }
    } as SignPayloadJSONRequest | SignPayloadRawRequest,
    this.snapId
    )
  ) as {signature: string};
}
Example #2
Source File: snap.ts    From metamask-snap-polkadot with Apache License 2.0 6 votes vote down vote up
public signer: InjectedSigner = {
    signPayload: async (payload: SignerPayloadJSON): Promise<SignerResult> => {
      const signature = await signPayloadJSON.bind(this)(payload);
      const id = this.requestCounter;
      this.requestCounter += 1;
      return {id, signature};
    },
    signRaw: async (raw: SignerPayloadRaw): Promise<SignerResult> => {
      const signature = await signPayloadRaw.bind(this)(raw);
      const id = this.requestCounter;
      this.requestCounter += 1;
      return {id, signature};
    },
    update: () => null
  };
Example #3
Source File: sign.ts    From metamask-snap-polkadot with Apache License 2.0 6 votes vote down vote up
export async function signPayloadJSON(
  wallet: Wallet, api: ApiPromise, payload: SignerPayloadJSON
): Promise<{ signature: string }|void> {
  const confirmation = await showConfirmationDialog(
    wallet,
    `Do you want to sign following payload: \n "${payload.method}" \n with account ${payload.address}`
  );
  if (confirmation) {
    const extrinsic = api.registry.createType('ExtrinsicPayload', payload, { version: payload.version });
    const keyPair = await getKeyPair(wallet);
    return extrinsic.sign(keyPair);
  }
}
Example #4
Source File: ApiSigner.ts    From crust-apps with Apache License 2.0 6 votes vote down vote up
public async signPayload (payload: SignerPayloadJSON): Promise<SignerResult> {
    return new Promise((resolve, reject): void => {
      this.#queuePayload(this.#registry, payload, (id: number, result: SignerResult | null): void => {
        if (result) {
          resolve(result);
        } else {
          reject(new Error('Unable to sign'));
        }
      });
    });
  }
Example #5
Source File: QrSigner.ts    From crust-apps with Apache License 2.0 6 votes vote down vote up
public async signPayload (payload: SignerPayloadJSON): Promise<SignerResult> {
    return new Promise((resolve, reject): void => {
      // limit size of the transaction
      const isQrHashed = (payload.method.length > 5000);
      const wrapper = this.#registry.createType('ExtrinsicPayload', payload, { version: payload.version });
      const qrPayload = isQrHashed
        ? blake2AsU8a(wrapper.toU8a(true))
        : wrapper.toU8a();

      this.#setState({
        isQrHashed,
        qrAddress: payload.address,
        qrPayload,
        qrReject: reject,
        qrResolve: resolve
      });
    });
  }
Example #6
Source File: SingleAccountSigner.ts    From interbtc-api with Apache License 2.0 6 votes vote down vote up
public async signPayload (payload: SignerPayloadJSON): Promise<SignerResult> {
      if (payload.address !== this.#keyringPair.address) {
          throw new Error("does not have the keyringPair");
      }

      return new Promise((resolve): void => {
          setTimeout((): void => {
              const signed = this.#registry.createType("ExtrinsicPayload", payload, { version: payload.version }).sign(this.#keyringPair);

              resolve({
                  id: ++id,
                  ...signed
              });
          }, this.#signDelay);
      });
  }
Example #7
Source File: AccountSigner.ts    From subscan-multisig-react with Apache License 2.0 6 votes vote down vote up
public async signPayload(payload: SignerPayloadJSON): Promise<SignerResult> {
    return new Promise((resolve): void => {
      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
      // @ts-ignore
      const signed = this.#registry
        .createType('ExtrinsicPayload', payload, { version: payload.version })
        .sign(this.#keyringPair);

      lockAccount(this.#keyringPair);
      resolve({ id: ++id, ...signed });
    });
  }
Example #8
Source File: ApiSigner.ts    From subscan-multisig-react with Apache License 2.0 6 votes vote down vote up
public async signPayload(payload: SignerPayloadJSON): Promise<SignerResult> {
    return new Promise((resolve, reject): void => {
      this.#queuePayload(this.#registry, payload, (id: number, result: SignerResult | null): void => {
        if (result) {
          resolve(result);
        } else {
          reject(new Error('Unable to sign'));
        }
      });
    });
  }
Example #9
Source File: QrSigner.ts    From subscan-multisig-react with Apache License 2.0 6 votes vote down vote up
public async signPayload(payload: SignerPayloadJSON): Promise<SignerResult> {
    return new Promise((resolve, reject): void => {
      // limit size of the transaction
      // eslint-disable-next-line no-magic-numbers
      const isQrHashed = payload.method.length > 5000;
      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
      // @ts-ignore
      const wrapper = this.#registry.createType('ExtrinsicPayload', payload, { version: payload.version });
      const qrPayload = isQrHashed ? blake2AsU8a(wrapper.toU8a(true)) : wrapper.toU8a();

      this.#setState({
        isQrHashed,
        qrAddress: payload.address,
        qrPayload,
        qrReject: reject,
        qrResolve: resolve,
      });
    });
  }
Example #10
Source File: methods.ts    From metamask-snap-polkadot with Apache License 2.0 5 votes vote down vote up
export async function signPayloadJSON(this: MetamaskPolkadotSnap, payload: SignerPayloadJSON): Promise<string> {
  return (await sign.bind(this)("signPayloadJSON", payload)).signature;
}
Example #11
Source File: AccountSigner.ts    From crust-apps with Apache License 2.0 5 votes vote down vote up
public async signPayload (payload: SignerPayloadJSON): Promise<SignerResult> {
    return new Promise((resolve): void => {
      const signed = this.#registry.createType('ExtrinsicPayload', payload, { version: payload.version }).sign(this.#keyringPair);

      lockAccount(this.#keyringPair);
      resolve({ id: ++id, ...signed });
    });
  }
Example #12
Source File: LedgerSigner.ts    From crust-apps with Apache License 2.0 5 votes vote down vote up
public async signPayload (payload: SignerPayloadJSON): Promise<SignerResult> {
    const raw = this.#registry.createType('ExtrinsicPayload', payload, { version: payload.version });
    const { signature } = await this.#getLedger().sign(raw.toU8a(true), this.#accountOffset, this.#addressOffset);

    return { id: ++id, signature };
  }
Example #13
Source File: LedgerSigner.ts    From subscan-multisig-react with Apache License 2.0 5 votes vote down vote up
public async signPayload(payload: SignerPayloadJSON): Promise<SignerResult> {
    const raw = this.registry.createType('ExtrinsicPayload', payload, { version: payload.version });
    const { signature } = await this.getLedger().sign(raw.toU8a(true), this.accountOffset, this.addressOffset);

    return { id: ++id, signature };
  }
Example #14
Source File: Queue.tsx    From crust-apps with Apache License 2.0 4 votes vote down vote up
function Queue ({ children }: Props): React.ReactElement<Props> {
  const [stqueue, _setStQueue] = useState<QueueStatus[]>([]);
  const [txqueue, _setTxQueue] = useState<QueueTx[]>([]);
  const stRef = useRef(stqueue);
  const txRef = useRef(txqueue);

  const setStQueue = useCallback(
    (st: QueueStatus[]): void => {
      stRef.current = st;
      _setStQueue(st);
    },
    []
  );

  const setTxQueue = useCallback(
    (tx: QueueTx[]): void => {
      txRef.current = tx;
      _setTxQueue(tx);
    },
    []
  );

  const addToTxQueue = useCallback(
    (value: QueueTxExtrinsic | QueueTxRpc | QueueTx): void => {
      const id = ++nextId;
      const removeItem = () => setTxQueue([
        ...txRef.current.map((item): QueueTx =>
          item.id === id
            ? { ...item, status: 'completed' }
            : item
        )
      ]);

      setTxQueue([...txRef.current, {
        ...value,
        id,
        removeItem,
        rpc: (value as QueueTxRpc).rpc || SUBMIT_RPC,
        status: 'queued'
      }]);
    },
    [setTxQueue]
  );

  const queueAction = useCallback(
    (_status: ActionStatus | ActionStatus[]): void => {
      const status = Array.isArray(_status) ? _status : [_status];

      status.length && setStQueue([
        ...stRef.current,
        ...(status.map((item): QueueStatus => {
          const id = ++nextId;
          const removeItem = (): void =>
            setStQueue([...stRef.current.filter((item): boolean => item.id !== id)]);

          setTimeout(removeItem, REMOVE_TIMEOUT);

          return {
            ...item,
            id,
            isCompleted: false,
            removeItem
          };
        }))
      ]);
    },
    [setStQueue]
  );

  const queueExtrinsic = useCallback(
    (value: PartialQueueTxExtrinsic) => addToTxQueue({ ...value }),
    [addToTxQueue]
  );

  const queuePayload = useCallback(
    (registry: Registry, payload: SignerPayloadJSON, signerCb: SignerCallback): void => {
      addToTxQueue({
        accountId: payload.address,
        // this is not great, but the Extrinsic doesn't need a submittable
        extrinsic: registry.createType('Extrinsic',
          { method: registry.createType('Call', payload.method) },
          { version: payload.version }
        ) as unknown as SubmittableExtrinsic,
        payload,
        signerCb
      });
    },
    [addToTxQueue]
  );

  const queueRpc = useCallback(
    (value: PartialQueueTxRpc) => addToTxQueue({ ...value }),
    [addToTxQueue]
  );

  const queueSetTxStatus = useCallback(
    (id: number, status: QueueTxStatus, result?: SubmittableResult, error?: Error): void => {
      setTxQueue([
        ...txRef.current.map((item): QueueTx =>
          item.id === id
            ? {
              ...item,
              error: error === undefined
                ? item.error
                : error,
              result: result === undefined
                ? item.result as SubmittableResult
                : result,
              status: item.status === 'completed'
                ? item.status
                : status
            }
            : item
        )
      ]);

      queueAction(extractEvents(result));

      if (STATUS_COMPLETE.includes(status)) {
        setTimeout((): void => {
          const item = txRef.current.find((item): boolean => item.id === id);

          item && item.removeItem();
        }, REMOVE_TIMEOUT);
      }
    },
    [queueAction, setTxQueue]
  );

  return (
    <QueueProvider value={{
      queueAction,
      queueExtrinsic,
      queuePayload,
      queueRpc,
      queueSetTxStatus,
      stqueue,
      txqueue
    }}>
      {children}
    </QueueProvider>
  );
}
Example #15
Source File: Queue.tsx    From subscan-multisig-react with Apache License 2.0 4 votes vote down vote up
function Queue({ children }: Props): React.ReactElement<Props> {
  const [stqueue, _setStQueue] = useState<QueueStatus[]>([]);
  const [txqueue, _setTxQueue] = useState<QueueTx[]>([]);
  const stRef = useRef(stqueue);
  const txRef = useRef(txqueue);

  const setStQueue = useCallback((st: QueueStatus[]): void => {
    stRef.current = st;
    _setStQueue(st);
  }, []);

  const setTxQueue = useCallback((tx: QueueTx[]): void => {
    txRef.current = tx;
    _setTxQueue(tx);
  }, []);

  const addToTxQueue = useCallback(
    (value: QueueTxExtrinsic | QueueTxRpc | QueueTx): void => {
      const id = ++nextId;
      const removeItem = () =>
        setTxQueue([
          ...txRef.current.map((item): QueueTx => (item.id === id ? { ...item, status: 'completed' } : item)),
        ]);

      setTxQueue([
        ...txRef.current,
        {
          ...value,
          id,
          removeItem,
          rpc: (value as QueueTxRpc).rpc || SUBMIT_RPC,
          status: 'queued',
        },
      ]);
    },
    [setTxQueue]
  );

  const queueAction = useCallback(
    (_status: ActionStatus | ActionStatus[]): void => {
      const status = Array.isArray(_status) ? _status : [_status];

      // eslint-disable-next-line
      status.length &&
        setStQueue([
          ...stRef.current,
          ...status.map((item): QueueStatus => {
            const id = ++nextId;
            const removeItem = (): void => setStQueue([...stRef.current.filter((item): boolean => item.id !== id)]);

            setTimeout(removeItem, REMOVE_TIMEOUT);

            return {
              ...item,
              id,
              isCompleted: false,
              removeItem,
            };
          }),
        ]);
    },
    [setStQueue]
  );

  const queueExtrinsic = useCallback((value: PartialQueueTxExtrinsic) => addToTxQueue({ ...value }), [addToTxQueue]);

  const queuePayload = useCallback(
    (registry: Registry, payload: SignerPayloadJSON, signerCb: SignerCallback): void => {
      addToTxQueue({
        accountId: payload.address,
        // this is not great, but the Extrinsic doesn't need a submittable
        extrinsic: registry.createType(
          'Extrinsic',
          { method: registry.createType('Call', payload.method) },
          { version: payload.version }
        ) as unknown as SubmittableExtrinsic,
        payload,
        signerCb,
      });
    },
    [addToTxQueue]
  );

  const queueRpc = useCallback((value: PartialQueueTxRpc) => addToTxQueue({ ...value }), [addToTxQueue]);

  const queueSetTxStatus = useCallback(
    (id: number, status: QueueTxStatus, result?: SubmittableResult, error?: Error): void => {
      setTxQueue([
        ...txRef.current.map(
          (item): QueueTx =>
            item.id === id
              ? {
                  ...item,
                  error: error === undefined ? item.error : error,
                  result: result === undefined ? (item.result as SubmittableResult) : result,
                  status: item.status === 'completed' ? item.status : status,
                }
              : item
        ),
      ]);

      queueAction(extractEvents(result));

      if (STATUS_COMPLETE.includes(status)) {
        setTimeout((): void => {
          const item = txRef.current.find((item): boolean => item.id === id);

          // eslint-disable-next-line
          item && item.removeItem();
        }, REMOVE_TIMEOUT);
      }
    },
    [queueAction, setTxQueue]
  );

  return (
    <QueueProvider
      value={{
        queueAction,
        queueExtrinsic,
        queuePayload,
        queueRpc,
        queueSetTxStatus,
        stqueue,
        txqueue,
      }}
    >
      {children}
    </QueueProvider>
  );
}