ghost-portal/packages/react-signer/src/signers/LedgerSigner.ts
2022-01-01 09:30:15 +02:00

30 lines
1.1 KiB
TypeScript

// Copyright 2017-2022 @polkadot/react-signer authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Signer, SignerResult } from '@polkadot/api/types';
import type { Ledger } from '@polkadot/hw-ledger';
import type { Registry, SignerPayloadJSON } from '@polkadot/types/types';
let id = 0;
export default class LedgerSigner implements Signer {
readonly #accountOffset: number;
readonly #addressOffset: number;
readonly #getLedger: () => Ledger;
readonly #registry: Registry;
constructor (registry: Registry, getLedger: () => Ledger, accountOffset: number, addressOffset: number) {
this.#accountOffset = accountOffset;
this.#addressOffset = addressOffset;
this.#getLedger = getLedger;
this.#registry = registry;
}
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 };
}
}