Skip to content

Commit

Permalink
Merge pull request #297 from 0xs34n/ivpavici_docs_refactor
Browse files Browse the repository at this point in the history
Added elliptic functions to utils docs
  • Loading branch information
ivpavici authored Aug 18, 2022
2 parents efee0ac + b940382 commit a67af4a
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 76 deletions.
36 changes: 20 additions & 16 deletions src/utils/ellipticCurve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ export const ec = new EC(
})
);

/*
The function _truncateToN in lib/elliptic/ec/index.js does a shift-right of 4 bits
in some cases. This function does the opposite operation so that
_truncateToN(fixMessage(msg)) == msg.
*/
/**
* The function _truncateToN in lib/elliptic/ec/index.js does a shift-right of 4 bits
* in some cases. This function does the opposite operation so that
* _truncateToN(fixMessage(msg)) == msg.
*
* @param msg
*/
function fixMessage(msg: string) {
const pureHex = msg.replace(/^0x0*/, '');

Expand Down Expand Up @@ -64,11 +66,12 @@ export function getKeyPairFromPublicKey(publicKey: BigNumberish): KeyPair {
return ec.keyFromPublic(removeHexPrefix(toHex(publicKeyBn)), 'hex');
}

/*
Signs a message using the provided key.
key should be an KeyPair with a valid private key.
Returns an Signature.
*/
/**
* Signs a message using the provided key.
*
* @param keyPair should be an KeyPair with a valid private key.
* @returns an Signature.
*/
export function sign(keyPair: KeyPair, msgHash: string): Signature {
const msgHashBN = toBN(addHexPrefix(msgHash));
// Verify message hash has valid length.
Expand All @@ -89,12 +92,13 @@ function chunkArray(arr: any[], n: number): any[][] {
.map((_, i) => arr.slice(i * n, i * n + n));
}

/*
Verifies a message using the provided key.
key should be an KeyPair with a valid public key.
msgSignature should be an Signature.
Returns a boolean true if the verification succeeds.
*/
/**
* Verifies a message using the provided key.
*
* @param keyPair should be an KeyPair with a valid public key.
* @param sig should be an Signature.
* @returns true if the verification succeeds.
*/
export function verify(keyPair: KeyPair | KeyPair[], msgHash: string, sig: Signature): boolean {
const keyPairArray = Array.isArray(keyPair) ? keyPair : [keyPair];
const msgHashBN = toBN(addHexPrefix(msgHash));
Expand Down
6 changes: 3 additions & 3 deletions www/docs/API/account.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ The _options_ object may include any of:

###### _EstimateFeeResponse_

```ts
```typescript
{
overall_fee: BN;
gas_consumed?: BN;
Expand All @@ -63,7 +63,7 @@ The _transactionsDetail_ object may include any of:

###### _AddTransactionResponse_

```ts
```typescript
{
transaction_hash: string;
};
Expand All @@ -77,7 +77,7 @@ Creates a signature from the passed data.

###### _Signature_

```ts
```typescript
string[];
```

Expand Down
2 changes: 1 addition & 1 deletion www/docs/API/provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ Gets the transaction trace from a tx hash.
events: Array<any>;
messages: Array<any>;
};
signature: Signature;
signature: Signature;
}
```

Expand Down
Loading

0 comments on commit a67af4a

Please sign in to comment.