diff --git a/src/utils/ellipticCurve.ts b/src/utils/ellipticCurve.ts index 7d71ab2cc..488de5a9e 100644 --- a/src/utils/ellipticCurve.ts +++ b/src/utils/ellipticCurve.ts @@ -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*/, ''); @@ -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. @@ -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)); diff --git a/www/docs/API/account.md b/www/docs/API/account.md index 8b46389db..b9b085b47 100644 --- a/www/docs/API/account.md +++ b/www/docs/API/account.md @@ -41,7 +41,7 @@ The _options_ object may include any of: ###### _EstimateFeeResponse_ -```ts +```typescript { overall_fee: BN; gas_consumed?: BN; @@ -63,7 +63,7 @@ The _transactionsDetail_ object may include any of: ###### _AddTransactionResponse_ -```ts +```typescript { transaction_hash: string; }; @@ -77,7 +77,7 @@ Creates a signature from the passed data. ###### _Signature_ -```ts +```typescript string[]; ``` diff --git a/www/docs/API/provider.md b/www/docs/API/provider.md index dd130810d..6df043a76 100644 --- a/www/docs/API/provider.md +++ b/www/docs/API/provider.md @@ -285,7 +285,7 @@ Gets the transaction trace from a tx hash. events: Array; messages: Array; }; - signature: Signature; + signature: Signature; } ``` diff --git a/www/docs/API/utils.md b/www/docs/API/utils.md index de2289966..99a89d02d 100644 --- a/www/docs/API/utils.md +++ b/www/docs/API/utils.md @@ -4,18 +4,16 @@ sidebar_position: 6 # Utils -Util functions are provided so you can use low level functions in your application. +Util functions are provided so you can use various low level functions in your application. ## **address** -the address helpers can be imported using: +The address helpers can be imported using: ```js import { address } from 'starknet.js'; ``` -
- ### getChecksumAddress `getChecksumAddress(address: BigNumberish): string` @@ -33,8 +31,6 @@ const checksummedAddress = address.getChecksumAddress(addressToCheck); console.log(checksummedAddress); // 0x02FD23D9182193775423497Fc0c472E156C57C69E4089a1967fb288a2D84e914 ``` -
- ### validateChecksumAddress `validateChecksumAddress(address: string): boolean` @@ -45,28 +41,22 @@ Returns true if the address is valid, false otherwise.
-## stark +## **stark** Functions for stark specific manipulations. -
- ### compressProgram `compressProgram(jsonProgram: Program | string): CompressedProgram` Function to compress compiled cairo program. Accepts a json file representing the compiled cairo program and returns a compressed cairo program. -
- ### randomAddress `randomAddress(): string` Function that generates a random contract address. -
- ### makeAddress `makeAddress(input: string): string` @@ -77,8 +67,6 @@ Returns a string. Example: `0xdFD0F27FCe99b50909de0bDD328Aed6eAbe76BC5` -> `0xdfd0f27fce99b50909de0bdd328aed6eabe76bc5` -
- ### formatSignature `formatSignature(sig?: Signature): string[]` @@ -87,8 +75,6 @@ Function that formats a Signature to BigNum and then to string array. Returns a string array. -
- ### compileCalldata `compileCalldata(args: RawArgs): Calldata` @@ -106,8 +92,6 @@ await this.callContract({ }); ``` -
- ### estimatedFeeToMaxFee `estimatedFeeToMaxFee(estimatedFee: BigNumberish, overhead: number = 0.5): BN` @@ -128,16 +112,12 @@ Various number formatting functions. export type BigNumberish = string | number | BN; ``` -
- ### isHex `isHex(hex: string): boolean` Check if number is in hex format. -
- ### toBN `toBN(number: BigNumberish, base?: number | 'hex'): BN` @@ -146,8 +126,6 @@ Converts BigNumberish to BN. Returns a BN. -
- ### toHex `toHex(number: BN): string` @@ -156,16 +134,12 @@ Converts BN to hex. Returns a string. -
- ### hexToDecimalString `hexToDecimalString(hex: string): string` Converts hex string to decimal string. -
- ### toFelt `toFelt(num: BigNumberish): string` @@ -174,8 +148,6 @@ Converts BN to Felt. Returns a string. -
- ### assertInRange `assertInRange(input: BigNumberish, lowerBound: BigNumberish, upperBound: BigNumberish, inputName = '')` @@ -184,8 +156,6 @@ Asserts input is equal to or greater then `lowerBound` and lower then `upperBoun `input`, `lowerBound`, and `upperBound` should be of type BN. `inputName` should be a string. -
- ### bigNumberishArrayToDecimalStringArray `bigNumberishArrayToDecimalStringArray(rawCalldata: BigNumberish[]): string[]` @@ -204,8 +174,6 @@ const signature = await this.signer.signTransaction(transactions, signerDetails) } ``` -
- ## **uint256** ```js @@ -218,24 +186,18 @@ export interface Uint256 { } ``` -
- ### uint256ToBN `uint256ToBN(uint256: Uint256): BN` Function to convert `Uint256` to `BN` (big number), which uses the `bn.js` library. -
- ### isUint256 `isUint256(bn: BigNumberish): boolean` Function to check if `BN` is smaller or equal to `2**256-1`. -
- ### bnToUint256 `bnToUint256(bignumber: BigNumberish): Uint256` @@ -253,12 +215,9 @@ Various hashing helpers. `starknetKeccak(value: string): BN` Function to get the starknet keccak hash from a string. Returns starknet keccak hash as BigNumber. -nction to get the starknet keccak hash from a string. Returns starknet keccak hash as BigNumber. -
- ### getSelectorFromName `getSelectorFromName(funcName: string)` @@ -267,8 +226,6 @@ Function to get the hex selector from a given function name. Returns hex selector of given abi function name. -
- ### pedersen `pedersen(input: [BigNumberish, BigNumberish])` @@ -277,8 +234,6 @@ Function to get the Pedersen hash for two arguments. Returns a string. -
- ### computeHashOnElements `computeHashOnElements(data: BigNumberish[])` @@ -287,8 +242,6 @@ Function to compute a Pedersen hash on a array of elements. Returns a string. -
- ### calculateTransactionHashCommon `calculateTransactionHashCommon(txHashPrefix: TransactionHashPrefix, version: BigNumberish,contractAddress: BigNumberish, entryPointSelector: BigNumberish, calldata: BigNumberish[], maxFee: BigNumberish, chainId: StarknetChainId, additionalData: BigNumberish[] = []): string` @@ -297,8 +250,6 @@ Calculates the transaction hash in the StarkNet network - a unique identifier of Called internally in `calculateDeployTransactionHash` and `calculcateTransactionHash`. -
- ### calculateDeployTransactionHash `calculateDeployTransactionHash(contractAddress: BigNumberish, constructorCalldata: BigNumberish[], version: BigNumberish, chainId: StarknetChainId): string` @@ -307,8 +258,6 @@ Function that calculates the deployment transaction hash in the StarkNet network Internally calls `calculateTransactionHashCommon` with `TransactionHashPrefix.DEPLOY`. -
- ### calculcateTransactionHash `calculcateTransactionHash(contractAddress: BigNumberish, version: BigNumberish, entryPointSelector: BigNumberish, calldata: BigNumberish[], maxFee: BigNumberish, chainId: StarknetChainId): string` @@ -326,8 +275,6 @@ const hashMsg = calculcateTransactionHash( ); ``` -
- ### calculateContractAddressFromHash `calculateContractAddressFromHash(salt: BigNumberish, classHash: BigNumberish, constructorCalldata: RawCalldata, deployerAddress: BigNumberish)` @@ -335,3 +282,57 @@ const hashMsg = calculcateTransactionHash( Function that calculates contract address from hash. Returns a string. + +
+ +## **ellipticCurve** + +Wrapper around the javascript `elliptic` library with additional functionality. + +### genKeyPair + +`ec.genKeyPair()` + +Generate a random key pair. + +### getKeyPair + +`getKeyPair(pk: BigNumberish): KeyPair` + +Get a key pair from a private key. + +### getStarkKey + +`getStarkKey(keyPair: KeyPair): string` + +Public key defined over a Stark-friendly elliptic curve that is different from the standard Ethereum elliptic curve + +### getKeyPairFromPublicKey + +`getKeyPairFromPublicKey(publicKey: BigNumberish): KeyPair` + +Takes a public key and casts it into `elliptic` KeyPair format. + +Returns keyPair with public key only, which can be used to verify signatures, but cant sign anything. + +### sign + +`sign(keyPair: KeyPair, msgHash: string): Signature` + +Signs a message using the provided key. + +keyPair should be an KeyPair with a valid private key. + +Returns an Signature. + +### verify + +`verify(keyPair: KeyPair | KeyPair[], msgHash: string, sig: Signature): boolean` + +Verifies a message using the provided key. + +keyPair should be an KeyPair with a valid public key. + +sig should be an Signature. + +Returns true if the verification succeeds. \ No newline at end of file