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

Stagenet #75

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
938 changes: 433 additions & 505 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@waves/signature-adapter",
"version": "5.10.1",
"version": "6.0.0-beta.1",
"types": "dist/index.d.ts",
"main": "dist/index.js",
"license": "MIT",
Expand Down
6 changes: 5 additions & 1 deletion src/adapters/Adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ export abstract class Adapter {
public isDestroyed(): boolean {
return this._isDestroyed;
}


public abstract getSyncAddress(): string;

public abstract getSyncPublicKey(): string;

public abstract getSignVersions(): Record<SIGN_TYPE, Array<number>>;

public abstract getPublicKey(): Promise<string>;
Expand Down
19 changes: 14 additions & 5 deletions src/adapters/LedgerAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ export class LedgerAdapter extends Adapter {
return this._isMyLedger();
}

public getSyncAddress(): string {
return this._currentUser.address;
}

public getSyncPublicKey(): string {
return this._currentUser.publicKey;
}

public getPublicKey() {
return Promise.resolve(this._currentUser.publicKey);
}
Expand Down Expand Up @@ -69,11 +77,11 @@ export class LedgerAdapter extends Adapter {
return this._isMyLedger()
.then(() => LedgerAdapter._ledger.signSomeData(this._currentUser.id, bytes));
}

public getEncodedSeed() {
return Promise.reject(Error('Method "getEncodedSeed" is not available!'));
}

public getPrivateKey() {
return Promise.reject('No private key');
}
Expand All @@ -99,7 +107,8 @@ export class LedgerAdapter extends Adapter {
[SIGN_TYPE.SET_SCRIPT]: [1],
[SIGN_TYPE.SPONSORSHIP]: [1],
[SIGN_TYPE.SET_ASSET_SCRIPT]: [1],
[SIGN_TYPE.SCRIPT_INVOCATION]: [1]
[SIGN_TYPE.SCRIPT_INVOCATION]: [1],
[SIGN_TYPE.UPDATE_ASSET_INFO]: [1],
};
}

Expand All @@ -112,11 +121,11 @@ export class LedgerAdapter extends Adapter {
throw {error: 'Invalid ledger'};
}
});

promise.catch((e: any) => {
console.warn(e);
});

return promise;
}

Expand Down
43 changes: 26 additions & 17 deletions src/adapters/PrivateKeyAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,29 @@ export class PrivateKeyAdapter extends Adapter {
return {
[SIGN_TYPE.AUTH]: [1],
[SIGN_TYPE.MATCHER_ORDERS]: [1],
[SIGN_TYPE.WAVES_CONFIRMATION]: [1],
[SIGN_TYPE.CREATE_ORDER]: [1, 2, 3],
[SIGN_TYPE.CANCEL_ORDER]: [0, 1],
[SIGN_TYPE.COINOMAT_CONFIRMATION]: [1],
[SIGN_TYPE.ISSUE]: [2],
[SIGN_TYPE.TRANSFER]: [2],
[SIGN_TYPE.REISSUE]: [2],
[SIGN_TYPE.BURN]: [2],
[SIGN_TYPE.EXCHANGE]: [0, 1, 2],
[SIGN_TYPE.LEASE]: [2],
[SIGN_TYPE.CANCEL_LEASING]: [2],
[SIGN_TYPE.CREATE_ALIAS]: [2],
[SIGN_TYPE.MASS_TRANSFER]: [1],
[SIGN_TYPE.DATA]: [1],
[SIGN_TYPE.SET_SCRIPT]: [1],
[SIGN_TYPE.SPONSORSHIP]: [1],
[SIGN_TYPE.SET_ASSET_SCRIPT]: [1],
[SIGN_TYPE.SCRIPT_INVOCATION]: [1],
[SIGN_TYPE.WAVES_CONFIRMATION]: [1],
[SIGN_TYPE.TRANSFER]: [2, 3],
[SIGN_TYPE.ISSUE]: [2, 3],
[SIGN_TYPE.REISSUE]: [2, 3],
[SIGN_TYPE.BURN]: [2, 3],
[SIGN_TYPE.EXCHANGE]: [0, 1, 2, 3],
[SIGN_TYPE.LEASE]: [2, 3],
[SIGN_TYPE.CANCEL_LEASING]: [2, 3],
[SIGN_TYPE.CREATE_ALIAS]: [2, 3],
[SIGN_TYPE.MASS_TRANSFER]: [1, 2],
[SIGN_TYPE.DATA]: [1, 2],
[SIGN_TYPE.SET_SCRIPT]: [1, 2],
[SIGN_TYPE.SPONSORSHIP]: [1, 2],
[SIGN_TYPE.SET_ASSET_SCRIPT]: [1, 2],
[SIGN_TYPE.SCRIPT_INVOCATION]: [1, 2],
[SIGN_TYPE.UPDATE_ASSET_INFO]: [1],
};
}


public getEncodedSeed() {
return Promise.reject(Error('Method "getEncodedSeed" is not available!'));
}
Expand All @@ -65,6 +66,14 @@ export class PrivateKeyAdapter extends Adapter {
return Promise.reject(Error('Method "getSeed" is not available!'));
}

public getSyncAddress(): string {
return this.address;
}

public getSyncPublicKey(): string {
return this.publicKey;
}

public getPublicKey(): Promise<string> {
return Promise.resolve(this.publicKey);
}
Expand Down
83 changes: 46 additions & 37 deletions src/adapters/SeedAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@ const Seed = seedUtils.Seed;
const signWithPrivateKey = libs.crypto.signBytes;

export class SeedAdapter extends Adapter {

public isEncoded: boolean = false;
private readonly encodedSeed: string | null = null;
private seed: seedUtils.Seed;
public static type = AdapterType.Seed;


constructor(data: string | IUser, networkCode?: string | number) {
super(networkCode);
let seed;

if (typeof data === 'string') {
seed = data;
} else {
const user = <ISeedUser>data;
const encryptionRounds = user.encryptionRounds;
seed = Seed.decryptSeedPhrase(user.encryptedSeed, user.password, encryptionRounds);
}

try {
if (/^base58:/.test(seed)) {
const encodedSeed = seed.replace('base58:', '');
Expand All @@ -44,12 +44,12 @@ export class SeedAdapter extends Adapter {
}
} catch (e) {
}

if (!this.encodedSeed) {
this.encodedSeed = libs.crypto.base58Encode(libs.crypto.stringToBytes(seed as string));
}


this.seed = {
encrypt: (password: string, encryptionRounds?: number) => {
return Seed.encryptSeedPhrase(`base58:${this.encodedSeed}`, password, encryptionRounds)
Expand All @@ -61,13 +61,13 @@ export class SeedAdapter extends Adapter {
},
phrase: typeof seed === 'string' ? seed : '',
};

Object.freeze(this.seed.keyPair);
Object.freeze(this.seed);

this._isDestroyed = false;
}

public getSignVersions(): Record<SIGN_TYPE, Array<number>> {
return {
[SIGN_TYPE.AUTH]: [1],
Expand All @@ -76,65 +76,74 @@ export class SeedAdapter extends Adapter {
[SIGN_TYPE.CANCEL_ORDER]: [0, 1],
[SIGN_TYPE.COINOMAT_CONFIRMATION]: [1],
[SIGN_TYPE.WAVES_CONFIRMATION]: [1],
[SIGN_TYPE.ISSUE]: [2],
[SIGN_TYPE.TRANSFER]: [2],
[SIGN_TYPE.REISSUE]: [2],
[SIGN_TYPE.BURN]: [2],
[SIGN_TYPE.EXCHANGE]: [0, 1, 2],
[SIGN_TYPE.LEASE]: [2],
[SIGN_TYPE.CANCEL_LEASING]: [2],
[SIGN_TYPE.CREATE_ALIAS]: [2],
[SIGN_TYPE.MASS_TRANSFER]: [1],
[SIGN_TYPE.DATA]: [1],
[SIGN_TYPE.SET_SCRIPT]: [1],
[SIGN_TYPE.SPONSORSHIP]: [1],
[SIGN_TYPE.SET_ASSET_SCRIPT]: [1],
[SIGN_TYPE.SCRIPT_INVOCATION]: [1],
[SIGN_TYPE.TRANSFER]: [2, 3],
[SIGN_TYPE.ISSUE]: [2, 3],
[SIGN_TYPE.REISSUE]: [2, 3],
[SIGN_TYPE.BURN]: [2, 3],
[SIGN_TYPE.EXCHANGE]: [0, 1, 2, 3],
[SIGN_TYPE.LEASE]: [2, 3],
[SIGN_TYPE.CANCEL_LEASING]: [2, 3],
[SIGN_TYPE.CREATE_ALIAS]: [2, 3],
[SIGN_TYPE.MASS_TRANSFER]: [1, 2],
[SIGN_TYPE.DATA]: [1, 2],
[SIGN_TYPE.SET_SCRIPT]: [1, 2],
[SIGN_TYPE.SPONSORSHIP]: [1, 2],
[SIGN_TYPE.SET_ASSET_SCRIPT]: [1, 2],
[SIGN_TYPE.SCRIPT_INVOCATION]: [1, 2],
[SIGN_TYPE.UPDATE_ASSET_INFO]: [1]
};
}

public getEncodedSeed(): Promise<string> {
return Promise.resolve(this.encodedSeed as string);
}


public getSyncAddress(): string {
return this.seed.address;
}

public getSyncPublicKey(): string {
return this.seed.keyPair.publicKey;
}

public getPublicKey(): Promise<string> {
return Promise.resolve(this.seed.keyPair.publicKey);
}

public getPrivateKey(): Promise<string> {
return Promise.resolve(this.seed.keyPair.privateKey);
}

public getAddress(): Promise<string> {
return Promise.resolve(this.seed.address);
}

public getSeed(): Promise<string> {
return typeof this.seed.phrase === 'string' ? Promise.resolve(this.seed.phrase) : Promise.reject(this.seed.phrase);
}

public signRequest(bytes: Uint8Array): Promise<string> {
return this._sign(bytes);
}

public signTransaction(bytes: Uint8Array, amountPrecision: number): Promise<string> {
return this._sign(bytes);
}

public signOrder(bytes: Uint8Array, amountPrecision: number): Promise<string> {
return this._sign(bytes);
}

public signData(bytes: Uint8Array): Promise<string> {
return this._sign(bytes);
}

private _sign(bytes: Uint8Array): Promise<string> {
return Promise.resolve(signWithPrivateKey(this.seed.keyPair, bytes));
}

public static isAvailable() {
return Promise.resolve(true);
}

}
39 changes: 24 additions & 15 deletions src/adapters/WavesKeeperAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@ const DEFAULT_TX_VERSIONS = {
[SIGN_TYPE.CANCEL_ORDER]: [1],
[SIGN_TYPE.COINOMAT_CONFIRMATION]: [1],
[SIGN_TYPE.WAVES_CONFIRMATION]: [1],
[SIGN_TYPE.ISSUE]: [2],
[SIGN_TYPE.TRANSFER]: [2],
[SIGN_TYPE.REISSUE]: [2],
[SIGN_TYPE.BURN]: [2],
[SIGN_TYPE.EXCHANGE]: [0,1,2],
[SIGN_TYPE.LEASE]: [2],
[SIGN_TYPE.CANCEL_LEASING]: [2],
[SIGN_TYPE.CREATE_ALIAS]: [2],
[SIGN_TYPE.MASS_TRANSFER]: [1],
[SIGN_TYPE.DATA]: [1],
[SIGN_TYPE.SET_SCRIPT]: [1],
[SIGN_TYPE.SPONSORSHIP]: [1],
[SIGN_TYPE.SET_ASSET_SCRIPT]: [1],
[SIGN_TYPE.SCRIPT_INVOCATION]: [1]
[SIGN_TYPE.TRANSFER]: [2, 3],
[SIGN_TYPE.ISSUE]: [2, 3],
[SIGN_TYPE.REISSUE]: [2, 3],
[SIGN_TYPE.BURN]: [2, 3],
[SIGN_TYPE.EXCHANGE]: [0, 1, 2, 3],
[SIGN_TYPE.LEASE]: [2, 3],
[SIGN_TYPE.CANCEL_LEASING]: [2, 3],
[SIGN_TYPE.CREATE_ALIAS]: [2, 3],
[SIGN_TYPE.MASS_TRANSFER]: [1, 2],
[SIGN_TYPE.DATA]: [1, 2],
[SIGN_TYPE.SET_SCRIPT]: [1, 2],
[SIGN_TYPE.SPONSORSHIP]: [1, 2],
[SIGN_TYPE.SET_ASSET_SCRIPT]: [1, 2],
[SIGN_TYPE.SCRIPT_INVOCATION]: [1, 2],
[SIGN_TYPE.UPDATE_ASSET_INFO]: [1]
};

export class WavesKeeperAdapter extends Adapter {
Expand Down Expand Up @@ -110,6 +111,14 @@ export class WavesKeeperAdapter extends Adapter {
this._onDestoryCb.push(cb);
}

public getSyncAddress(): string {
return this._address;
}

public getSyncPublicKey(): string {
return this._pKey;
}

public getPublicKey() {
return Promise.resolve(this._pKey);
}
Expand All @@ -134,7 +143,7 @@ export class WavesKeeperAdapter extends Adapter {
if (signData && signData.type === 'customData') {
return (await WavesKeeperAdapter._api.signCustomData(signData)).signature;
}

return await WavesKeeperAdapter._api.signRequest(WavesKeeperAdapter._serializedData(signData));
}

Expand Down
Loading