-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
299 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { ethers } from "ethers" | ||
|
||
export type BasePacket = { | ||
code: string | ||
} | ||
|
||
export type Signature = { | ||
session: string, | ||
signature: string | ||
} | ||
|
||
export type Payload<T extends BasePacket> = { | ||
version: string, | ||
packet: T, | ||
signatures: Signature[] | ||
} | ||
|
||
export function signPacket(signer: ethers.Signer, packed: BasePacket): Promise<string> { | ||
const encoded = ethers.utils.toUtf8Bytes(JSON.stringify(packed, null, 0)) | ||
return signer.signMessage(encoded) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,179 @@ | ||
import { ethers } from "ethers" | ||
import { BasePacket } from "." | ||
|
||
export type TransactionsPayload = { | ||
export type TransactionsPacket = BasePacket & { | ||
wallet: string; | ||
chainId: number; | ||
|
||
transaction: ethers.providers.TransactionRequest[]; | ||
transactions: TransactionSubpacket[] | ||
} | ||
|
||
signer: { | ||
address: string; | ||
signature: string; | ||
} | ||
export type TransactionSubpacket = | ||
RawTransactionSubpacket | | ||
SendERC20Subpacket | | ||
SendERC721Subpacket | | ||
SendERC1155Subpacket | ||
|
||
|
||
export type RawTransactionSubpacket = { | ||
type: 'transaction', | ||
to: string, | ||
value: string, | ||
data: string | ||
} | ||
|
||
export function hashTransactionsPayload(wallet: string, transactions: ethers.providers.TransactionRequest[], chainId: number): string { | ||
const txHashes = transactions.map(transaction => | ||
ethers.utils.keccak256( | ||
ethers.utils.defaultAbiCoder.encode( | ||
['address', 'uint256', 'bytes', 'uint256'], | ||
[transaction.to, transaction.value, transaction.data] | ||
) | ||
) | ||
) | ||
export type SendERC20Subpacket = { | ||
type: 'erc20send', | ||
token: string, | ||
to: string, | ||
value: string | ||
} | ||
|
||
const preimage = ethers.utils.keccak256( | ||
ethers.utils.defaultAbiCoder.encode( | ||
['bytes32[]'], | ||
[[ | ||
ethers.utils.defaultAbiCoder.encode(['uint256'], [chainId]), | ||
ethers.utils.defaultAbiCoder.encode(['address'], [wallet]), | ||
...txHashes | ||
]] | ||
) | ||
) | ||
export type SendERC721Subpacket = { | ||
type: 'erc721send', | ||
token: string, | ||
to: string, | ||
id: string, | ||
safe?: boolean, | ||
data?: string | ||
} | ||
|
||
return preimage | ||
export type SendERC1155Subpacket = { | ||
type: 'erc1155send', | ||
token: string, | ||
to: string, | ||
vals: { | ||
id: string, | ||
amount: string | ||
}[], | ||
data?: string | ||
} | ||
|
||
export async function sendTransactions( | ||
signer: ethers.Wallet, | ||
export function sendTransactions( | ||
wallet: string, | ||
transactions: ethers.providers.TransactionRequest[], | ||
chainId: number | ||
): Promise<TransactionsPayload> { | ||
const hash = hashTransactionsPayload(wallet, transactions, chainId) | ||
const signature = await signer.signMessage(ethers.utils.arrayify(hash)) | ||
): TransactionsPacket { | ||
return { | ||
code: 'sendTransactions', | ||
wallet, | ||
chainId, | ||
transactions: transactions.map(tx => { | ||
if (!tx.to || tx.to === ethers.constants.AddressZero) { | ||
throw new Error('Contract creation not supported') | ||
} | ||
|
||
return { | ||
type: 'transaction', | ||
to: tx.to, | ||
value: ethers.BigNumber.from(tx.value || 0).toHexString(), | ||
data: ethers.utils.hexlify(tx.data || []) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
export function sendERC20( | ||
wallet: string, | ||
token: string, | ||
to: string, | ||
value: ethers.BigNumberish, | ||
chainId: number | ||
): TransactionsPacket { | ||
return { | ||
code: 'sendTransactions', | ||
wallet, | ||
chainId, | ||
transactions: [ | ||
{ | ||
type: 'erc20send', | ||
token, | ||
to, | ||
value: ethers.BigNumber.from(value).toString() | ||
} | ||
] | ||
} | ||
} | ||
|
||
transaction: transactions, | ||
export function sendERC721( | ||
wallet: string, | ||
token: string, | ||
to: string, | ||
id: string, | ||
chainId: number, | ||
safe?: boolean, | ||
data?: string | ||
): TransactionsPacket { | ||
return { | ||
code: 'sendTransactions', | ||
wallet, | ||
chainId, | ||
transactions: [ | ||
{ | ||
type: 'erc721send', | ||
token, | ||
to, | ||
id, | ||
safe, | ||
data | ||
} | ||
] | ||
} | ||
} | ||
|
||
export function sendERC1155( | ||
wallet: string, | ||
token: string, | ||
to: string, | ||
vals: { | ||
id: string, | ||
amount: ethers.BigNumberish | ||
}[], | ||
chainId: number, | ||
data?: string | ||
): TransactionsPacket { | ||
return { | ||
code: 'sendTransactions', | ||
wallet, | ||
chainId, | ||
transactions: [ | ||
{ | ||
type: 'erc1155send', | ||
token, | ||
to, | ||
vals: vals.map(v => ({ | ||
id: v.id, | ||
amount: ethers.BigNumber.from(v.amount).toString() | ||
})), | ||
data | ||
} | ||
] | ||
} | ||
} | ||
|
||
signer: { | ||
address: signer.address, | ||
signature | ||
} | ||
export function combinePackets( | ||
packets: TransactionsPacket[] | ||
): TransactionsPacket { | ||
if (packets.length === 0) { | ||
throw new Error('No packets provided') | ||
} | ||
|
||
// Ensure that all packets are for the same network and wallet | ||
const chainId = packets[0].chainId | ||
const wallet = packets[0].wallet | ||
|
||
if (!packets.every(p => p.chainId === chainId)) { | ||
throw new Error('All packets must have the same chainId') | ||
} | ||
|
||
if (!packets.every(p => p.wallet === wallet)) { | ||
throw new Error('All packets must have the same wallet') | ||
} | ||
|
||
return { | ||
code: 'sendTransactions', | ||
chainId, | ||
wallet, | ||
transactions: packets.reduce((acc, p) => acc.concat(p.transactions), [] as TransactionSubpacket[]) | ||
} | ||
} |
Oops, something went wrong.