Skip to content

Commit

Permalink
feat: get-starknet-core repacked for mjs, and initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
tabaktoni committed Oct 17, 2023
1 parent f4f52e6 commit ab7fa19
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 47 deletions.
32 changes: 3 additions & 29 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@
"@noble/curves": "~1.2.0",
"@scure/starknet": "~0.3.0",
"abi-wan-kanabi": "^1.0.3",
"get-starknet-core": "^3.2.0",
"isomorphic-fetch": "^3.0.0",
"lossless-json": "^2.0.8",
"pako": "^2.0.4",
"url-join": "^4.0.1"
"url-join": "^4.0.1",
"get-starknet-core": "~/Work/ShardLabs/get-starknet/packages/core"
},
"lint-staged": {
"*.ts": "eslint --cache --fix",
Expand Down
185 changes: 169 additions & 16 deletions src/account/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,187 @@
import { RpcMessage } from 'get-starknet-core';
import {
AccountChangeEventHandler,
AddDeclareTransactionResult,
AddDeployAccountTransactionResult,
AddInvokeTransactionResult,
AddStarknetChainParameters,
ConnectedStarknetWindowObject,
NetworkChangeEventHandler,
RpcMessage,
WatchAssetParameters,
} from 'get-starknet-core';

import { AllowArray, Call } from '..';
import { StarknetChainId } from '../constants';
// eslint-disable-next-line import/no-cycle
import {
AllowArray,
ArraySignatureType,
Call,
CallData,
CompiledSierra,
DeclareContractPayload,
DeployAccountContractPayload,
MultiDeployContractResponse,
TypedData,
UniversalDeployerContractPayload,
extractContractHashes,
json,
} from '..';

// ---- TT Request Handler
type RpcCall = Omit<RpcMessage, 'result'>;

// -- TT Better naming
// This is provider object expected by WalletAccount to communicate with wallet
interface StarknetWalletProvider extends ConnectedStarknetWindowObject {}

// Represent 'Selected Active' Account inside Connected Wallet
export class WalletAccount /* implements AccountInterface */ {
public address?: string;
public address: string;

public provider: StarknetWalletProvider;

constructor(provider: StarknetWalletProvider) {
if (!provider.isConnected) throw Error('StarknetWalletProvider should be connected');
this.provider = provider;
this.address = provider.selectedAddress;

public starknetWindowObject?: StarknetWindowObject; // Should be ConnectedStarknetWindowObject
this.provider.on('accountsChanged', () => {
this.address = provider.selectedAddress;
});
}

/**
* WALLET EVENTS
*/
public onAccountChange(callback: AccountChangeEventHandler) {
this.provider.on('accountsChanged', callback);
}

public async connect(options?: ConnectOptions) {
const starknetWindowObject = await connect(options);
if (!starknetWindowObject) {
throw Error('StarknetWindowObject is null');
}
if (!starknetWindowObject.isConnected) {
throw Error('StarknetWindowObject need to be connected to the wallet');
}
public onNetworkChanged(callback: NetworkChangeEventHandler) {
this.provider.on('networkChanged', callback);
}

/**
* WALLET SPECIFIC METHODS
*/

/**
* Request Permission for wallet account, return addresses that's allowed by user
* @param silentMode false: request user interaction allowance. true: return only pre-allowed
* @returns allowed accounts addresses
*/
public requestAccounts(silentMode = false) {
const rpcCall: RpcCall = {
type: 'wallet_requestAccounts',
params: {
silentMode,
},
};
return this.provider.request(rpcCall) as Promise<string[]>;
}

/**
* Request Wallet Network change
* @param chainId StarknetChainId
* @returns boolean
*/
public switchStarknetChain(chainId: StarknetChainId) {
const rpcCall: RpcCall = {
type: 'wallet_switchStarknetChain',
params: {
chainId,
},
};
return this.provider.request(rpcCall) as Promise<boolean>;
}

/**
* Request adding ERC20 Token to Wallet List
* @param asset WatchAssetParameters
* @returns boolean
*/
public watchAsset(asset: WatchAssetParameters) {
const rpcCall: RpcCall = {
type: 'wallet_watchAsset',
params: asset,
};
return this.provider.request(rpcCall) as Promise<boolean>;
}

this.starknetWindowObject = starknetWindowObject;
/**
* Request adding custom Starknet chain
* @param chain AddStarknetChainParameters
* @returns boolean
*/
public addStarknetChain(chain: AddStarknetChainParameters) {
// Can this set custom RPC endpoint ?
const rpcCall: RpcCall = {
type: 'wallet_addStarknetChain',
params: chain,
};
return this.provider.request(rpcCall) as Promise<boolean>;
}

/**
* ACCOUNT METHODS
*/

public async execute(calls: AllowArray<Call>) {
const req2: RpcCall = {
const rpcCall: RpcCall = {
type: 'starknet_addInvokeTransaction',
params: {
calls,
calls: [].concat(calls as any),
},
};
return this.starknetWindowObject.request(req2);
return this.provider.request(rpcCall) as Promise<AddInvokeTransactionResult>;
}

public async declare(payload: DeclareContractPayload) {
const declareContractPayload = extractContractHashes(payload);

// DISCUSS: HOTFIX: Adapt Abi format
const pContract = payload.contract as CompiledSierra;
const cairo1Contract = {
...pContract,
abi: json.stringify(pContract.abi),
};

const rpcCall: RpcCall = {
type: 'starknet_addDeclareTransaction',
params: {
compiled_class_hash: declareContractPayload.compiledClassHash,
contract_class: cairo1Contract,
},
};
return this.provider.request(rpcCall) as Promise<AddDeclareTransactionResult>;
}

public async deploy(
payload: UniversalDeployerContractPayload | UniversalDeployerContractPayload[]
): Promise<MultiDeployContractResponse> {
// TODO: Create UDC PRocedure using invoke()
return new Promise((e) => false);
}

public async deployAccount(payload: DeployAccountContractPayload) {
const rpcCall: RpcCall = {
type: 'starknet_addDeployAccountTransaction',
params: {
contract_address_salt: payload.addressSalt?.toString(),
constructor_calldata: CallData.compile(payload.constructorCalldata),
class_hash: payload.classHash,
},
};
return this.provider.request(rpcCall) as Promise<AddDeployAccountTransactionResult>;
}

public async signMessage(typedData: TypedData) {
const rpcCall: RpcCall = {
type: 'starknet_signTypedData',
params: typedData,
};
return this.provider.request(rpcCall) as Promise<ArraySignatureType>;
}

// MISSING ESTIMATES
}

0 comments on commit ab7fa19

Please sign in to comment.