Skip to content

Commit

Permalink
Merge pull request #5 from everFinance/feature/upgrade-ar-signature
Browse files Browse the repository at this point in the history
feat(arweave-sig): move arOwner from data to sig
  • Loading branch information
Xaber20110202 authored Aug 24, 2021
2 parents 657fa97 + db1cb22 commit 3637c03
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 37 deletions.
5 changes: 2 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getEverpayTxDataField, getEverpayTxMessage, signMessageAsync, transferAsync } from './lib/sign'
import { getEverpayTxMessage, signMessageAsync, transferAsync } from './lib/sign'
import { getEverpayBalance, getEverpayBalances, getEverpayInfo, getEverpayTransaction, getEverpayTransactions, getExpressInfo, getMintdEverpayTransactionByChainTxHash, postTx } from './api'
import { everpayTxVersion, getExpressHost, getEverpayHost } from './config'
import { getTimestamp, getTokenBySymbol, toBN, getAccountChainType, fromDecimalToUnit, genTokenTag, matchTokenTag, genExpressData, fromUnitToDecimalBN } from './utils/util'
Expand Down Expand Up @@ -147,7 +147,6 @@ class Everpay extends EverpayBase {
checkParams({ token })

const from = this._config.account as string
const accountChainType = getAccountChainType(from)
let data = params.data
let to = params?.to as string
let decimalFeeBN = toBN(0)
Expand Down Expand Up @@ -228,7 +227,7 @@ class Everpay extends EverpayBase {
tokenID: token?.id as string,
chainType: token?.chainType as string,
chainID: token?.chainID as string,
data: await getEverpayTxDataField(this._config, accountChainType, data),
data: data !== undefined ? JSON.stringify(data) : '',
version: everpayTxVersion
}
return everpayTxWithoutSig
Expand Down
24 changes: 10 additions & 14 deletions src/lib/arweave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ export const checkArPermissions = async (permissions: string[] | string): Promis
}
}

const getEverpayTxDataFieldAsync = async (arJWK: ArJWK, data?: Record<string, unknown>): Promise<string> => {
const signMessageAsync = async (arJWK: ArJWK, address: string, everHash: string): Promise<string> => {
const arweave = Arweave.init(options)
const everHashBuffer: Buffer = Buffer.from(everHash.slice(2), 'hex')
let arOwner = ''
let signatureB64url = ''
// web
if (arJWK === 'use_wallet') {
try {
await checkArPermissions('ACCESS_PUBLIC_KEY')
Expand All @@ -58,17 +62,7 @@ const getEverpayTxDataFieldAsync = async (arJWK: ArJWK, data?: Record<string, un
} catch {
throw new Error(ERRORS.ACCESS_PUBLIC_KEY_FAILED)
}
} else if (arJWK !== undefined) {
arOwner = arJWK.n
}
return JSON.stringify(data !== undefined ? { ...data, arOwner } : { arOwner })
}

const signMessageAsync = async (arJWK: ArJWK, address: string, everHash: string): Promise<string> => {
const arweave = Arweave.init(options)
const everHashBuffer: Buffer = Buffer.from(everHash.slice(2), 'hex')
// web
if (arJWK === 'use_wallet') {
try {
await checkArPermissions('SIGNATURE')
} catch {
Expand All @@ -87,16 +81,19 @@ const signMessageAsync = async (arJWK: ArJWK, address: string, everHash: string)
algorithm
)
const buf = new Uint8Array(Object.values(signature))
return Arweave.utils.bufferTob64Url(buf)
signatureB64url = Arweave.utils.bufferTob64Url(buf)
} catch {
throw new Error(ERRORS.SIGNATURE_FAILED)
}

// node
} else {
const buf = await arweave.crypto.sign(arJWK, everHashBuffer)
return Arweave.utils.bufferTob64Url(buf)
arOwner = arJWK.n
signatureB64url = Arweave.utils.bufferTob64Url(buf)
}

return `${signatureB64url},${arOwner}`
}

const transferAsync = async (arJWK: ArJWK, {
Expand All @@ -122,7 +119,6 @@ const transferAsync = async (arJWK: ArJWK, {
}

export default {
getEverpayTxDataFieldAsync,
signMessageAsync,
transferAsync
}
5 changes: 0 additions & 5 deletions src/lib/ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ import erc20Abi from '../constants/abi/erc20'
import { getTokenAddrByChainType } from '../utils/util'
import { ChainType, EthereumTransaction } from '../types'

const getEverpayTxDataFieldAsync = async (data?: Record<string, unknown>): Promise<string> => {
return data !== undefined ? JSON.stringify(data) : ''
}

// 参考自 zkSync
// https://github.com/WalletConnect/walletconnect-monorepo/issues/347#issuecomment-880553018
const signMessageAsync = async (ethConnectedSigner: Signer, address: string, message: string): Promise<string> => {
Expand Down Expand Up @@ -57,7 +53,6 @@ const transferAsync = async (ethConnectedSigner: Signer, {
}

export default {
getEverpayTxDataFieldAsync,
signMessageAsync,
transferAsync
}
13 changes: 0 additions & 13 deletions src/lib/sign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,6 @@ const getDepositAddr = (info: EverpayInfo, accountChainType: ChainType): string
throw new Error(ERRORS.INVALID_ACCOUNT_TYPE)
}

export const getEverpayTxDataField = async (
config: Config,
accountChainType: ChainType,
data?: Record<string, unknown>
): Promise<string> => {
if (accountChainType === ChainType.ethereum) {
return await ethereumLib.getEverpayTxDataFieldAsync(data)
} else if (accountChainType === ChainType.arweave) {
return await arweaveLib.getEverpayTxDataFieldAsync(config.arJWK as ArJWK, data)
}
throw new Error(ERRORS.INVALID_ACCOUNT_TYPE)
}

export const getEverpayTxMessage = (everpayTxWithoutSig: EverpayTxWithoutSig): string => {
const keys = [
'tokenSymbol',
Expand Down
4 changes: 2 additions & 2 deletions test/sign.arweave.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ const options = {
test('check arweaveLib.signMessageAsync', async () => {
const everHash = '0xdfc9ef6613b52c043d68496ec1bd2db0cd2d5891d878208938c4ce31a4826274'
const dataBuf = Buffer.from(everHash.slice(2), 'hex')
const msgBase64 = await arweaveLib.signMessageAsync(arWallet1.jwk, arWallet1.address, everHash)
const msgBase64WithArOwner = await arweaveLib.signMessageAsync(arWallet1.jwk, arWallet1.address, everHash)

const arweave = Arweave.init(options)
const verified = await arweave.crypto.verify(arWallet1.jwk.n, dataBuf, b64UrlToBuffer(msgBase64))
const verified = await arweave.crypto.verify(arWallet1.jwk.n, dataBuf, b64UrlToBuffer(msgBase64WithArOwner.split(',')[0]))
expect(verified).toBe(true)
})

Expand Down

0 comments on commit 3637c03

Please sign in to comment.