-
Notifications
You must be signed in to change notification settings - Fork 40
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
1 parent
d0b08ae
commit b50a439
Showing
12 changed files
with
871 additions
and
218 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
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
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,126 @@ | ||
import { ethers } from "ethers"; | ||
import { SponsoredSend as SponsoredSendModel } from "../client/api"; | ||
import { SponsoredSendStatus } from "./types"; | ||
|
||
/** | ||
* A representation of an onchain Sponsored Send. | ||
*/ | ||
export class SponsoredSend { | ||
private model: SponsoredSendModel; | ||
|
||
/** | ||
* Sponsored Sends should be constructed via higher level abstractions like Transfer. | ||
* | ||
* @class | ||
* @param model - The underlying Sponsored Send object. | ||
*/ | ||
constructor(model: SponsoredSendModel) { | ||
if (!model) { | ||
throw new Error("Invalid model type"); | ||
} | ||
this.model = model; | ||
} | ||
|
||
/** | ||
* Returns the Keccak256 hash of the typed data. This payload must be signed | ||
* by the sender to be used as an approval in the EIP-3009 transaction. | ||
* | ||
* @returns The Keccak256 hash of the typed data. | ||
*/ | ||
getTypedDataHash(): string { | ||
return this.model.typed_data_hash; | ||
} | ||
|
||
/** | ||
* Returns the signature of the typed data. | ||
* | ||
* @returns The Keccak256 hash of the typed data. | ||
*/ | ||
getSignature(): string | undefined { | ||
return this.model.signature; | ||
} | ||
|
||
/** | ||
* Signs the Sponsored Send with the provided key and returns the hex signature. | ||
* | ||
* @param key - The key to sign the Sponsored Send with | ||
* @returns The hex-encoded signature | ||
*/ | ||
async sign(key: ethers.Wallet) { | ||
ethers.toBeArray; | ||
const signature = key.signingKey.sign(ethers.getBytes(this.getTypedDataHash())).serialized; | ||
this.model.signature = signature; | ||
// Removes the '0x' prefix as required by the API. | ||
return signature.slice(2); | ||
} | ||
|
||
/** | ||
* Returns whether the Sponsored Send has been signed. | ||
* | ||
* @returns if the Sponsored Send has been signed. | ||
*/ | ||
isSigned(): boolean { | ||
return this.getSignature() ? true : false; | ||
} | ||
|
||
/** | ||
* Returns the Status of the Sponsored Send. | ||
* | ||
* @returns the Status of the Sponsored Send | ||
*/ | ||
getStatus(): SponsoredSendStatus | undefined { | ||
switch (this.model.status) { | ||
case SponsoredSendStatus.PENDING: | ||
return SponsoredSendStatus.PENDING; | ||
case SponsoredSendStatus.SIGNED: | ||
return SponsoredSendStatus.SIGNED; | ||
case SponsoredSendStatus.SUBMITTED: | ||
return SponsoredSendStatus.SUBMITTED; | ||
case SponsoredSendStatus.COMPLETE: | ||
return SponsoredSendStatus.COMPLETE; | ||
case SponsoredSendStatus.FAILED: | ||
return SponsoredSendStatus.FAILED; | ||
default: | ||
undefined; | ||
} | ||
} | ||
|
||
/** | ||
* Returns whether the Sponsored Send is in a terminal State. | ||
* | ||
* @returns Whether the Sponsored Send is in a terminal State | ||
*/ | ||
isTerminalState(): boolean { | ||
const status = this.getStatus(); | ||
return !status | ||
? false | ||
: [SponsoredSendStatus.COMPLETE, SponsoredSendStatus.FAILED].includes(status); | ||
} | ||
|
||
/** | ||
* Returns the Transaction Hash of the Sponsored Send. | ||
* | ||
* @returns The Transaction Hash | ||
*/ | ||
getTransactionHash(): string | undefined { | ||
return this.model.transaction_hash; | ||
} | ||
|
||
/** | ||
* Returns the link to the Sponsored Send on the blockchain explorer. | ||
* | ||
* @returns The link to the Sponsored Send on the blockchain explorer | ||
*/ | ||
getTransactionLink(): string | undefined { | ||
return this.model.transaction_link; | ||
} | ||
|
||
/** | ||
* Returns a string representation of the Sponsored Send. | ||
* | ||
* @returns A string representation of the Sponsored Send | ||
*/ | ||
toString(): string { | ||
return `SponsoredSend { transactionHash: '${this.getTransactionHash()}', status: '${this.getStatus()}', typedDataHash: '${this.getTypedDataHash()}', signature: ${this.getSignature()}, transactionLink: ${this.getTransactionLink()} }`; | ||
} | ||
} |
Oops, something went wrong.