1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00
squid-js/src/ocean/Order.ts

70 lines
2.2 KiB
TypeScript
Raw Normal View History

2018-10-17 18:24:01 +02:00
import * as EthEcies from "eth-ecies"
2018-10-16 14:56:18 +02:00
import Keeper from "../keeper/Keeper"
2018-10-17 18:24:01 +02:00
import AccessStatus from "../models/AccessStatus"
2018-10-02 10:06:26 +02:00
import Logger from "../utils/Logger"
2018-10-16 15:08:04 +02:00
import Account from "./Account"
2018-10-16 14:56:18 +02:00
import Asset from "./Asset"
2018-10-09 10:55:53 +02:00
import OceanBase from "./OceanBase"
2018-10-09 10:55:53 +02:00
export default class Order extends OceanBase {
2018-10-16 14:56:18 +02:00
private paid: boolean
2018-10-10 11:02:00 +02:00
2018-10-16 14:56:18 +02:00
constructor(private asset: Asset, private timeout: number,
private pubkey: string, private key: any) {
super()
}
2018-10-17 18:24:01 +02:00
public async getStatus(): Promise<AccessStatus> {
const {auth} = await Keeper.getInstance()
return auth.getOrderStatus(this.id)
2018-10-16 14:56:18 +02:00
}
2018-10-16 14:56:18 +02:00
public setPaid(paid: boolean) {
this.paid = paid
}
2018-10-16 14:56:18 +02:00
public getPaid() {
return this.paid
}
2018-10-16 14:56:18 +02:00
public getTimeout() {
return this.timeout
}
2018-10-16 14:56:18 +02:00
public getKey() {
return this.key
}
2018-10-17 18:24:01 +02:00
public async pay(consumer: Account): Promise<string> {
2018-10-16 14:56:18 +02:00
const {market} = await Keeper.getInstance()
2018-10-17 18:24:01 +02:00
Logger.log(
`Sending payment: ${this.getId()} ${this.asset.publisher.getId()} ${this.asset.price} ${this.timeout}`,
)
const payReceipt = await market.payOrder(this, this.asset.publisher.getId(), this.asset.price, consumer.getId())
2018-10-17 10:12:40 +02:00
return payReceipt.events.PaymentReceived.returnValues._paymentId
}
2018-10-17 18:24:01 +02:00
public async commit(accessToken: string) {
const {auth} = await Keeper.getInstance()
const commitAccessRequestReceipt = await auth.commitAccessRequest(this, this.asset.publisher.getId())
if (commitAccessRequestReceipt.events.AccessRequestRejected) {
const {returnValues} = commitAccessRequestReceipt.events.AccessRequestRejected
throw new Error(`commitAccessRequest failed ${JSON.stringify(returnValues, null, 2)}`)
}
const pubKey = await auth.getTempPubKey(this.getId())
if (this.pubkey !== pubKey) {
throw new Error("Pubkey missmatch")
}
const encryptedAccessToken =
EthEcies.encrypt(new Buffer(pubKey, "hex"), new Buffer(accessToken)).toString("hex")
await auth.deliverAccessToken(this.getId(), `0x${encryptedAccessToken}`, this.asset.publisher.getId())
}
}