Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] feat: sign data #88

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
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
26 changes: 26 additions & 0 deletions helios.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4230,12 +4230,19 @@ export class CoinSelection {
static selectSmallestFirst(utxos: UTxO[], amount: Value): [UTxO[], UTxO[]];
static selectLargestFirst(utxos: UTxO[], amount: Value): [UTxO[], UTxO[]];
}
/**
* @typedef {{
* signature: string,
* key: string,
* }} DataSignature
*/
/**
* @typedef {{
* isMainnet(): Promise<boolean>,
* usedAddresses: Promise<Address[]>,
* unusedAddresses: Promise<Address[]>,
* utxos: Promise<UTxO[]>,
* signData(address: Address, data: string): Promise<DataSignature>,
* signTx(tx: Tx): Promise<Signature[]>,
* submitTx(tx: Tx): Promise<TxId>
* }} Wallet
Expand All @@ -4246,6 +4253,7 @@ export class CoinSelection {
* getUsedAddresses(): Promise<string[]>,
* getUnusedAddresses(): Promise<string[]>,
* getUtxos(): Promise<string[]>,
* signData(address: string, data: string): Promise<DataSignature>,
* signTx(txHex: string, partialSign: boolean): Promise<string>,
* submitTx(txHex: string): Promise<string>
* }} Cip30Handle
Expand Down Expand Up @@ -4274,6 +4282,12 @@ export class Cip30Wallet implements Wallet {
* @type {Promise<UTxO[]>}
*/
get utxos(): Promise<UTxO[]>;
/**
* @param {Address} address
* @param {string} data
* @returns {Promise<DataSignature>}
*/
signData(address: Address, data: string): Promise<DataSignature>;
/**
* @param {Tx} tx
* @returns {Promise<Signature[]>}
Expand Down Expand Up @@ -4435,6 +4449,12 @@ export class WalletEmulator implements Wallet {
* @type {Promise<UTxO[]>}
*/
get utxos(): Promise<UTxO[]>;
/**
* @param {Address} address
* @param {string} data
* @returns {Promise<DataSignature>}
*/
signData(address: Address, data: string): Promise<DataSignature>;
/**
* Simply assumed the tx needs to by signed by this wallet without checking.
* @param {Tx} tx
Expand Down Expand Up @@ -4718,11 +4738,16 @@ export type PropertyTest = (args: UplcValue[], res: (UplcValue | UserError)) =>
[x: string]: boolean;
});
export type CoinSelectionAlgorithm = (utxos: UTxO[], amount: Value) => [UTxO[], UTxO[]];
export type DataSignature = {
signature: string;
key: string;
};
export type Wallet = {
isMainnet(): Promise<boolean>;
usedAddresses: Promise<Address[]>;
unusedAddresses: Promise<Address[]>;
utxos: Promise<UTxO[]>;
signData(address: Address, data: string): Promise<DataSignature>;
signTx(tx: Tx): Promise<Signature[]>;
submitTx(tx: Tx): Promise<TxId>;
};
Expand All @@ -4731,6 +4756,7 @@ export type Cip30Handle = {
getUsedAddresses(): Promise<string[]>;
getUnusedAddresses(): Promise<string[]>;
getUtxos(): Promise<string[]>;
signData(address: string, data: string): Promise<DataSignature>;
signTx(txHex: string, partialSign: boolean): Promise<string>;
submitTx(txHex: string): Promise<string>;
};
Expand Down
83 changes: 56 additions & 27 deletions helios.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// Website: https://www.hyperion-bt.org
// Repository: https://github.com/hyperion-bt/helios
// Version: 0.14.0
// Last update: May 2023
// Last update: June 2023
// License type: BSD-3-Clause
//
//
Expand Down Expand Up @@ -42587,13 +42587,21 @@ export class CoinSelection {
// Section 37: Wallets
//////////////////////

// Where best to put this?
/**
* @typedef {{
* signature: string,
* key: string,
* }} DataSignature
*/

/**
* @typedef {{
* isMainnet(): Promise<boolean>,
* usedAddresses: Promise<Address[]>,
* unusedAddresses: Promise<Address[]>,
* utxos: Promise<UTxO[]>,
* signData(address: Address, data: string): Promise<DataSignature>,
* signTx(tx: Tx): Promise<Signature[]>,
* submitTx(tx: Tx): Promise<TxId>
* }} Wallet
Expand All @@ -42605,6 +42613,7 @@ export class CoinSelection {
* getUsedAddresses(): Promise<string[]>,
* getUnusedAddresses(): Promise<string[]>,
* getUtxos(): Promise<string[]>,
* signData(address: string, data: string): Promise<DataSignature>,
* signTx(txHex: string, partialSign: boolean): Promise<string>,
* submitTx(txHex: string): Promise<string>
* }} Cip30Handle
Expand All @@ -42617,7 +42626,7 @@ export class Cip30Wallet {
#handle;

/**
* @param {Cip30Handle} handle
* @param {Cip30Handle} handle
*/
constructor(handle) {
this.#handle = handle;
Expand Down Expand Up @@ -42652,17 +42661,26 @@ export class Cip30Wallet {
}

/**
* @param {Tx} tx
* @param {Address} address
* @param {string} data
* @returns {Promise<DataSignature>}
*/
async signData(address, data) {
return this.#handle.signData(address.toHex(), data);
}

/**
* @param {Tx} tx
* @returns {Promise<Signature[]>}
*/
async signTx(tx) {
const res = await this.#handle.signTx(bytesToHex(tx.toCbor()), true);

return TxWitnesses.fromCbor(hexToBytes(res)).signatures;
}

/**
* @param {Tx} tx
* @param {Tx} tx
* @returns {Promise<TxId>}
*/
async submitTx(tx) {
Expand All @@ -42676,7 +42694,7 @@ export class WalletHelper {
#wallet;

/**
* @param {Wallet} wallet
* @param {Wallet} wallet
*/
constructor(wallet) {
this.#wallet = wallet;
Expand Down Expand Up @@ -42745,10 +42763,10 @@ export class WalletHelper {
}

/**
* @param {Value} amount
* @param {Value} amount
* @param {(allUtxos: UTxO[], anount: Value) => [UTxO[], UTxO[]]} algorithm
* @returns {Promise<[UTxO[], UTxO[]]>} - [picked, not picked that can be used as spares]
*/
*/
async pickUtxos(amount, algorithm = CoinSelection.selectSmallestFirst) {
return algorithm(await this.#wallet.utxos, amount);
}
Expand Down Expand Up @@ -42811,6 +42829,7 @@ export class WalletHelper {
}



//////////////////////
// Section 38: Network
//////////////////////
Expand Down Expand Up @@ -43029,7 +43048,7 @@ export class WalletEmulator {
*/
#pubKey;

/**
/**
* @param {Network} network
* @param {NumberGenerator} random - used to generate the private key
*/
Expand Down Expand Up @@ -43104,6 +43123,15 @@ export class WalletEmulator {
});
}

/**
* @param {Address} address
* @param {string} data
* @returns {Promise<DataSignature>}
*/
async signData(address, data) {
throw new Error("not yet implemented");
}

/**
* Simply assumed the tx needs to by signed by this wallet without checking.
* @param {Tx} tx
Expand All @@ -43116,7 +43144,7 @@ export class WalletEmulator {
}

/**
* @param {Tx} tx
* @param {Tx} tx
* @returns {Promise<TxId>}
*/
async submitTx(tx) {
Expand Down Expand Up @@ -43144,9 +43172,9 @@ class GenesisTx {

/**
* @param {number} id
* @param {Address} address
* @param {Address} address
* @param {bigint} lovelace
* @param {Assets} assets
* @param {Assets} assets
*/
constructor(id, address, lovelace, assets) {
this.#id = id;
Expand All @@ -43171,9 +43199,9 @@ class GenesisTx {
}

/**
* @param {TxId} txId
* @param {bigint} utxoIdx
* @returns
* @param {TxId} txId
* @param {bigint} utxoIdx
* @returns
*/
consumes(txId, utxoIdx) {
return false;
Expand Down Expand Up @@ -43211,7 +43239,7 @@ class RegularTx {
#tx;

/**
* @param {Tx} tx
* @param {Tx} tx
*/
constructor(tx) {
this.#tx = tx;
Expand All @@ -43238,8 +43266,8 @@ class RegularTx {
}

/**
* @param {Address} address
* @param {UTxO[]} utxos
* @param {Address} address
* @param {UTxO[]} utxos
* @returns {UTxO[]}
*/
collectUtxos(address, utxos) {
Expand Down Expand Up @@ -43291,7 +43319,7 @@ export class NetworkEmulator {
#blocks;

/**
* @param {number} seed
* @param {number} seed
*/
constructor(seed = 0) {
this.#slot = 0n;
Expand All @@ -43304,7 +43332,7 @@ export class NetworkEmulator {
/**
* Create a copy of networkParams that always has access to the current slot
* (for setting the validity range automatically)
* @param {NetworkParams} networkParams
* @param {NetworkParams} networkParams
* @returns {NetworkParams}
*/
initNetworkParams(networkParams) {
Expand Down Expand Up @@ -43332,9 +43360,9 @@ export class NetworkEmulator {

/**
* Creates a UTxO using a GenesisTx.
* @param {WalletEmulator} wallet
* @param {bigint} lovelace
* @param {Assets} assets
* @param {WalletEmulator} wallet
* @param {bigint} lovelace
* @param {Assets} assets
*/
createUtxo(wallet, lovelace, assets = new Assets([])) {
if (lovelace != 0n || !assets.isZero()) {
Expand All @@ -43352,7 +43380,7 @@ export class NetworkEmulator {

/**
* Mint a block with the current mempool, and advance the slot.
* @param {bigint} nSlots
* @param {bigint} nSlots
*/
tick(nSlots) {
if (this.#mempool.length > 0) {
Expand Down Expand Up @@ -43384,8 +43412,8 @@ export class NetworkEmulator {
}

/**
* @param {TxId} txId
* @param {bigint} utxoIdx
* @param {TxId} txId
* @param {bigint} utxoIdx
* @returns {boolean}
*/
isConsumed(txId, utxoIdx) {
Expand All @@ -43399,7 +43427,7 @@ export class NetworkEmulator {
}

/**
* @param {Tx} tx
* @param {Tx} tx
* @returns {Promise<TxId>}
*/
async submitTx(tx) {
Expand All @@ -43414,6 +43442,7 @@ export class NetworkEmulator {
}
}


/**
* The following functions are used for some tests in ./test/, and aren't
* intended to be used by regular users of this library.
Expand Down
Loading