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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 14 additions & 1 deletion
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

Lines changed: 4 additions & 1 deletion
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

Lines changed: 15 additions & 0 deletions
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

Lines changed: 8 additions & 0 deletions
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

Lines changed: 8 additions & 0 deletions
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

Lines changed: 12 additions & 4 deletions
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
}

0 commit comments

Comments
 (0)