forked from EOSIO/eosjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eosjs-jssig.ts
42 lines (36 loc) · 1.49 KB
/
eosjs-jssig.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* @module JS-Sig
*/
// copyright defined in eosjs/LICENSE.txt
import * as ecc from 'eosjs-ecc';
import { SignatureProvider, SignatureProviderArgs } from './eosjs-api-interfaces';
import { convertLegacyPublicKey } from './eosjs-numeric';
/** Signs transactions using in-process private keys */
export class JsSignatureProvider implements SignatureProvider {
/** map public to private keys */
public keys = new Map<string, string>();
/** public keys */
public availableKeys = [] as string[];
/** @param privateKeys private keys to sign with */
constructor(privateKeys: string[]) {
for (const k of privateKeys) {
const pub = convertLegacyPublicKey(ecc.PrivateKey.fromString(k).toPublic().toString());
this.keys.set(pub, k);
this.availableKeys.push(pub);
}
}
/** Public keys associated with the private keys that the `SignatureProvider` holds */
public async getAvailableKeys() {
return this.availableKeys;
}
/** Sign a transaction */
public async sign({ chainId, requiredKeys, serializedTransaction }: SignatureProviderArgs) {
const signBuf = Buffer.concat([
new Buffer(chainId, 'hex'), new Buffer(serializedTransaction), new Buffer(new Uint8Array(32)),
]);
const signatures = requiredKeys.map(
(pub) => ecc.Signature.sign(signBuf, this.keys.get(convertLegacyPublicKey(pub))).toString(),
);
return { signatures, serializedTransaction };
}
}