Socket
Socket
Sign inDemoInstall

starknet

Package Overview
Dependencies
Maintainers
1
Versions
220
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

starknet - npm Package Compare versions

Comparing version 1.1.1 to 1.2.0

__mocks__/ERC20.json

37

__tests__/index.test.ts
import fs from 'fs';
import starknet, {
import {
CompiledContract,
compressProgram,
randomAddress,
makeAddress,
JsonParser,
} from '..';
getContractAddresses,
getBlock,
getCode,
getStorageAt,
getTransactionStatus,
getTransaction,
addTransaction,
deployContract,
} from '../src';

@@ -17,11 +24,11 @@ const compiledArgentAccount = JsonParser.parse(

test('getContractAddresses()', () => {
return expect(starknet.getContractAddresses()).resolves.not.toThrow();
return expect(getContractAddresses()).resolves.not.toThrow();
});
xtest('callContract()', () => {});
test('getBlock()', () => {
return expect(starknet.getBlock(46500)).resolves.not.toThrow();
return expect(getBlock(46500)).resolves.not.toThrow();
});
test('getCode()', () => {
return expect(
starknet.getCode('0x5f778a983bf8760ad37868f4c869d70247c5546044a7f0386df96d8043d4e9d', 46500)
getCode('0x5f778a983bf8760ad37868f4c869d70247c5546044a7f0386df96d8043d4e9d', 46500)
).resolves.not.toThrow();

@@ -31,14 +38,10 @@ });

return expect(
starknet.getStorageAt(
'0x5f778a983bf8760ad37868f4c869d70247c5546044a7f0386df96d8043d4e9d',
0,
46500
)
getStorageAt('0x5f778a983bf8760ad37868f4c869d70247c5546044a7f0386df96d8043d4e9d', 0, 46500)
).resolves.not.toThrow();
});
test('getTransactionStatus()', () => {
return expect(starknet.getTransactionStatus(286136)).resolves.not.toThrow();
return expect(getTransactionStatus(286136)).resolves.not.toThrow();
});
test('getTransaction()', () => {
return expect(starknet.getTransaction(286136)).resolves.not.toThrow();
return expect(getTransaction(286136)).resolves.not.toThrow();
});

@@ -56,3 +59,3 @@ });

const response = await starknet.addTransaction({
const response = await addTransaction({
type: 'DEPLOY',

@@ -69,3 +72,2 @@ contract_address: randomAddress(),

});
xtest('type: "INVOKE_FUNCTION"', () => {});

@@ -75,6 +77,3 @@ test('deployContract()', async () => {

const response = await starknet.deployContract(
inputContract,
makeAddress('0x20b5B1b8aFd65F1FCB755a449000cFC4aBCA0D40')
);
const response = await deployContract(inputContract);
expect(response.code).toBe('TRANSACTION_RECEIVED');

@@ -81,0 +80,0 @@ expect(response.tx_id).toBeGreaterThan(0);

@@ -6,3 +6,3 @@ /**

import fs from 'fs';
import { compressProgram, isBrowser, JsonParser } from '..';
import { compressProgram, isBrowser, JsonParser } from '../src';

@@ -9,0 +9,0 @@ const compiledArgentAccount = JsonParser.parse(

import fs from 'fs';
import { compressProgram, makeAddress, isBrowser, JsonParser } from '..';
import { compressProgram, makeAddress, isBrowser, JsonParser, getSelectorFromName } from '../src';

@@ -36,1 +36,18 @@ const compiledArgentAccount = JsonParser.parse(

});
describe('starknetKeccak()', () => {
test('hash works for value="test"', () => {
expect(getSelectorFromName('test')).toBe(
'0x22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658'
);
});
test('hash works for value="initialize"', () => {
expect(getSelectorFromName('initialize')).toBe(
'0x79dc0da7c54b95f10aa182ad0a46400db63156920adb65eca2654c0945a463'
);
});
test('hash works for value="mint"', () => {
expect(getSelectorFromName('mint')).toBe(
'0x02f0b3c5710379609eb5495f1ecd348cb28167711b73609fe565a72734550354'
);
});
});

@@ -0,1 +1,15 @@

# [1.2.0](https://github.com/seanjameshan/starknet.js/compare/v1.1.1...v1.2.0) (2021-10-27)
### Bug Fixes
- commitlint for release ([e8ad972](https://github.com/seanjameshan/starknet.js/commit/e8ad9721a9cf86365bf6e44d314c5f06a5589ff0))
- **docs:** new docs page ([2e46cff](https://github.com/seanjameshan/starknet.js/commit/2e46cffff59c8f4db3967896258d737a4cc1bcf8))
- remove useless comment ([ebf07b4](https://github.com/seanjameshan/starknet.js/commit/ebf07b46b6745ffa224caf33a66fc5958745564b))
### Features
- implement needed helper methods ([46f7173](https://github.com/seanjameshan/starknet.js/commit/46f7173f72a213bace2f7dc021172548f5ae8295))
- introduce contract class ([db322fd](https://github.com/seanjameshan/starknet.js/commit/db322fda5a9828a9a4577fd2d712217622717e14))
- type and use callContract ([10c7fc4](https://github.com/seanjameshan/starknet.js/commit/10c7fc4a46f94dc51cecee66e36f1979019fe994)), closes [#6](https://github.com/seanjameshan/starknet.js/issues/6)
## [1.1.1](https://github.com/seanjameshan/starknet.js/compare/v1.1.0...v1.1.1) (2021-10-24)

@@ -2,0 +16,0 @@

@@ -1,99 +0,4 @@

import { compressProgram } from './utils';
import type { GetBlockResponse, GetCode, GetContractAddressesResponse, GetTransactionResponse, GetTransactionStatusResponse, Transaction, AddTransactionResponse, CompiledContract } from './types';
/**
* Gets the smart contract address on the goerli testnet.
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L13-L15)
* @returns starknet smart contract addresses
*/
export declare function getContractAddresses(): Promise<GetContractAddressesResponse>;
/**
* Calls a function on the StarkNet contract.
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L17-L25)
*
* @param invokeTx - transaction to be invoked (WIP)
* @param blockId
* @returns the result of the function on the smart contract.
*/
export declare function callContract(invokeTx: object, blockId: number): Promise<object>;
/**
* Gets the block information from a block ID.
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L27-L31)
*
* @param blockId
* @returns the block object { block_id, previous_block_id, state_root, status, timestamp, transaction_receipts, transactions }
*/
export declare function getBlock(blockId: number): Promise<GetBlockResponse>;
/**
* Gets the code of the deployed contract.
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L33-L36)
*
* @param contractAddress
* @param blockId
* @returns Bytecode and ABI of compiled contract
*/
export declare function getCode(contractAddress: string, blockId: number): Promise<GetCode>;
/**
* Gets the contract's storage variable at a specific key.
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L38-L46)
*
* @param contractAddress
* @param key - from getStorageVarAddress('<STORAGE_VARIABLE_NAME>') (WIP)
* @param blockId
* @returns the value of the storage variable
*/
export declare function getStorageAt(contractAddress: string, key: number, blockId: number): Promise<object>;
/**
* Gets the status of a transaction.
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L48-L52)
*
* @param txId
* @returns the transaction status object { block_id, tx_status: NOT_RECEIVED | RECEIVED | PENDING | REJECTED | ACCEPTED_ONCHAIN }
*/
export declare function getTransactionStatus(txId: number): Promise<GetTransactionStatusResponse>;
/**
* Gets the transaction information from a tx id.
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L54-L58)
*
* @param txId
* @returns the transacton object { transaction_id, status, transaction, block_id?, block_number?, transaction_index?, transaction_failure_reason? }
*/
export declare function getTransaction(txId: number): Promise<GetTransactionResponse>;
/**
* Invoke a function on the starknet contract
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17)
*
* @param tx - transaction to be invoked (WIP)
* @returns a confirmation of invoking a function on the starknet contract
*/
export declare function addTransaction(tx: Transaction): Promise<AddTransactionResponse>;
/**
* Deploys a given compiled contract (json) to starknet
*
* @param contract - a json object containing the compiled contract
* @param address - (optional, defaults to a random address) the address where the contract should be deployed (alpha)
* @returns a confirmation of sending a transaction on the starknet contract
*/
export declare function deployContract(contract: CompiledContract | string, address?: string): Promise<AddTransactionResponse>;
export * from './utils';
export * from './types';
declare const _default: {
getContractAddresses: typeof getContractAddresses;
callContract: typeof callContract;
getBlock: typeof getBlock;
getCode: typeof getCode;
getStorageAt: typeof getStorageAt;
getTransactionStatus: typeof getTransactionStatus;
getTransaction: typeof getTransaction;
addTransaction: typeof addTransaction;
compressProgram: typeof compressProgram;
deployContract: typeof deployContract;
};
export default _default;
export * from './contract';
export * from './starknet';
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {

@@ -23,199 +12,6 @@ if (k2 === undefined) k2 = k;

};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.deployContract = exports.addTransaction = exports.getTransaction = exports.getTransactionStatus = exports.getStorageAt = exports.getCode = exports.getBlock = exports.callContract = exports.getContractAddresses = void 0;
var axios_1 = __importDefault(require("axios"));
var utils_1 = require("./utils");
var API_URL = 'https://alpha2.starknet.io';
var FEEDER_GATEWAY_URL = API_URL + "/feeder_gateway";
var GATEWAY_URL = API_URL + "/gateway";
/**
* Gets the smart contract address on the goerli testnet.
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L13-L15)
* @returns starknet smart contract addresses
*/
function getContractAddresses() {
return new Promise(function (resolve, reject) {
axios_1.default
.get(FEEDER_GATEWAY_URL + "/get_contract_addresses")
.then(function (resp) {
resolve(resp.data);
})
.catch(reject);
});
}
exports.getContractAddresses = getContractAddresses;
// TODO: add proper type
/**
* Calls a function on the StarkNet contract.
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L17-L25)
*
* @param invokeTx - transaction to be invoked (WIP)
* @param blockId
* @returns the result of the function on the smart contract.
*/
function callContract(invokeTx, blockId) {
return new Promise(function (resolve, reject) {
axios_1.default
.post(FEEDER_GATEWAY_URL + "/call_contract?blockId=" + blockId, invokeTx)
.then(function (resp) {
resolve(resp.data);
})
.catch(reject);
});
}
exports.callContract = callContract;
/**
* Gets the block information from a block ID.
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L27-L31)
*
* @param blockId
* @returns the block object { block_id, previous_block_id, state_root, status, timestamp, transaction_receipts, transactions }
*/
function getBlock(blockId) {
return new Promise(function (resolve, reject) {
axios_1.default
.get(FEEDER_GATEWAY_URL + "/get_block?blockId=" + blockId)
.then(function (resp) {
resolve(resp.data);
})
.catch(reject);
});
}
exports.getBlock = getBlock;
/**
* Gets the code of the deployed contract.
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L33-L36)
*
* @param contractAddress
* @param blockId
* @returns Bytecode and ABI of compiled contract
*/
function getCode(contractAddress, blockId) {
return new Promise(function (resolve, reject) {
axios_1.default
.get(FEEDER_GATEWAY_URL + "/get_code?contractAddress=" + contractAddress + "&blockId=" + blockId)
.then(function (resp) {
resolve(resp.data);
})
.catch(reject);
});
}
exports.getCode = getCode;
// TODO: add proper type
/**
* Gets the contract's storage variable at a specific key.
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L38-L46)
*
* @param contractAddress
* @param key - from getStorageVarAddress('<STORAGE_VARIABLE_NAME>') (WIP)
* @param blockId
* @returns the value of the storage variable
*/
function getStorageAt(contractAddress, key, blockId) {
return new Promise(function (resolve, reject) {
axios_1.default
.get(FEEDER_GATEWAY_URL + "/get_storage_at?contractAddress=" + contractAddress + "&key=" + key + "&blockId=" + blockId)
.then(function (resp) {
resolve(resp.data);
})
.catch(reject);
});
}
exports.getStorageAt = getStorageAt;
/**
* Gets the status of a transaction.
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L48-L52)
*
* @param txId
* @returns the transaction status object { block_id, tx_status: NOT_RECEIVED | RECEIVED | PENDING | REJECTED | ACCEPTED_ONCHAIN }
*/
function getTransactionStatus(txId) {
return new Promise(function (resolve, reject) {
axios_1.default
.get(FEEDER_GATEWAY_URL + "/get_transaction_status?transactionId=" + txId)
.then(function (resp) {
resolve(resp.data);
})
.catch(reject);
});
}
exports.getTransactionStatus = getTransactionStatus;
/**
* Gets the transaction information from a tx id.
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L54-L58)
*
* @param txId
* @returns the transacton object { transaction_id, status, transaction, block_id?, block_number?, transaction_index?, transaction_failure_reason? }
*/
function getTransaction(txId) {
return new Promise(function (resolve, reject) {
axios_1.default
.get(FEEDER_GATEWAY_URL + "/get_transaction?transactionId=" + txId)
.then(function (resp) {
resolve(resp.data);
})
.catch(reject);
});
}
exports.getTransaction = getTransaction;
/**
* Invoke a function on the starknet contract
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17)
*
* @param tx - transaction to be invoked (WIP)
* @returns a confirmation of invoking a function on the starknet contract
*/
function addTransaction(tx) {
return new Promise(function (resolve, reject) {
axios_1.default
.post(GATEWAY_URL + "/add_transaction", tx)
.then(function (resp) {
resolve(resp.data);
})
.catch(reject);
});
}
exports.addTransaction = addTransaction;
/**
* Deploys a given compiled contract (json) to starknet
*
* @param contract - a json object containing the compiled contract
* @param address - (optional, defaults to a random address) the address where the contract should be deployed (alpha)
* @returns a confirmation of sending a transaction on the starknet contract
*/
function deployContract(contract, address) {
if (address === void 0) { address = (0, utils_1.randomAddress)(); }
var parsedContract = typeof contract === 'string' ? utils_1.JsonParser.parse(contract) : contract;
var contractDefinition = __assign(__assign({}, parsedContract), { program: (0, utils_1.compressProgram)(parsedContract.program) });
return addTransaction({
type: 'DEPLOY',
contract_address: address,
contract_definition: contractDefinition,
});
}
exports.deployContract = deployContract;
__exportStar(require("./utils"), exports);
__exportStar(require("./types"), exports);
exports.default = {
getContractAddresses: getContractAddresses,
callContract: callContract,
getBlock: getBlock,
getCode: getCode,
getStorageAt: getStorageAt,
getTransactionStatus: getTransactionStatus,
getTransaction: getTransaction,
addTransaction: addTransaction,
compressProgram: utils_1.compressProgram,
deployContract: deployContract,
};
__exportStar(require("./contract"), exports);
__exportStar(require("./starknet"), exports);

@@ -1,5 +0,5 @@

export interface GetContractAddressesResponse {
export declare type GetContractAddressesResponse = {
Starknet: string;
GpsStatementVerifier: string;
}
};
export declare type Status = 'NOT_RECEIVED' | 'RECEIVED' | 'PENDING' | 'REJECTED' | 'ACCEPTED_ONCHAIN';

@@ -10,6 +10,6 @@ export declare type TxStatus = 'TRANSACTION_RECEIVED';

export declare type CompressedProgram = string;
export interface Abi {
export declare type Abi = {
inputs: {
name: string;
type: string;
type: 'felt' | 'felt*';
}[];

@@ -19,22 +19,21 @@ name: string;

name: string;
type: string;
type: 'felt' | 'felt*';
}[];
type: string;
}
stateMutability?: 'view';
type: 'function';
};
export declare type EntryPointsByType = object;
export declare type Program = object;
export interface CompiledContract {
abi: Abi;
export declare type CompiledContract = {
abi: Abi[];
entry_points_by_type: EntryPointsByType;
program: Program;
}
export interface CompressedCompiledContract extends Omit<CompiledContract, 'program'> {
program: CompressedProgram;
}
export interface DeployTransaction {
};
export declare type CompressedCompiledContract = Omit<CompiledContract, 'program'>;
export declare type DeployTransaction = {
type: 'DEPLOY';
contract_definition: CompressedCompiledContract;
contract_address: string;
}
export interface InvokeFunctionTransaction {
};
export declare type InvokeFunctionTransaction = {
type: 'INVOKE_FUNCTION';

@@ -45,5 +44,9 @@ contract_address: string;

calldata?: string[];
}
};
export declare type Call = Omit<InvokeFunctionTransaction, 'type'>;
export declare type Transaction = DeployTransaction | InvokeFunctionTransaction;
export interface GetBlockResponse {
export declare type CallContractResponse = {
result: string[];
};
export declare type GetBlockResponse = {
sequence_number: number;

@@ -72,12 +75,12 @@ state_root: string;

status: Status;
}
export interface GetCode {
};
export declare type GetCodeResponse = {
bytecode: string[];
abi: Abi[];
}
export interface GetTransactionStatusResponse {
};
export declare type GetTransactionStatusResponse = {
tx_status: Status;
block_id: number;
}
export interface GetTransactionResponse {
};
export declare type GetTransactionResponse = {
transaction_index: number;

@@ -89,6 +92,6 @@ transaction: Transaction;

transaction_id: number;
}
export interface AddTransactionResponse {
};
export declare type AddTransactionResponse = {
code: TxStatus;
tx_id: number;
}
};

@@ -0,3 +1,7 @@

import { BigNumber } from '@ethersproject/bignumber';
import { CompressedProgram, Program } from './types';
export declare const isBrowser: boolean;
export declare const hexToDecimalString: (hex: string) => string;
export declare const arrayBufferToString: (array: ArrayBuffer) => string;
export declare const stringToUint8Array: (str: string) => Uint8Array;
export declare const btoaUniversal: (b: ArrayBuffer) => string;

@@ -22,1 +26,17 @@ export declare function randomIntFromInterval(min: number, max: number): number;

export declare function compressProgram(jsonProgram: Program | string): CompressedProgram;
/**
* Function to get the starknet keccak hash from a string
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/public/abi.py#L17-L22)
* @param value - string you want to get the starknetKeccak hash from
* @returns starknet keccak hash as BigNumber
*/
export declare function starknetKeccak(value: string): BigNumber;
/**
* Function to get the hex selector from a given function name
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/public/abi.py#L25-L26)
* @param funcName - selectors abi function name
* @returns hex selector of given abi function name
*/
export declare function getSelectorFromName(funcName: string): string;
"use strict";
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
var __importDefault = (this && this.__importDefault) || function (mod) {

@@ -6,9 +31,22 @@ return (mod && mod.__esModule) ? mod : { "default": mod };

Object.defineProperty(exports, "__esModule", { value: true });
exports.compressProgram = exports.JsonParser = exports.makeAddress = exports.randomAddress = exports.randomIntFromInterval = exports.btoaUniversal = exports.isBrowser = void 0;
exports.getSelectorFromName = exports.starknetKeccak = exports.compressProgram = exports.JsonParser = exports.makeAddress = exports.randomAddress = exports.randomIntFromInterval = exports.btoaUniversal = exports.stringToUint8Array = exports.arrayBufferToString = exports.hexToDecimalString = exports.isBrowser = void 0;
var pako_1 = require("pako");
var json_bigint_1 = __importDefault(require("json-bigint"));
var keccak_1 = require("ethereum-cryptography/keccak");
var bignumber_1 = require("@ethersproject/bignumber");
var constants_1 = require("./constants");
exports.isBrowser = typeof window !== 'undefined';
var MASK_250 = bignumber_1.BigNumber.from(2).pow(250).sub(1); // 2 ** 250 - 1
var hexToDecimalString = function (hex) {
return bignumber_1.BigNumber.from("0x" + hex.replace(/^0x/, '')).toString();
};
exports.hexToDecimalString = hexToDecimalString;
var arrayBufferToString = function (array) {
return String.fromCharCode.apply(null, array);
};
exports.arrayBufferToString = arrayBufferToString;
var stringToUint8Array = function (str) { return new TextEncoder().encode(str); };
exports.stringToUint8Array = stringToUint8Array;
var btoaUniversal = function (b) {
return exports.isBrowser ? btoa(String.fromCharCode.apply(null, b)) : Buffer.from(b).toString('base64');
return exports.isBrowser ? btoa((0, exports.arrayBufferToString)(b)) : Buffer.from(b).toString('base64');
};

@@ -46,1 +84,28 @@ exports.btoaUniversal = btoaUniversal;

exports.compressProgram = compressProgram;
function buf2hex(buffer) {
return __spreadArray([], __read(buffer), false).map(function (x) { return x.toString(16).padStart(2, '0'); }).join('');
}
var keccakHex = function (value) { return "0x" + buf2hex((0, keccak_1.keccak256)((0, exports.stringToUint8Array)(value))); };
/**
* Function to get the starknet keccak hash from a string
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/public/abi.py#L17-L22)
* @param value - string you want to get the starknetKeccak hash from
* @returns starknet keccak hash as BigNumber
*/
function starknetKeccak(value) {
return bignumber_1.BigNumber.from(keccakHex(value)).and(MASK_250);
}
exports.starknetKeccak = starknetKeccak;
/**
* Function to get the hex selector from a given function name
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/public/abi.py#L25-L26)
* @param funcName - selectors abi function name
* @returns hex selector of given abi function name
*/
function getSelectorFromName(funcName) {
// sometimes BigInteger pads the hex string with zeros, which isnt allowed in the starknet api
return starknetKeccak(funcName).toHexString();
}
exports.getSelectorFromName = getSelectorFromName;

@@ -1,115 +0,4 @@

import { compressProgram } from './utils';
import type {
GetBlockResponse,
GetCode,
GetContractAddressesResponse,
GetTransactionResponse,
GetTransactionStatusResponse,
Transaction,
AddTransactionResponse,
CompiledContract,
} from './types';
/**
* Gets the smart contract address on the goerli testnet.
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L13-L15)
* @returns starknet smart contract addresses
*/
export declare function getContractAddresses(): Promise<GetContractAddressesResponse>;
/**
* Calls a function on the StarkNet contract.
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L17-L25)
*
* @param invokeTx - transaction to be invoked (WIP)
* @param blockId
* @returns the result of the function on the smart contract.
*/
export declare function callContract(invokeTx: object, blockId: number): Promise<object>;
/**
* Gets the block information from a block ID.
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L27-L31)
*
* @param blockId
* @returns the block object { block_id, previous_block_id, state_root, status, timestamp, transaction_receipts, transactions }
*/
export declare function getBlock(blockId: number): Promise<GetBlockResponse>;
/**
* Gets the code of the deployed contract.
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L33-L36)
*
* @param contractAddress
* @param blockId
* @returns Bytecode and ABI of compiled contract
*/
export declare function getCode(contractAddress: string, blockId: number): Promise<GetCode>;
/**
* Gets the contract's storage variable at a specific key.
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L38-L46)
*
* @param contractAddress
* @param key - from getStorageVarAddress('<STORAGE_VARIABLE_NAME>') (WIP)
* @param blockId
* @returns the value of the storage variable
*/
export declare function getStorageAt(
contractAddress: string,
key: number,
blockId: number
): Promise<object>;
/**
* Gets the status of a transaction.
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L48-L52)
*
* @param txId
* @returns the transaction status object { block_id, tx_status: NOT_RECEIVED | RECEIVED | PENDING | REJECTED | ACCEPTED_ONCHAIN }
*/
export declare function getTransactionStatus(txId: number): Promise<GetTransactionStatusResponse>;
/**
* Gets the transaction information from a tx id.
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L54-L58)
*
* @param txId
* @returns the transacton object { transaction_id, status, transaction, block_id?, block_number?, transaction_index?, transaction_failure_reason? }
*/
export declare function getTransaction(txId: number): Promise<GetTransactionResponse>;
/**
* Invoke a function on the starknet contract
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17)
*
* @param tx - transaction to be invoked (WIP)
* @returns a confirmation of invoking a function on the starknet contract
*/
export declare function addTransaction(tx: Transaction): Promise<AddTransactionResponse>;
/**
* Deploys a given compiled contract (json) to starknet
*
* @param contract - a json object containing the compiled contract
* @param address - (optional, defaults to a random address) the address where the contract should be deployed (alpha)
* @returns a confirmation of sending a transaction on the starknet contract
*/
export declare function deployContract(
contract: CompiledContract | string,
address?: string
): Promise<AddTransactionResponse>;
export * from './utils';
export * from './types';
declare const _default: {
getContractAddresses: typeof getContractAddresses;
callContract: typeof callContract;
getBlock: typeof getBlock;
getCode: typeof getCode;
getStorageAt: typeof getStorageAt;
getTransactionStatus: typeof getTransactionStatus;
getTransaction: typeof getTransaction;
addTransaction: typeof addTransaction;
compressProgram: typeof compressProgram;
deployContract: typeof deployContract;
};
export default _default;
export * from './contract';
export * from './starknet';
'use strict';
var __assign =
(this && this.__assign) ||
function () {
__assign =
Object.assign ||
function (t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __createBinding =

@@ -39,224 +25,6 @@ (this && this.__createBinding) ||

};
var __importDefault =
(this && this.__importDefault) ||
function (mod) {
return mod && mod.__esModule ? mod : { default: mod };
};
Object.defineProperty(exports, '__esModule', { value: true });
exports.deployContract =
exports.addTransaction =
exports.getTransaction =
exports.getTransactionStatus =
exports.getStorageAt =
exports.getCode =
exports.getBlock =
exports.callContract =
exports.getContractAddresses =
void 0;
var axios_1 = __importDefault(require('axios'));
var utils_1 = require('./utils');
var API_URL = 'https://alpha2.starknet.io';
var FEEDER_GATEWAY_URL = API_URL + '/feeder_gateway';
var GATEWAY_URL = API_URL + '/gateway';
/**
* Gets the smart contract address on the goerli testnet.
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L13-L15)
* @returns starknet smart contract addresses
*/
function getContractAddresses() {
return new Promise(function (resolve, reject) {
axios_1.default
.get(FEEDER_GATEWAY_URL + '/get_contract_addresses')
.then(function (resp) {
resolve(resp.data);
})
.catch(reject);
});
}
exports.getContractAddresses = getContractAddresses;
// TODO: add proper type
/**
* Calls a function on the StarkNet contract.
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L17-L25)
*
* @param invokeTx - transaction to be invoked (WIP)
* @param blockId
* @returns the result of the function on the smart contract.
*/
function callContract(invokeTx, blockId) {
return new Promise(function (resolve, reject) {
axios_1.default
.post(FEEDER_GATEWAY_URL + '/call_contract?blockId=' + blockId, invokeTx)
.then(function (resp) {
resolve(resp.data);
})
.catch(reject);
});
}
exports.callContract = callContract;
/**
* Gets the block information from a block ID.
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L27-L31)
*
* @param blockId
* @returns the block object { block_id, previous_block_id, state_root, status, timestamp, transaction_receipts, transactions }
*/
function getBlock(blockId) {
return new Promise(function (resolve, reject) {
axios_1.default
.get(FEEDER_GATEWAY_URL + '/get_block?blockId=' + blockId)
.then(function (resp) {
resolve(resp.data);
})
.catch(reject);
});
}
exports.getBlock = getBlock;
/**
* Gets the code of the deployed contract.
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L33-L36)
*
* @param contractAddress
* @param blockId
* @returns Bytecode and ABI of compiled contract
*/
function getCode(contractAddress, blockId) {
return new Promise(function (resolve, reject) {
axios_1.default
.get(
FEEDER_GATEWAY_URL + '/get_code?contractAddress=' + contractAddress + '&blockId=' + blockId
)
.then(function (resp) {
resolve(resp.data);
})
.catch(reject);
});
}
exports.getCode = getCode;
// TODO: add proper type
/**
* Gets the contract's storage variable at a specific key.
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L38-L46)
*
* @param contractAddress
* @param key - from getStorageVarAddress('<STORAGE_VARIABLE_NAME>') (WIP)
* @param blockId
* @returns the value of the storage variable
*/
function getStorageAt(contractAddress, key, blockId) {
return new Promise(function (resolve, reject) {
axios_1.default
.get(
FEEDER_GATEWAY_URL +
'/get_storage_at?contractAddress=' +
contractAddress +
'&key=' +
key +
'&blockId=' +
blockId
)
.then(function (resp) {
resolve(resp.data);
})
.catch(reject);
});
}
exports.getStorageAt = getStorageAt;
/**
* Gets the status of a transaction.
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L48-L52)
*
* @param txId
* @returns the transaction status object { block_id, tx_status: NOT_RECEIVED | RECEIVED | PENDING | REJECTED | ACCEPTED_ONCHAIN }
*/
function getTransactionStatus(txId) {
return new Promise(function (resolve, reject) {
axios_1.default
.get(FEEDER_GATEWAY_URL + '/get_transaction_status?transactionId=' + txId)
.then(function (resp) {
resolve(resp.data);
})
.catch(reject);
});
}
exports.getTransactionStatus = getTransactionStatus;
/**
* Gets the transaction information from a tx id.
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L54-L58)
*
* @param txId
* @returns the transacton object { transaction_id, status, transaction, block_id?, block_number?, transaction_index?, transaction_failure_reason? }
*/
function getTransaction(txId) {
return new Promise(function (resolve, reject) {
axios_1.default
.get(FEEDER_GATEWAY_URL + '/get_transaction?transactionId=' + txId)
.then(function (resp) {
resolve(resp.data);
})
.catch(reject);
});
}
exports.getTransaction = getTransaction;
/**
* Invoke a function on the starknet contract
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17)
*
* @param tx - transaction to be invoked (WIP)
* @returns a confirmation of invoking a function on the starknet contract
*/
function addTransaction(tx) {
return new Promise(function (resolve, reject) {
axios_1.default
.post(GATEWAY_URL + '/add_transaction', tx)
.then(function (resp) {
resolve(resp.data);
})
.catch(reject);
});
}
exports.addTransaction = addTransaction;
/**
* Deploys a given compiled contract (json) to starknet
*
* @param contract - a json object containing the compiled contract
* @param address - (optional, defaults to a random address) the address where the contract should be deployed (alpha)
* @returns a confirmation of sending a transaction on the starknet contract
*/
function deployContract(contract, address) {
if (address === void 0) {
address = (0, utils_1.randomAddress)();
}
var parsedContract = typeof contract === 'string' ? utils_1.JsonParser.parse(contract) : contract;
var contractDefinition = __assign(__assign({}, parsedContract), {
program: (0, utils_1.compressProgram)(parsedContract.program),
});
return addTransaction({
type: 'DEPLOY',
contract_address: address,
contract_definition: contractDefinition,
});
}
exports.deployContract = deployContract;
__exportStar(require('./utils'), exports);
__exportStar(require('./types'), exports);
exports.default = {
getContractAddresses: getContractAddresses,
callContract: callContract,
getBlock: getBlock,
getCode: getCode,
getStorageAt: getStorageAt,
getTransactionStatus: getTransactionStatus,
getTransaction: getTransaction,
addTransaction: addTransaction,
compressProgram: utils_1.compressProgram,
deployContract: deployContract,
};
__exportStar(require('./contract'), exports);
__exportStar(require('./starknet'), exports);
{
"name": "starknet",
"version": "1.1.1",
"version": "1.2.0",
"description": "JavaScript library for StarkNet",

@@ -14,3 +14,3 @@ "main": "dist/index.js",

"test:watch": "jest --watch",
"docs": "typedoc --readme none --name 'StarkNet.js Docs' --excludeNotDocumented --disableSources",
"docs": "typedoc",
"format": "prettier --loglevel warn --write \"**/*.{ts,js,md,yml,json}\"",

@@ -55,7 +55,8 @@ "lint": "eslint . --cache --fix --ext .ts"

"typedoc": "^0.22.6",
"typedoc-plugin-markdown": "^3.11.3",
"typescript": "^4.4.4"
},
"dependencies": {
"@ethersproject/bignumber": "^5.5.0",
"axios": "^0.23.0",
"ethereum-cryptography": "^0.2.0",
"json-bigint": "^1.0.0",

@@ -69,4 +70,4 @@ "pako": "^2.0.4"

"jest": {
"testTimeout": 20000
"testTimeout": 300000
}
}

@@ -45,6 +45,6 @@ <!-- logo -->

Import `starknet` and use the [API](docs)
Import `starknet` and use the [API](https://www.starknetjs.com/modules.html)
```javascript
import starknet from 'starknet';
import * as starknet from 'starknet';

@@ -58,7 +58,7 @@ starknet.getContractAddresses().then((data) => {

[Click Here](docs)
[Click Here](https://www.starknetjs.com/modules.html)
## Contributing
If you consider to contribute to this project please read [CONTRIBUTING.md](CONTRIBUTING.md) first.
If you consider to contribute to this project please read [CONTRIBUTING.md](https://github.com/seanjameshan/starknet.js/blob/main/CONTRIBUTING.md) first.

@@ -69,2 +69,2 @@ ## License

Licensed under the [MIT license](LICENSE).
Licensed under the [MIT license](https://github.com/seanjameshan/starknet.js/blob/main/LICENSE).

@@ -1,222 +0,4 @@

import axios from 'axios';
import { randomAddress, compressProgram, JsonParser } from './utils';
import type {
GetBlockResponse,
GetCode,
GetContractAddressesResponse,
GetTransactionResponse,
GetTransactionStatusResponse,
Transaction,
AddTransactionResponse,
CompiledContract,
} from './types';
const API_URL = 'https://alpha2.starknet.io';
const FEEDER_GATEWAY_URL = `${API_URL}/feeder_gateway`;
const GATEWAY_URL = `${API_URL}/gateway`;
/**
* Gets the smart contract address on the goerli testnet.
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L13-L15)
* @returns starknet smart contract addresses
*/
export function getContractAddresses(): Promise<GetContractAddressesResponse> {
return new Promise((resolve, reject) => {
axios
.get<GetContractAddressesResponse>(`${FEEDER_GATEWAY_URL}/get_contract_addresses`)
.then((resp) => {
resolve(resp.data);
})
.catch(reject);
});
}
// TODO: add proper type
/**
* Calls a function on the StarkNet contract.
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L17-L25)
*
* @param invokeTx - transaction to be invoked (WIP)
* @param blockId
* @returns the result of the function on the smart contract.
*/
export function callContract(invokeTx: object, blockId: number): Promise<object> {
return new Promise((resolve, reject) => {
axios
.post(`${FEEDER_GATEWAY_URL}/call_contract?blockId=${blockId}`, invokeTx)
.then((resp: any) => {
resolve(resp.data);
})
.catch(reject);
});
}
/**
* Gets the block information from a block ID.
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L27-L31)
*
* @param blockId
* @returns the block object { block_id, previous_block_id, state_root, status, timestamp, transaction_receipts, transactions }
*/
export function getBlock(blockId: number): Promise<GetBlockResponse> {
return new Promise((resolve, reject) => {
axios
.get<GetBlockResponse>(`${FEEDER_GATEWAY_URL}/get_block?blockId=${blockId}`)
.then((resp: any) => {
resolve(resp.data);
})
.catch(reject);
});
}
/**
* Gets the code of the deployed contract.
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L33-L36)
*
* @param contractAddress
* @param blockId
* @returns Bytecode and ABI of compiled contract
*/
export function getCode(contractAddress: string, blockId: number): Promise<GetCode> {
return new Promise((resolve, reject) => {
axios
.get<GetCode>(
`${FEEDER_GATEWAY_URL}/get_code?contractAddress=${contractAddress}&blockId=${blockId}`
)
.then((resp) => {
resolve(resp.data);
})
.catch(reject);
});
}
// TODO: add proper type
/**
* Gets the contract's storage variable at a specific key.
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L38-L46)
*
* @param contractAddress
* @param key - from getStorageVarAddress('<STORAGE_VARIABLE_NAME>') (WIP)
* @param blockId
* @returns the value of the storage variable
*/
export function getStorageAt(
contractAddress: string,
key: number,
blockId: number
): Promise<object> {
return new Promise((resolve, reject) => {
axios
.get(
`${FEEDER_GATEWAY_URL}/get_storage_at?contractAddress=${contractAddress}&key=${key}&blockId=${blockId}`
)
.then((resp: any) => {
resolve(resp.data);
})
.catch(reject);
});
}
/**
* Gets the status of a transaction.
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L48-L52)
*
* @param txId
* @returns the transaction status object { block_id, tx_status: NOT_RECEIVED | RECEIVED | PENDING | REJECTED | ACCEPTED_ONCHAIN }
*/
export function getTransactionStatus(txId: number): Promise<GetTransactionStatusResponse> {
return new Promise((resolve, reject) => {
axios
.get<GetTransactionStatusResponse>(
`${FEEDER_GATEWAY_URL}/get_transaction_status?transactionId=${txId}`
)
.then((resp) => {
resolve(resp.data);
})
.catch(reject);
});
}
/**
* Gets the transaction information from a tx id.
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L54-L58)
*
* @param txId
* @returns the transacton object { transaction_id, status, transaction, block_id?, block_number?, transaction_index?, transaction_failure_reason? }
*/
export function getTransaction(txId: number): Promise<GetTransactionResponse> {
return new Promise((resolve, reject) => {
axios
.get<GetTransactionResponse>(`${FEEDER_GATEWAY_URL}/get_transaction?transactionId=${txId}`)
.then((resp) => {
resolve(resp.data);
})
.catch(reject);
});
}
/**
* Invoke a function on the starknet contract
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17)
*
* @param tx - transaction to be invoked (WIP)
* @returns a confirmation of invoking a function on the starknet contract
*/
export function addTransaction(tx: Transaction): Promise<AddTransactionResponse> {
return new Promise((resolve, reject) => {
axios
.post(`${GATEWAY_URL}/add_transaction`, tx)
.then((resp: any) => {
resolve(resp.data);
})
.catch(reject);
});
}
/**
* Deploys a given compiled contract (json) to starknet
*
* @param contract - a json object containing the compiled contract
* @param address - (optional, defaults to a random address) the address where the contract should be deployed (alpha)
* @returns a confirmation of sending a transaction on the starknet contract
*/
export function deployContract(
contract: CompiledContract | string,
address: string = randomAddress()
): Promise<AddTransactionResponse> {
const parsedContract =
typeof contract === 'string' ? (JsonParser.parse(contract) as CompiledContract) : contract;
const contractDefinition = {
...parsedContract,
program: compressProgram(parsedContract.program),
};
return addTransaction({
type: 'DEPLOY',
contract_address: address,
contract_definition: contractDefinition,
});
}
export * from './utils';
export * from './types';
export default {
getContractAddresses,
callContract,
getBlock,
getCode,
getStorageAt,
getTransactionStatus,
getTransaction,
addTransaction,
compressProgram,
deployContract,
};
export * from './contract';
export * from './starknet';

@@ -1,5 +0,5 @@

export interface GetContractAddressesResponse {
export type GetContractAddressesResponse = {
Starknet: string;
GpsStatementVerifier: string;
}
};

@@ -12,28 +12,27 @@ export type Status = 'NOT_RECEIVED' | 'RECEIVED' | 'PENDING' | 'REJECTED' | 'ACCEPTED_ONCHAIN';

export interface Abi {
inputs: { name: string; type: string }[];
export type Abi = {
inputs: { name: string; type: 'felt' | 'felt*' }[];
name: string;
outputs: { name: string; type: string }[];
type: string;
}
outputs: { name: string; type: 'felt' | 'felt*' }[];
stateMutability?: 'view';
type: 'function';
};
export type EntryPointsByType = object;
export type Program = object;
export interface CompiledContract {
abi: Abi;
export type CompiledContract = {
abi: Abi[];
entry_points_by_type: EntryPointsByType;
program: Program;
}
};
export interface CompressedCompiledContract extends Omit<CompiledContract, 'program'> {
program: CompressedProgram;
}
export type CompressedCompiledContract = Omit<CompiledContract, 'program'>;
export interface DeployTransaction {
export type DeployTransaction = {
type: 'DEPLOY';
contract_definition: CompressedCompiledContract;
contract_address: string;
}
};
export interface InvokeFunctionTransaction {
export type InvokeFunctionTransaction = {
type: 'INVOKE_FUNCTION';

@@ -44,7 +43,13 @@ contract_address: string;

calldata?: string[];
}
};
export type Call = Omit<InvokeFunctionTransaction, 'type'>;
export type Transaction = DeployTransaction | InvokeFunctionTransaction;
export interface GetBlockResponse {
export type CallContractResponse = {
result: string[];
};
export type GetBlockResponse = {
sequence_number: number;

@@ -73,15 +78,15 @@ state_root: string;

status: Status;
}
};
export interface GetCode {
export type GetCodeResponse = {
bytecode: string[];
abi: Abi[];
}
};
export interface GetTransactionStatusResponse {
export type GetTransactionStatusResponse = {
tx_status: Status;
block_id: number;
}
};
export interface GetTransactionResponse {
export type GetTransactionResponse = {
transaction_index: number;

@@ -93,7 +98,7 @@ transaction: Transaction;

transaction_id: number;
}
};
export interface AddTransactionResponse {
export type AddTransactionResponse = {
code: TxStatus;
tx_id: number;
}
};
import { gzip } from 'pako';
import Json from 'json-bigint';
import { keccak256 } from 'ethereum-cryptography/keccak';
import { BigNumber } from '@ethersproject/bignumber';
import { CompressedProgram, Program } from './types';

@@ -7,5 +9,14 @@ import { CONTRACT_ADDRESS_LOWER_BOUND, CONTRACT_ADDRESS_UPPER_BOUND } from './constants';

export const isBrowser = typeof window !== 'undefined';
const MASK_250 = BigNumber.from(2).pow(250).sub(1); // 2 ** 250 - 1
export const hexToDecimalString = (hex: string): string =>
BigNumber.from(`0x${hex.replace(/^0x/, '')}`).toString();
export const arrayBufferToString = (array: ArrayBuffer): string =>
String.fromCharCode.apply(null, array as any);
export const stringToUint8Array = (str: string): Uint8Array => new TextEncoder().encode(str);
export const btoaUniversal = (b: ArrayBuffer): string =>
isBrowser ? btoa(String.fromCharCode.apply(null, b as any)) : Buffer.from(b).toString('base64');
isBrowser ? btoa(arrayBufferToString(b)) : Buffer.from(b).toString('base64');

@@ -46,1 +57,30 @@ export function randomIntFromInterval(min: number, max: number) {

}
function buf2hex(buffer: Uint8Array) {
return [...buffer].map((x) => x.toString(16).padStart(2, '0')).join('');
}
const keccakHex = (value: string): string => `0x${buf2hex(keccak256(stringToUint8Array(value)))}`;
/**
* Function to get the starknet keccak hash from a string
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/public/abi.py#L17-L22)
* @param value - string you want to get the starknetKeccak hash from
* @returns starknet keccak hash as BigNumber
*/
export function starknetKeccak(value: string): BigNumber {
return BigNumber.from(keccakHex(value)).and(MASK_250);
}
/**
* Function to get the hex selector from a given function name
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/public/abi.py#L25-L26)
* @param funcName - selectors abi function name
* @returns hex selector of given abi function name
*/
export function getSelectorFromName(funcName: string) {
// sometimes BigInteger pads the hex string with zeros, which isnt allowed in the starknet api
return starknetKeccak(funcName).toHexString();
}

@@ -60,3 +60,3 @@ {

// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types */
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
"downlevelIteration": true /* Emit more compliant, but verbose and less performant JavaScript for iteration. */,
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */

@@ -109,5 +109,9 @@ // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */

"typedocOptions": {
"entryPoints": ["src/index.ts"],
"out": "docs"
"entryPoints": ["src/starknet.ts", "src/types.ts"],
"out": "docs",
"githubPages": false,
"readme": "./README.md",
"name": "StarkNet.js Docs",
"sort": "source-order"
}
}

@@ -1,5 +0,5 @@

export interface GetContractAddressesResponse {
export declare type GetContractAddressesResponse = {
Starknet: string;
GpsStatementVerifier: string;
}
};
export declare type Status =

@@ -15,6 +15,6 @@ | 'NOT_RECEIVED'

export declare type CompressedProgram = string;
export interface Abi {
export declare type Abi = {
inputs: {
name: string;
type: string;
type: 'felt' | 'felt*';
}[];

@@ -24,22 +24,21 @@ name: string;

name: string;
type: string;
type: 'felt' | 'felt*';
}[];
type: string;
}
stateMutability?: 'view';
type: 'function';
};
export declare type EntryPointsByType = object;
export declare type Program = object;
export interface CompiledContract {
abi: Abi;
export declare type CompiledContract = {
abi: Abi[];
entry_points_by_type: EntryPointsByType;
program: Program;
}
export interface CompressedCompiledContract extends Omit<CompiledContract, 'program'> {
program: CompressedProgram;
}
export interface DeployTransaction {
};
export declare type CompressedCompiledContract = Omit<CompiledContract, 'program'>;
export declare type DeployTransaction = {
type: 'DEPLOY';
contract_definition: CompressedCompiledContract;
contract_address: string;
}
export interface InvokeFunctionTransaction {
};
export declare type InvokeFunctionTransaction = {
type: 'INVOKE_FUNCTION';

@@ -50,5 +49,9 @@ contract_address: string;

calldata?: string[];
}
};
export declare type Call = Omit<InvokeFunctionTransaction, 'type'>;
export declare type Transaction = DeployTransaction | InvokeFunctionTransaction;
export interface GetBlockResponse {
export declare type CallContractResponse = {
result: string[];
};
export declare type GetBlockResponse = {
sequence_number: number;

@@ -77,12 +80,12 @@ state_root: string;

status: Status;
}
export interface GetCode {
};
export declare type GetCodeResponse = {
bytecode: string[];
abi: Abi[];
}
export interface GetTransactionStatusResponse {
};
export declare type GetTransactionStatusResponse = {
tx_status: Status;
block_id: number;
}
export interface GetTransactionResponse {
};
export declare type GetTransactionResponse = {
transaction_index: number;

@@ -94,6 +97,6 @@ transaction: Transaction;

transaction_id: number;
}
export interface AddTransactionResponse {
};
export declare type AddTransactionResponse = {
code: TxStatus;
tx_id: number;
}
};

@@ -0,3 +1,7 @@

import { BigNumber } from '@ethersproject/bignumber';
import { CompressedProgram, Program } from './types';
export declare const isBrowser: boolean;
export declare const hexToDecimalString: (hex: string) => string;
export declare const arrayBufferToString: (array: ArrayBuffer) => string;
export declare const stringToUint8Array: (str: string) => Uint8Array;
export declare const btoaUniversal: (b: ArrayBuffer) => string;

@@ -30,1 +34,17 @@ export declare function randomIntFromInterval(min: number, max: number): number;

export declare function compressProgram(jsonProgram: Program | string): CompressedProgram;
/**
* Function to get the starknet keccak hash from a string
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/public/abi.py#L17-L22)
* @param value - string you want to get the starknetKeccak hash from
* @returns starknet keccak hash as BigNumber
*/
export declare function starknetKeccak(value: string): BigNumber;
/**
* Function to get the hex selector from a given function name
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/public/abi.py#L25-L26)
* @param funcName - selectors abi function name
* @returns hex selector of given abi function name
*/
export declare function getSelectorFromName(funcName: string): string;
'use strict';
var __read =
(this && this.__read) ||
function (o, n) {
var m = typeof Symbol === 'function' && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o),
r,
ar = [],
e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
} catch (error) {
e = { error: error };
} finally {
try {
if (r && !r.done && (m = i['return'])) m.call(i);
} finally {
if (e) throw e.error;
}
}
return ar;
};
var __spreadArray =
(this && this.__spreadArray) ||
function (to, from, pack) {
if (pack || arguments.length === 2)
for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
var __importDefault =

@@ -8,3 +42,5 @@ (this && this.__importDefault) ||

Object.defineProperty(exports, '__esModule', { value: true });
exports.compressProgram =
exports.getSelectorFromName =
exports.starknetKeccak =
exports.compressProgram =
exports.JsonParser =

@@ -15,2 +51,5 @@ exports.makeAddress =

exports.btoaUniversal =
exports.stringToUint8Array =
exports.arrayBufferToString =
exports.hexToDecimalString =
exports.isBrowser =

@@ -20,7 +59,22 @@ void 0;

var json_bigint_1 = __importDefault(require('json-bigint'));
var keccak_1 = require('ethereum-cryptography/keccak');
var bignumber_1 = require('@ethersproject/bignumber');
var constants_1 = require('./constants');
exports.isBrowser = typeof window !== 'undefined';
var MASK_250 = bignumber_1.BigNumber.from(2).pow(250).sub(1); // 2 ** 250 - 1
var hexToDecimalString = function (hex) {
return bignumber_1.BigNumber.from('0x' + hex.replace(/^0x/, '')).toString();
};
exports.hexToDecimalString = hexToDecimalString;
var arrayBufferToString = function (array) {
return String.fromCharCode.apply(null, array);
};
exports.arrayBufferToString = arrayBufferToString;
var stringToUint8Array = function (str) {
return new TextEncoder().encode(str);
};
exports.stringToUint8Array = stringToUint8Array;
var btoaUniversal = function (b) {
return exports.isBrowser
? btoa(String.fromCharCode.apply(null, b))
? btoa((0, exports.arrayBufferToString)(b))
: Buffer.from(b).toString('base64');

@@ -66,1 +120,34 @@ };

exports.compressProgram = compressProgram;
function buf2hex(buffer) {
return __spreadArray([], __read(buffer), false)
.map(function (x) {
return x.toString(16).padStart(2, '0');
})
.join('');
}
var keccakHex = function (value) {
return '0x' + buf2hex((0, keccak_1.keccak256)((0, exports.stringToUint8Array)(value)));
};
/**
* Function to get the starknet keccak hash from a string
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/public/abi.py#L17-L22)
* @param value - string you want to get the starknetKeccak hash from
* @returns starknet keccak hash as BigNumber
*/
function starknetKeccak(value) {
return bignumber_1.BigNumber.from(keccakHex(value)).and(MASK_250);
}
exports.starknetKeccak = starknetKeccak;
/**
* Function to get the hex selector from a given function name
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/public/abi.py#L25-L26)
* @param funcName - selectors abi function name
* @returns hex selector of given abi function name
*/
function getSelectorFromName(funcName) {
// sometimes BigInteger pads the hex string with zeros, which isnt allowed in the starknet api
return starknetKeccak(funcName).toHexString();
}
exports.getSelectorFromName = getSelectorFromName;

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc