Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function foo(): Promise {
// Bob (the payee)
const bobRaw = {
identity: {
type: IdentityTypes.TYPE.ETHEREUM_ADDRESS,
value: '0xAf083f77F1fFd54218d91491AFD06c9296EaC3ce',
},
signatureParams: {
method: SignatureTypes.METHOD.ECDSA,
privateKey: '0x04674d2e53e0e14653487d7323cc5f0a7959c83067f5654cafe4094bde90fa8a',
},
};
// Alice (the payer)
const aliceRaw = {
identity: {
type: IdentityTypes.TYPE.ETHEREUM_ADDRESS,
value: '0x740fc87Bd3f41d07d23A01DEc90623eBC5fed9D6',
},
signatureParams: {
method: SignatureTypes.METHOD.ECDSA,
privateKey: '0x0906ff14227cead2b25811514302d57706e7d5013fcc40eca5985b216baeb998',
},
};
function recover(signedData: SignatureTypes.ISignedData): IdentityTypes.IIdentity {
let value: string;
if (signedData.signature.method === SignatureTypes.METHOD.ECDSA) {
value = Crypto.EcUtils.recover(
signedData.signature.value,
Crypto.normalizeKeccak256Hash(signedData.data).value,
);
return {
type: IdentityTypes.TYPE.ETHEREUM_ADDRESS,
value,
};
}
if (signedData.signature.method === SignatureTypes.METHOD.ECDSA_ETHEREUM) {
// In ethereum V = 0x1B or 0x1C instead of 0x00 or 0x01
// We make the replacement here
const v = signedData.signature.value.slice(V_POSITION_FROM_END_IN_ECDSA_HEX);
let signature = signedData.signature.value;
expectedAmount: '170000000000',
payee: {
type: IdentityTypes.TYPE.ETHEREUM_ADDRESS,
value: '0xAf083f77F1fFd54218d91491AFD06c9296EaC3ce',
},
payer: {
type: IdentityTypes.TYPE.ETHEREUM_ADDRESS,
value: '0x627306090abab3a6e1400e9345bc60c78a8bef57',
},
timestamp: 1544426030,
};
// A signature provider, for example @requestnetwork/epk-signature
const signatureProvider: SignatureProviderTypes.ISignatureProvider = new EthereumPrivateKeySignatureProvider(
{
method: SignatureTypes.METHOD.ECDSA,
privateKey: '0xc87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3',
},
);
/* tslint:disable:no-console */
(async (): Promise => {
// Data access setup
const dataAccess = new DataAccess(new MockStorage());
await dataAccess.initialize();
// A transaction manager, for example @requestnetwork/transaction-manager
const transactionManager: TransactionTypes.ITransactionManager = new TransactionManager(
dataAccess,
);
const requestLogic = new RequestLogic(transactionManager, signatureProvider);
import { IdentityTypes, SignatureProviderTypes, SignatureTypes } from '@requestnetwork/types';
import Utils from '@requestnetwork/utils';
/** Type of the dictionary of signatureParameters (private keys) indexed by ethereum address */
type ISignatureParametersDictionary = Map;
/**
* Implementation of the signature provider from private key
* Allows to sign() with "Ethereum_address" identities thanks to their private key given in constructor() or addSignatureParameters()
*/
export default class EthereumPrivateKeySignatureProvider
implements SignatureProviderTypes.ISignatureProvider {
/** list of supported signing method */
public supportedMethods: SignatureTypes.METHOD[] = [SignatureTypes.METHOD.ECDSA];
/** list of supported identity types */
public supportedIdentityTypes: IdentityTypes.TYPE[] = [IdentityTypes.TYPE.ETHEREUM_ADDRESS];
/** Dictionary containing all the private key indexed by address */
private signatureParametersDictionary: ISignatureParametersDictionary;
constructor(signatureParameter?: SignatureTypes.ISignatureParameters) {
// using Map allow to easily remove dynamically entries
this.signatureParametersDictionary = new Map();
if (signatureParameter) {
this.addSignatureParameters(signatureParameter);
}
}
/**
* Signs data
};
const payeeDecryptionParameters: EncryptionTypes.IDecryptionParameters = {
key: '0x0906ff14227cead2b25811514302d57706e7d5013fcc40eca5985b216baeb998',
method: EncryptionTypes.METHOD.ECIES,
};
const payerEncryptionParameters: EncryptionTypes.IEncryptionParameters = {
key:
'cf4a1d0bbef8bf0e3fa479a9def565af1b22ea6266294061bfb430701b54a83699e3d47bf52e9f0224dcc29a02721810f1f624f1f70ea3cc5f1fb752cfed379d',
method: EncryptionTypes.METHOD.ECIES,
};
// A signature provider, for example @requestnetwork/epk-signature
const signatureProvider: SignatureProviderTypes.ISignatureProvider = new EthereumPrivateKeySignatureProvider(
{
method: SignatureTypes.METHOD.ECDSA,
privateKey: '0xc87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3',
},
);
// A decryption provider, for example @requestnetwork/epk-decryption
const decryptionProvider: DecryptionProviderTypes.IDecryptionProvider = new EthereumPrivateKeyDecryptionProvider(
payeeDecryptionParameters,
);
/* tslint:disable:no-console */
(async (): Promise => {
// Data access setup
const dataAccess = new DataAccess(new MockStorage());
await dataAccess.initialize();
// A transaction manager, for example @requestnetwork/transaction-manager
function recover(signedData: SignatureTypes.ISignedData): IdentityTypes.IIdentity {
let value: string;
if (signedData.signature.method === SignatureTypes.METHOD.ECDSA) {
value = Crypto.EcUtils.recover(
signedData.signature.value,
Crypto.normalizeKeccak256Hash(signedData.data).value,
);
return {
type: IdentityTypes.TYPE.ETHEREUM_ADDRESS,
value,
};
}
if (signedData.signature.method === SignatureTypes.METHOD.ECDSA_ETHEREUM) {
// In ethereum V = 0x1B or 0x1C instead of 0x00 or 0x01
// We make the replacement here
const v = signedData.signature.value.slice(V_POSITION_FROM_END_IN_ECDSA_HEX);
let signature = signedData.signature.value;
if (v.toLowerCase() === '00') {
signature = `${signedData.signature.value.slice(0, V_POSITION_FROM_END_IN_ECDSA_HEX)}1b`;
} else if (v.toLowerCase() === '01') {
signature = `${signedData.signature.value.slice(0, V_POSITION_FROM_END_IN_ECDSA_HEX)}1b`;
}
const normalizedData = Crypto.normalize(signedData.data);
value = Crypto.EcUtils.recover(
signature,
Crypto.keccak256Hash(
// add the ethereum padding (only for ECDSA_ETHEREUM)
// This is to make the system compatible with the ethereum signatures
public async sign(
data: any,
signer: IdentityTypes.IIdentity,
): Promise {
if (!this.supportedIdentityTypes.includes(signer.type)) {
throw Error(`Identity type not supported ${signer.type}`);
}
const normalizedData = Utils.crypto.normalize(data);
const signatureValue = await this.eth.personal.sign(normalizedData, signer.value);
return {
data,
signature: {
method: SignatureTypes.METHOD.ECDSA_ETHEREUM,
value: signatureValue,
},
};
}
}
import { AdvancedLogic } from '@requestnetwork/advanced-logic';
import { DataAccess } from '@requestnetwork/data-access';
import { EthereumPrivateKeySignatureProvider } from '@requestnetwork/epk-signature';
import { RequestLogic } from '@requestnetwork/request-logic';
import { TransactionManager } from '@requestnetwork/transaction-manager';
import { IdentityTypes, RequestLogicTypes, SignatureTypes } from '@requestnetwork/types';
import MockStorage from './mock-storage';
const signatureInfo: SignatureTypes.ISignatureParameters = {
method: SignatureTypes.METHOD.ECDSA,
privateKey: '0xc87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3',
};
const signerIdentity: IdentityTypes.IIdentity = {
type: IdentityTypes.TYPE.ETHEREUM_ADDRESS,
value: '0x627306090abab3a6e1400e9345bc60c78a8bef57',
};
const requestCreationHash: RequestLogicTypes.ICreateParameters = {
currency: {
network: 'mainnet',
type: RequestLogicTypes.CURRENCY.ETH,
value: 'ETH',
},
expectedAmount: '100000000000',
payee: signerIdentity,
payer: {