Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 38 additions & 37 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zkfold-smart-wallet-api",
"version": "1.8.2",
"version": "1.9.0",
"description": "Smart Wallet API - Browser and extension compatible",
"main": "dist/smart-wallet-api.es.js",
"module": "dist/smart-wallet-api.es.js",
Expand Down
56 changes: 42 additions & 14 deletions src/Service/Backend.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as CSL from '@emurgo/cardano-serialization-lib-browser';
import axios from 'axios';
import { serialize } from '../JSON';
import { BigIntWrap, ProofBytes, Output, Reference, UTxO, CreateWalletResponse, SendFundsResponse, PrepareTxParameters, PrepareTxResponse, SubmitTxResult, ClientCredentials, Settings, BalanceResponse, Transaction } from '../Types'
import { BigIntWrap, ProofBytes, SigmaProof, Output, Reference, UTxO, CreateWalletResponse, SendFundsResponse, PrepareTxParameters, PrepareTxResponse, SubmitTxResult, ClientCredentials, Settings, BalanceResponse, Transaction } from '../Types'

/**
* A wrapper for interaction with the backend.
Expand All @@ -10,6 +10,7 @@ import { BigIntWrap, ProofBytes, Output, Reference, UTxO, CreateWalletResponse,
export class Backend {
private url: string
private secret: string | null
private apiVersion: number

/**
* Creates a new Backend object.
Expand All @@ -19,6 +20,11 @@ export class Backend {
constructor(url: string, secret: string | null = null) {
this.url = url
this.secret = secret
this.apiVersion = 0
}

public setApiVersion(version: number) {
this.apiVersion = version
}

private headers(additional: Record<string, string> = {}) {
Expand All @@ -40,7 +46,7 @@ export class Backend {
* @returns {Settings}
*/
public async settings(): Promise<Settings> {
const { data } = await axios.get(`${this.url}/v0/settings`, this.headers())
const { data } = await axios.get(`${this.url}/v${this.apiVersion}/settings`, this.headers())
return data
}

Expand All @@ -50,7 +56,7 @@ export class Backend {
* @returns {ClientCredentials}
*/
public async credentials(): Promise<ClientCredentials> {
const { data } = await axios.get(`${this.url}/v0/oauth/credentials`, this.headers())
const { data } = await axios.get(`${this.url}/v${this.apiVersion}/oauth/credentials`, this.headers())
return data
}

Expand All @@ -61,7 +67,7 @@ export class Backend {
* @returns {CSL.Address}
*/
public async walletMainAddress(email: string): Promise<CSL.Address> {
const { data } = await axios.post(`${this.url}/v0/wallet/address`, {
const { data } = await axios.post(`${this.url}/v${this.apiVersion}/wallet/address`, {
'email': email
}, this.headers())

Expand All @@ -75,7 +81,7 @@ export class Backend {
* @returns {CSL.Address}
*/
public async walletUnusedAddress(email: string): Promise<CSL.Address> {
const { data } = await axios.post(`${this.url}/v0/wallet/extra-address`, {
const { data } = await axios.post(`${this.url}/v${this.apiVersion}/wallet/extra-address`, {
'email': email
}, this.headers())

Expand All @@ -100,7 +106,7 @@ export class Backend {

const payload = serialize(requestData)

const { data } = await axios.post(`${this.url}/v0/wallet/activate`, payload,
const { data } = await axios.post(`${this.url}/v${this.apiVersion}/wallet/activate`, payload,
this.headers({ 'Content-Type': 'application/json' })
)

Expand Down Expand Up @@ -134,7 +140,7 @@ export class Backend {

const payload = serialize(requestData)

const { data } = await axios.post(`${this.url}/v0/wallet/activate-and-send-funds`, payload,
const { data } = await axios.post(`${this.url}/v${this.apiVersion}/wallet/activate-and-send-funds`, payload,
this.headers({ 'Content-Type': 'application/json' })
)

Expand Down Expand Up @@ -166,7 +172,29 @@ export class Backend {

const payload = serialize(requestData)

const { data } = await axios.post(`${this.url}/v0/wallet/send-funds`, payload,
const { data } = await axios.post(`${this.url}/v${this.apiVersion}/wallet/send-funds`, payload,
this.headers({ 'Content-Type': 'application/json' })
)

const response: SendFundsResponse = {
transaction: data.transaction,
transaction_fee: data.transaction_fee,
transaction_id: data.transaction_id
}

return response
}

public async sendFundsv1(jwt: string, outs: Output[], proof: SigmaProof): Promise<SendFundsResponse> {
const requestData = {
'jwt': jwt,
'outs': outs,
'proof': proof,
}

const payload = serialize(requestData)

const { data } = await axios.post(`${this.url}/v${this.apiVersion}/wallet/send-funds`, payload,
this.headers({ 'Content-Type': 'application/json' })
)

Expand All @@ -188,7 +216,7 @@ export class Backend {
async prepareTx(params: PrepareTxParameters): Promise<PrepareTxResponse> {
const payload = serialize(params)

const { data } = await axios.post(`${this.url}/v0/wallet/prepare-tx`, payload,
const { data } = await axios.post(`${this.url}/v${this.apiVersion}/wallet/prepare-tx`, payload,
this.headers({ 'Content-Type': 'application/json' })
)

Expand All @@ -209,7 +237,7 @@ export class Backend {
* @returns {SubmitTxResult} - Transaction ID and email delivery errors, if any
*/
public async submitTx(transaction: string, email_recipients: string[] = [], sender?: string): Promise<SubmitTxResult> {
const { data } = await axios.post(`${this.url}/v0/tx/submit`, {
const { data } = await axios.post(`${this.url}/v${this.apiVersion}/tx/submit`, {
email_recipients: email_recipients,
sender: sender,
transaction: transaction
Expand All @@ -230,7 +258,7 @@ export class Backend {
* @returns {SubmitTxResult} - Transaction ID and email delivery errors, if any
*/
public async addVkeyAndSubmitTx(unsigned_transaction: string, vkey_witness: string, email_recipients: string[] = [], sender?: string): Promise<SubmitTxResult> {
const { data } = await axios.post(`${this.url}/v0/tx/add-vkey-and-submit`, {
const { data } = await axios.post(`${this.url}/v${this.apiVersion}/tx/add-vkey-and-submit`, {
unsigned_transaction: unsigned_transaction,
vkey_witness: vkey_witness,
email_recipients: email_recipients,
Expand All @@ -250,7 +278,7 @@ export class Backend {
* @returns {UTxO[]}
*/
public async addressUtxo(address: CSL.Address): Promise<UTxO[]> {
const { data } = await axios.post(`${this.url}/v0/address/utxos`, [address.to_bech32()], this.headers())
const { data } = await axios.post(`${this.url}/v${this.apiVersion}/address/utxos`, [address.to_bech32()], this.headers())

const result: UTxO[] = []

Expand Down Expand Up @@ -286,7 +314,7 @@ export class Backend {
* @returns {BalanceResponse}
*/
public async balance(email: string): Promise<BalanceResponse> {
const { data } = await axios.post(`${this.url}/v0/address/balance`, email, this.headers({ 'Content-Type': 'application/json' }))
const { data } = await axios.post(`${this.url}/v${this.apiVersion}/address/balance`, email, this.headers({ 'Content-Type': 'application/json' }))
return data
}

Expand All @@ -297,7 +325,7 @@ export class Backend {
* @returns {Transaction[]}
*/
public async txHistory(email: string): Promise<Transaction[]> {
const { data } = await axios.post(`${this.url}/v0/wallet/txs`, { 'email': email }, this.headers())
const { data } = await axios.post(`${this.url}/v${this.apiVersion}/wallet/txs`, { 'email': email }, this.headers())

// TODO: fetch token tickers from Cardano Token Registry if it isn't done on the back end
//
Expand Down
13 changes: 12 additions & 1 deletion src/Service/Google.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import axios from 'axios'
import { GoogleTokenResponse, GoogleCertKey } from '../Types'
import { base64UrlDecode } from '../Utils'

export class GoogleApi {
private clientId: string
Expand Down Expand Up @@ -130,4 +131,14 @@ export class GoogleApi {
const parts = jwt.split(".")
return `${parts[0]}.${parts[1]}`
}
}

/**
* Base64url-decode the jwt
* @param {string} jwt - The JWT string.
* @returns {string} The decoded JWT without the signature.
*/
public decodeJwt(jwt: string): string {
const parts = jwt.split(".")
return `${base64UrlDecode(parts[0])}.${base64UrlDecode(parts[1])}`
}
}
10 changes: 5 additions & 5 deletions src/Service/Prover.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from 'axios';
import forge from 'node-forge';
import { ProofBytes, ProverPublicKey, ProofInput, BigIntWrap } from '../Types';
import { ProofBytes, ProverPublicKey, PlonkProofInput, BigIntWrap } from '../Types';
import { deserialize, serialize } from '../JSON';

/**
Expand Down Expand Up @@ -41,10 +41,10 @@ export class Prover {
/**
* Submit a proof request to the Prover. It will return a Request ID which can be used to retrieve proof status
* @async
* @param {ProofInput} proofInput for the expMod circuit: exponent, modulus, signature and token name
* @param {PlonkProofInput} proofInput for the expMod circuit: exponent, modulus, signature and token name
* @returns {string} proof request ID
*/
public async requestProof(proofInput: ProofInput): Promise<string> {
public async requestProof(proofInput: PlonkProofInput): Promise<string> {
const keys = await this.serverKeys()

const key = keys[0]
Expand Down Expand Up @@ -108,10 +108,10 @@ export class Prover {
/**
* Obtain a Proof from the Prover. Unlike requestProof(), this method waits for the proof completion
* @async
* @param {ProofInput} proofInput for the expMod circuit: exponent, modulus, signature and token name
* @param {PlonkProofInput} proofInput for the expMod circuit: exponent, modulus, signature and token name
* @returns {ProofBytes} ZK proof bytes for the expMod circuit
*/
public async prove(proofInput: ProofInput): Promise<ProofBytes> {
public async prove(proofInput: PlonkProofInput): Promise<ProofBytes> {
const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms))
const proofId = await this.requestProof(proofInput)

Expand Down
Loading