ghost-portal/packages/react-signer/src/signers/LedgerSigner.ts
2021-01-03 11:33:39 +01:00

29 lines
1005 B
TypeScript

// Copyright 2017-2021 @polkadot/react-signer authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Signer, SignerResult } from '@polkadot/api/types';
import type { Registry, SignerPayloadJSON } from '@polkadot/types/types';
import { getLedger } from '@polkadot/react-api';
let id = 0;
export default class LedgerSigner implements Signer {
readonly #accountOffset: number;
readonly #addressOffset: number;
readonly #registry: Registry;
constructor (registry: Registry, accountOffset: number, addressOffset: number) {
this.#accountOffset = accountOffset;
this.#addressOffset = addressOffset;
this.#registry = registry;
}
public async signPayload (payload: SignerPayloadJSON): Promise<SignerResult> {
const raw = this.#registry.createType('ExtrinsicPayload', payload, { version: payload.version });
const { signature } = await getLedger().sign(raw.toU8a(true), this.#accountOffset, this.#addressOffset);
return { id: ++id, signature };
}
}