Skip to content

Commit e49753f

Browse files
authored
Merge pull request #2062 from aeternity/unsafe-sign
fix(account): add `unsafeSign`, deprecate `sign`
2 parents 26d66a3 + b0288cc commit e49753f

File tree

20 files changed

+102
-29
lines changed

20 files changed

+102
-29
lines changed

.eslintrc.cjs

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module.exports = {
2020
'es',
2121
'src/apis',
2222
'docs/api',
23+
'test/assets',
2324
'test/environment/ledger/browser',
2425
'docs/examples',
2526
'site',

examples/browser/aepp/src/components/DataSign.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export default {
7272
this.data = encode(Buffer.from(data, type), Encoding.Bytearray);
7373
},
7474
dataSign() {
75-
return this.aeSdk.sign(decode(this.data || emptyData));
75+
return this.aeSdk.unsafeSign(decode(this.data || emptyData));
7676
},
7777
},
7878
};

examples/browser/wallet-iframe/src/App.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,11 @@ export default {
175175
return super.signTypedData(data, aci, options);
176176
}
177177
178-
async sign(data, { aeppRpcClientId: id, aeppOrigin, ...options } = {}) {
178+
async unsafeSign(data, { aeppRpcClientId: id, aeppOrigin, ...options } = {}) {
179179
if (id != null) {
180180
genConfirmCallback(`sign raw data ${data}`)(id, options, aeppOrigin);
181181
}
182-
return super.sign(data, options);
182+
return super.unsafeSign(data, options);
183183
}
184184
185185
async signDelegation(delegation, { aeppRpcClientId: id, aeppOrigin, ...options }) {

examples/browser/wallet-web-extension/src/background.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ class AccountMemoryProtected extends MemoryAccount {
8686
return super.signTypedData(data, aci, options);
8787
}
8888

89-
async sign(data, { aeppRpcClientId: id, aeppOrigin, ...options } = {}) {
89+
async unsafeSign(data, { aeppRpcClientId: id, aeppOrigin, ...options } = {}) {
9090
if (id != null) {
9191
await genConfirmCallback(`sign raw data ${data}`)(id, options, aeppOrigin);
9292
}
93-
return super.sign(data, options);
93+
return super.unsafeSign(data, options);
9494
}
9595

9696
async signDelegation(delegation, { aeppRpcClientId: id, aeppOrigin, ...options }) {

src/AeSdkBase.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,25 @@ export default class AeSdkBase extends AeSdkMethods {
164164
* Sign data blob
165165
* @param data - Data to sign
166166
* @param options - Options
167+
* @deprecated Use `unsafeSign` method instead
167168
*/
168169
async sign(
170+
data: string | Uint8Array,
171+
options: { onAccount?: OnAccount } = {},
172+
): Promise<Uint8Array> {
173+
return this.unsafeSign(data, options);
174+
}
175+
176+
/**
177+
* Sign data blob
178+
* @param data - Data to sign
179+
* @param options - Options
180+
*/
181+
async unsafeSign(
169182
data: string | Uint8Array,
170183
{ onAccount, ...options }: { onAccount?: OnAccount } = {},
171184
): Promise<Uint8Array> {
172-
return this._resolveAccount(onAccount).sign(data, options);
185+
return this._resolveAccount(onAccount).unsafeSign(data, options);
173186
}
174187

175188
/**

src/AeSdkWallet.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,10 @@ export default class AeSdkWallet extends AeSdk {
331331
if (!this._isRpcClientConnected(id)) throw new RpcNotAuthorizeError();
332332
if (!this.addresses().includes(onAccount)) throw new RpcPermissionDenyError(onAccount);
333333
const parameters = { onAccount, aeppOrigin: origin, aeppRpcClientId: id };
334-
const signature = encode(await this.sign(decode(data), parameters), Encoding.Signature);
334+
const signature = encode(
335+
await this.unsafeSign(decode(data), parameters),
336+
Encoding.Signature,
337+
);
335338
return { signature };
336339
},
337340
[METHODS.signDelegation]: async ({ delegation, onAccount = this.address }, origin) => {

src/account/Base.ts

+15
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export default abstract class AccountBase {
7474
* @param data - Data blob to sign
7575
* @param options - Options
7676
* @returns Signature
77+
* @deprecated Use `unsafeSign` method instead
7778
*/
7879
abstract sign(
7980
data: string | Uint8Array,
@@ -83,6 +84,20 @@ export default abstract class AccountBase {
8384
},
8485
): Promise<Uint8Array>;
8586

87+
/**
88+
* Sign data blob
89+
* @param data - Data blob to sign
90+
* @param options - Options
91+
* @returns Signature
92+
*/
93+
abstract unsafeSign(
94+
data: string | Uint8Array,
95+
options?: {
96+
aeppOrigin?: string;
97+
aeppRpcClientId?: string;
98+
},
99+
): Promise<Uint8Array>;
100+
86101
/**
87102
* Account address
88103
*/

src/account/Generalized.ts

+8
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,16 @@ export default class AccountGeneralized extends AccountBase {
2828
this.address = address;
2929
}
3030

31+
/**
32+
* @deprecated Use `unsafeSign` method instead
33+
*/
3134
// eslint-disable-next-line class-methods-use-this
3235
override async sign(): Promise<Uint8Array> {
36+
return this.unsafeSign();
37+
}
38+
39+
// eslint-disable-next-line class-methods-use-this
40+
override async unsafeSign(): Promise<Uint8Array> {
3341
throw new NotImplementedError("Can't sign using generalized account");
3442
}
3543

src/account/Ledger.ts

+8
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,16 @@ export default class AccountLedger extends AccountBase {
2929
transport.decorateAppAPIMethods(this, ['signTransaction', 'signMessage'], 'w0w');
3030
}
3131

32+
/**
33+
* @deprecated Use `unsafeSign` method instead
34+
*/
3235
// eslint-disable-next-line class-methods-use-this
3336
override async sign(): Promise<Uint8Array> {
37+
return this.unsafeSign();
38+
}
39+
40+
// eslint-disable-next-line class-methods-use-this
41+
override async unsafeSign(): Promise<Uint8Array> {
3442
throw new NotImplementedError('RAW signing using Ledger HW');
3543
}
3644

src/account/Memory.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,16 @@ export default class AccountMemory extends AccountBase {
4545
return new AccountMemory(secretKey);
4646
}
4747

48+
/**
49+
* @deprecated Use `unsafeSign` method instead
50+
*/
4851
// eslint-disable-next-line @typescript-eslint/no-unused-vars
4952
override async sign(data: string | Uint8Array, options?: any): Promise<Uint8Array> {
53+
return this.unsafeSign(data, options);
54+
}
55+
56+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
57+
override async unsafeSign(data: string | Uint8Array, options?: any): Promise<Uint8Array> {
5058
return nacl.sign.detached(Buffer.from(data), this.#secretKeyDecoded);
5159
}
5260

@@ -60,12 +68,12 @@ export default class AccountMemory extends AccountBase {
6068
const rlpBinaryTx = decode(transaction);
6169
const txWithNetworkId = getBufferToSign(transaction, networkId, innerTx === true);
6270

63-
const signatures = [await this.sign(txWithNetworkId, options)];
71+
const signatures = [await this.unsafeSign(txWithNetworkId, options)];
6472
return buildTx({ tag: Tag.SignedTx, encodedTx: rlpBinaryTx, signatures });
6573
}
6674

6775
override async signMessage(message: string, options?: any): Promise<Uint8Array> {
68-
return this.sign(messageToHash(message), options);
76+
return this.unsafeSign(messageToHash(message), options);
6977
}
7078

7179
override async signTypedData(
@@ -85,7 +93,7 @@ export default class AccountMemory extends AccountBase {
8593
networkId,
8694
contractAddress,
8795
});
88-
const signature = await this.sign(dHash, options);
96+
const signature = await this.unsafeSign(dHash, options);
8997
return encode(signature, Encoding.Signature);
9098
}
9199

@@ -100,7 +108,7 @@ export default class AccountMemory extends AccountBase {
100108
Buffer.from(networkId),
101109
decode(delegation),
102110
]);
103-
const signature = await this.sign(payload);
111+
const signature = await this.unsafeSign(payload);
104112
return encode(signature, Encoding.Signature);
105113
}
106114
}

src/account/Metamask.ts

+8
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,16 @@ export default class AccountMetamask extends AccountBase {
4646
this.address = address;
4747
}
4848

49+
/**
50+
* @deprecated Use `unsafeSign` method instead
51+
*/
4952
// eslint-disable-next-line class-methods-use-this
5053
override async sign(): Promise<Uint8Array> {
54+
return this.unsafeSign();
55+
}
56+
57+
// eslint-disable-next-line class-methods-use-this
58+
override async unsafeSign(): Promise<Uint8Array> {
5159
throw new NotImplementedError('RAW signing using MetaMask');
5260
}
5361

src/account/Rpc.ts

+7
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,14 @@ export default class AccountRpc extends AccountBase {
2323
this.address = address;
2424
}
2525

26+
/**
27+
* @deprecated Use `unsafeSign` method instead
28+
*/
2629
async sign(dataRaw: string | Uint8Array): Promise<Uint8Array> {
30+
return this.unsafeSign(dataRaw);
31+
}
32+
33+
async unsafeSign(dataRaw: string | Uint8Array): Promise<Uint8Array> {
2734
const data = encode(Buffer.from(dataRaw), Encoding.Bytearray);
2835
const { signature } = await this._rpcClient.request(METHODS.unsafeSign, {
2936
onAccount: this.address,

src/utils/jwt.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export async function signJwt(originalPayload: any, account: AccountBase): Promi
3939
}
4040
if (payload.sub_jwk === undefined) delete payload.sub_jwk;
4141
const body = `${header}.${objectToBase64Url(payload)}` as const;
42-
const signature = await account.sign(body);
42+
const signature = await account.unsafeSign(body);
4343
return `${body}.${toBase64Url(signature)}`;
4444
}
4545

test/integration/accounts.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ describe('Accounts', () => {
206206

207207
it('Invalid on account options', async () => {
208208
await expect(
209-
aeSdk.sign('tx_Aasdasd', { onAccount: 123 as unknown as Encoded.AccountAddress }),
209+
aeSdk.unsafeSign('tx_Aasdasd', { onAccount: 123 as unknown as Encoded.AccountAddress }),
210210
).to.be.rejectedWith(
211211
TypeError,
212212
'Account should be an address (ak-prefixed string), or instance of AccountBase, got 123 instead',
@@ -216,8 +216,8 @@ describe('Accounts', () => {
216216
it('Make operation on account using MemoryAccount', async () => {
217217
const account = MemoryAccount.generate();
218218
const data = 'Hello';
219-
const signature = await account.sign(data);
220-
const sigUsingMemoryAccount = await aeSdk.sign(data, { onAccount: account });
219+
const signature = await account.unsafeSign(data);
220+
const sigUsingMemoryAccount = await aeSdk.unsafeSign(data, { onAccount: account });
221221
expect(signature).to.eql(sigUsingMemoryAccount);
222222
});
223223
});

test/integration/contract-aci.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1329,7 +1329,7 @@ describe('Contract instance', () => {
13291329
});
13301330

13311331
it('Valid', async () => {
1332-
const fakeSignature = Buffer.from(await aeSdk.sign(decode(aeSdk.address)));
1332+
const fakeSignature = Buffer.from(await aeSdk.unsafeSign(decode(aeSdk.address)));
13331333
const hashAsBuffer = await testContract.signatureFn(fakeSignature);
13341334
const hashAsHex = await testContract.signatureFn(fakeSignature.toString('hex'));
13351335
hashAsBuffer.decodedResult.should.be.eql(fakeSignature);

test/integration/contract.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ describe('Contract', () => {
7070
});
7171
await signContract.$deploy([]);
7272
const data = Buffer.from(new Array(32).fill(0).map((_, idx) => idx ** 2));
73-
const signature = await aeSdk.sign(data);
73+
const signature = await aeSdk.unsafeSign(data);
7474
expect((await signContract.verify(data, aeSdk.address, signature)).decodedResult).to.equal(
7575
true,
7676
);

test/integration/rpc.ts

+11-9
Original file line numberDiff line numberDiff line change
@@ -503,28 +503,30 @@ describe('Aepp<->Wallet', () => {
503503

504504
it('rejected by wallet', async () => {
505505
let origin;
506-
const s = stub(wallet._resolveAccount(), 'sign').callsFake((data, { aeppOrigin } = {}) => {
507-
origin = aeppOrigin;
508-
throw new RpcRejectedByUserError();
509-
});
510-
await expect(aepp.sign(rawData))
506+
const s = stub(wallet._resolveAccount(), 'unsafeSign').callsFake(
507+
(data, { aeppOrigin } = {}) => {
508+
origin = aeppOrigin;
509+
throw new RpcRejectedByUserError();
510+
},
511+
);
512+
await expect(aepp.unsafeSign(rawData))
511513
.to.be.eventually.rejectedWith('Operation rejected by user')
512514
.with.property('code', 4);
513515
expect(origin).to.equal('http://origin.test');
514516
s.restore();
515517
});
516518

517519
it('works', async () => {
518-
const signature = await aepp.sign(rawData);
520+
const signature = await aepp.unsafeSign(rawData);
519521
expect(signature).to.be.an.instanceOf(Buffer);
520522
expect(verify(rawData, signature, aepp.address)).to.equal(true);
521523
});
522524

523525
it('fails with unknown error', async () => {
524-
const s = stub(wallet._resolveAccount(), 'sign').callsFake(() => {
526+
const s = stub(wallet._resolveAccount(), 'unsafeSign').callsFake(() => {
525527
throw new Error('test');
526528
});
527-
await expect(aepp.sign(rawData))
529+
await expect(aepp.unsafeSign(rawData))
528530
.to.be.eventually.rejectedWith(
529531
'The peer failed to execute your request due to unknown error',
530532
)
@@ -534,7 +536,7 @@ describe('Aepp<->Wallet', () => {
534536

535537
it('signs using specific account', async () => {
536538
const onAccount = wallet.addresses()[1];
537-
const signature = await aepp.sign(rawData, { onAccount });
539+
const signature = await aepp.unsafeSign(rawData, { onAccount });
538540
expect(verify(rawData, signature, onAccount)).to.equal(true);
539541
});
540542
});

test/unit/ledger.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ describe('Ledger HW', function () {
181181
it('fails on calling raw signing', async () => {
182182
const transport = await initTransport('\n');
183183
const account = new AccountLedger(transport, 0, address);
184-
await expect(account.sign()).to.be.rejectedWith('RAW signing using Ledger HW');
184+
await expect(account.unsafeSign()).to.be.rejectedWith('RAW signing using Ledger HW');
185185
});
186186

187187
const transaction = buildTx({

test/unit/memory-account.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe('MemoryAccount', () => {
2727
it('Sign raw data', async () => {
2828
const data = Buffer.from(new Array(10).fill(0).map((_, idx) => idx));
2929
const account = new MemoryAccount(secretKey);
30-
const signature = await account.sign(data);
30+
const signature = await account.unsafeSign(data);
3131
expect(signature).to.eql(
3232
Uint8Array.from([
3333
113, 154, 121, 195, 164, 141, 153, 234, 196, 82, 120, 31, 198, 179, 208, 138, 88, 63, 98,

test/unit/metamask.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ describe('Aeternity Snap for MetaMask', function () {
239239
it('fails on calling raw signing', async () => {
240240
const provider = await initProvider([]);
241241
const account = new AccountMetamask(provider, 0, address);
242-
await expect(account.sign()).to.be.rejectedWith('RAW signing using MetaMask');
242+
await expect(account.unsafeSign()).to.be.rejectedWith('RAW signing using MetaMask');
243243
});
244244

245245
const transaction = buildTx({

0 commit comments

Comments
 (0)