From 461955de0f3094c16dedea76a190447374296641 Mon Sep 17 00:00:00 2001 From: katien Date: Wed, 10 Mar 2021 14:38:38 -0700 Subject: [PATCH 1/2] make registerUser abiEncoder call, this commit also contains transfer logic to be used in a later task --- .../src/starkware/starkwareProvider.ts | 84 ++++++++++++++++++- 1 file changed, 81 insertions(+), 3 deletions(-) diff --git a/packages/portis-web3/src/starkware/starkwareProvider.ts b/packages/portis-web3/src/starkware/starkwareProvider.ts index 1782936..0ec4b20 100644 --- a/packages/portis-web3/src/starkware/starkwareProvider.ts +++ b/packages/portis-web3/src/starkware/starkwareProvider.ts @@ -1,9 +1,31 @@ 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, quantizeAmount, sign, Signature } from '@authereum/starkware-crypto'; export default class StarkwareProvider { + private _abiEncoder: StarkwareAbiEncoder; + + constructor() { + this._abiEncoder = new StarkwareAbiEncoder(); + } + + register(ethereumAddress, starkKey, operatorSignature) { + const txData = this._abiEncoder.registerUser({ + ethKey: ethereumAddress, + starkKey, + operatorSignature, + }); + console.log(txData); + return txData; + } /** - * Demo code - logic will be moved into the widget in a later ticket * Do we want to allow users to supply application/index/ethaddress? * */ getStarkKey() { @@ -19,9 +41,65 @@ export default class StarkwareProvider { createLimitOrder(params: OrderParams) {} - transferEth(params: TransferEthParams) {} + transferEth(params: TransferEthParams): Promise { + 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, + }); + } transferErc20(params: TransferErc20Params) {} transferErc721(params: TransferErc721Params) {} + + private getKeypair() { + const mnemonic = 'curve become rib fuel garment engine great spring aisle mandate also host'; + 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 keyPair; + } + + private async transfer(input: TransferParams): Promise { + 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; + } } From 13b20c55200eebf3ffc0b6ad735503b9d34181f6 Mon Sep 17 00:00:00 2001 From: katien Date: Mon, 15 Mar 2021 16:44:08 -0600 Subject: [PATCH 2/2] update getStarkKey to return only x coord of key with 0x prefix, add register abi encoder call --- .../src/starkware/starkwareProvider.ts | 35 ++++++++++--------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/packages/portis-web3/src/starkware/starkwareProvider.ts b/packages/portis-web3/src/starkware/starkwareProvider.ts index 0ec4b20..f85eea6 100644 --- a/packages/portis-web3/src/starkware/starkwareProvider.ts +++ b/packages/portis-web3/src/starkware/starkwareProvider.ts @@ -7,7 +7,7 @@ import { TransferEthParams, TransferParams, } from './starkwareTypes'; -import { getAssetId, quantizeAmount, sign, Signature } from '@authereum/starkware-crypto'; +import { getAssetId, getXCoordinate, quantizeAmount, sign, Signature } from '@authereum/starkware-crypto'; export default class StarkwareProvider { private _abiEncoder: StarkwareAbiEncoder; @@ -17,30 +17,28 @@ export default class StarkwareProvider { } register(ethereumAddress, starkKey, operatorSignature) { - const txData = this._abiEncoder.registerUser({ + return this._abiEncoder.registerUser({ ethKey: ethereumAddress, starkKey, operatorSignature, }); - console.log(txData); - return txData; } - /** - * Do we want to allow users to supply application/index/ethaddress? - * */ + 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 { const starkKey = this.getStarkKey(); const { vaultId, to, quantum, amount, nonce, expirationTimestamp, condition } = params; @@ -63,19 +61,22 @@ export default class StarkwareProvider { }); } + createLimitOrder(params: OrderParams) {} + transferErc20(params: TransferErc20Params) {} transferErc721(params: TransferErc721Params) {} private getKeypair() { - 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 index = '2'; + const path = sc.getAccountPath(layer, application, ethAddress, index); - const keyPair = sc.getKeyPairFromPath(mnemonic, path); - return keyPair; + return sc.getKeyPairFromPath(mnemonic, path); } private async transfer(input: TransferParams): Promise {