Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1556 register #92

Open
wants to merge 2 commits into
base: 1138-starkware-provider
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 89 additions & 10 deletions packages/portis-web3/src/starkware/starkwareProvider.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,106 @@
import * as sc from '@authereum/starkware-crypto';
import { OrderParams, TransferErc20Params, TransferErc721Params, TransferEthParams } from './starkwareTypes';
import StarkwareAbiEncoder from '@authereum/starkware-abi-encoder';
import {
OrderParams,
TransferErc20Params,
TransferErc721Params,
TransferEthParams,
TransferParams,
} from './starkwareTypes';
import { getAssetId, getXCoordinate, quantizeAmount, sign, Signature } from '@authereum/starkware-crypto';

export default class StarkwareProvider {
/**
* Demo code - logic will be moved into the widget in a later ticket
* Do we want to allow users to supply application/index/ethaddress?
* */
private _abiEncoder: StarkwareAbiEncoder;

constructor() {
this._abiEncoder = new StarkwareAbiEncoder();
}

register(ethereumAddress, starkKey, operatorSignature) {
return this._abiEncoder.registerUser({
ethKey: ethereumAddress,
starkKey,
operatorSignature,
});
}

getStarkKey() {
const mnemonic = 'curve become rib fuel garment engine great spring aisle mandate also host';
const mnemonic = 'raw reveal finish flash cancel famous improve gas slam unaware polar city';
const ethAddress = '0xBA8d15d1FCE4b6f3be8F2DA33c15a678D2f34180';

const layer = 'starkex';
const application = 'starkexdvf';
const index = '1';
const ethAddress = '0xc536a7ac035015a017146a97f3ef44851eaaef41';

const path = sc.getAccountPath(layer, application, ethAddress, index);
const keyPair = sc.getKeyPairFromPath(mnemonic, path);
return sc.getStarkKey(keyPair);
const pubKey = sc.getPublic(keyPair, false);
const xCoord = getXCoordinate(pubKey);
return '0x' + xCoord;
}

createLimitOrder(params: OrderParams) {}
transferEth(params: TransferEthParams): Promise<Signature> {
const starkKey = this.getStarkKey();
const { vaultId, to, quantum, amount, nonce, expirationTimestamp, condition } = params;
return this.transfer({
from: {
starkKey,
vaultId,
},
to,
asset: {
type: 'ETH',
data: {
quantum,
},
},
amount,
nonce,
expirationTimestamp,
condition,
});
}

transferEth(params: TransferEthParams) {}
createLimitOrder(params: OrderParams) {}

transferErc20(params: TransferErc20Params) {}

transferErc721(params: TransferErc721Params) {}

private getKeypair() {
const mnemonic = 'raw reveal finish flash cancel famous improve gas slam unaware polar city';
const ethAddress = '0xBA8d15d1FCE4b6f3be8F2DA33c15a678D2f34180';

const layer = 'starkex';
const application = 'starkexdvf';
const index = '2';

const path = sc.getAccountPath(layer, application, ethAddress, index);
return sc.getKeyPairFromPath(mnemonic, path);
}

private async transfer(input: TransferParams): Promise<Signature> {
const { from, to, asset, amount, nonce, expirationTimestamp, condition } = input;
const assetId = getAssetId(asset);
const quantizedAmount = quantizeAmount(amount as string, asset.data.quantum as string);
const senderVaultId = from.vaultId;
const targetVaultId = to.vaultId;
const targetKey = input.to.starkKey;

const msgHash = await this._abiEncoder.transfer({
quantizedAmount,
nonce,
senderVaultId,
assetId,
targetVaultId,
targetKey,
expirationTimestamp,
condition,
});

console.log(`executing transfer with hash ${msgHash}`);
const keypair = this.getKeypair();
const starkSignature = await sign(keypair, msgHash);
return starkSignature;
}
}