import { IdentityTypes, SignatureProviderTypes, SignatureTypes } from '@requestnetwork/types';
import Utils from '@requestnetwork/utils';
import mySignaturePackage from 'mySignaturePackage';
type IWalletIdDictionary = Map<string, number>;
export default class MySignatureProvider
implements SignatureProviderTypes.ISignatureProvider {
public supportedMethods: SignatureTypes.METHOD[] = [SignatureTypes.METHOD.ECDSA];
public supportedIdentityTypes: IdentityTypes.TYPE[] = [IdentityTypes.TYPE.ETHEREUM_ADDRESS];
private walletIdDictionary: IWalletIdDictionary;
constructor(identity?: IdentityTypes.IIdentity?, walletId?: number) {
this.walletIdDictionary = new Map<string, number>();
if (identity && walletId) {
this.addSignatureParameters(identity, walletId);
}
}
public async sign(
data: any,
signer: IdentityTypes.IIdentity,
): Promise<SignatureTypes.ISignedData> {
if (!this.supportedIdentityTypes.includes(signer.type)) {
throw Error(`Identity type not supported ${signer.type}`);
}
const walletId: number | undefined = this.walletIdDictionary.get(signer.value.toLowerCase());
if (!walletId) {
throw Error(`Identity unknown: ${signer.type}, ${signer.value}`);
}
const hashData = Utils.crypto.normalizeKeccak256Hash(data).value;
const hashDataBuffer = Buffer.from(hashData.slice(2), 'hex')
const signatureValueBuffer = mySignaturePackage.sign(hashDataBuffer, walletId);
const signatureValue = `0x${signatureValueBuffer.toString('hex')}`
return {
data,
signature: {
method: SignatureTypes.METHOD.ECDSA,
value: signatureValue,
},
};
}
public addIdentity(identity: IdentityTypes.IIdentity, walletId: number): void {
if (!this.supportedIdentityTypes.includes(identity.type)) {
throw Error(`Identity type not supported ${identity.type}`);
}
this.walletIdDictionary.set(identity.value.toLowerCase(), walletId);
}
}