mirror of
https://github.com/pezkuwichain/pezkuwi-api.git
synced 2026-11-01 06:39:48 +00:00
Rebrand: polkadot → pezkuwi, substrate → bizinikiwi, kusama → dicle
This commit is contained in:
commit
d5f038faea
1383 files changed
+1088018
No files matched your search
@@ -0,0 +1,53 @@
|
||||
// Copyright 2017-2025 @pezkuwi/api authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { KeyringPair } from '@pezkuwi/keyring/types';
|
||||
import type { Registry, SignerPayloadJSON, SignerPayloadRaw } from '@pezkuwi/types/types';
|
||||
import type { Signer, SignerResult } from '../types/index.js';
|
||||
|
||||
import { hexToU8a, objectSpread, u8aToHex } from '@pezkuwi/util';
|
||||
|
||||
let id = 0;
|
||||
|
||||
export class SingleAccountSigner implements Signer {
|
||||
readonly #keyringPair: KeyringPair;
|
||||
readonly #registry: Registry;
|
||||
readonly #signDelay: number;
|
||||
|
||||
constructor (registry: Registry, keyringPair: KeyringPair, signDelay = 0) {
|
||||
this.#keyringPair = keyringPair;
|
||||
this.#registry = registry;
|
||||
this.#signDelay = signDelay;
|
||||
}
|
||||
|
||||
public async signPayload (payload: SignerPayloadJSON): Promise<SignerResult> {
|
||||
if (payload.address !== this.#keyringPair.address) {
|
||||
throw new Error('Signer 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(objectSpread({ id: ++id }, signed));
|
||||
}, this.#signDelay);
|
||||
});
|
||||
}
|
||||
|
||||
public async signRaw ({ address, data }: SignerPayloadRaw): Promise<SignerResult> {
|
||||
if (address !== this.#keyringPair.address) {
|
||||
throw new Error('Signer does not have the keyringPair');
|
||||
}
|
||||
|
||||
return new Promise((resolve): void => {
|
||||
setTimeout((): void => {
|
||||
const signature = u8aToHex(this.#keyringPair.sign(hexToU8a(data)));
|
||||
|
||||
resolve({
|
||||
id: ++id,
|
||||
signature
|
||||
});
|
||||
}, this.#signDelay);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user