Skip to content

Commit

Permalink
Merge pull request #56 from ardriveapp/PE-4971-generate-optional-fiel…
Browse files Browse the repository at this point in the history
…ds-if-missing

PE-4971: generate optional fields if missing
  • Loading branch information
thiagocarvalhodev authored Nov 14, 2023
2 parents a61f9d7 + ccaf926 commit 6629b72
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
28 changes: 27 additions & 1 deletion lib/src/models/wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ class Wallet {
d: encodeBigIntToBytes(privK.privateExponent!),
p: encodeBigIntToBytes(privK.p!),
q: encodeBigIntToBytes(privK.q!),
dp: encodeBigIntToBytes(
privK.privateExponent! % (privK.p! - BigInt.one)),
dq: encodeBigIntToBytes(
privK.privateExponent! % (privK.q! - BigInt.one)),
qi: encodeBigIntToBytes(privK.q!.modInverse(privK.p!)),
),
);
}
Expand Down Expand Up @@ -81,7 +86,28 @@ class Wallet {
}
});

return Wallet(keyPair: Jwk.fromJson(jwk).toKeyPair());
var privK = Jwk.fromJson(jwk).toKeyPair() as RsaKeyPairData;

if (privK.dp == null ||
privK.dp!.isEmpty ||
privK.dq == null ||
privK.dq!.isEmpty ||
privK.qi == null ||
privK.qi!.isEmpty) {
final d = decodeBytesToBigInt(privK.d);
final p = decodeBytesToBigInt(privK.p);
final q = decodeBytesToBigInt(privK.q);

jwk.addAll({
'dp': base64.normalize(encodeBigIntToBase64(d % (p - BigInt.one))),
'dq': base64.normalize(encodeBigIntToBase64(d % (q - BigInt.one))),
'qi': base64.normalize(encodeBigIntToBase64(q.modInverse(p))),
});

privK = Jwk.fromJson(jwk).toKeyPair() as RsaKeyPairData;
}

return Wallet(keyPair: privK);
}

Map<String, dynamic> toJwk() => Jwk.fromKeyPair(_keyPair!).toJson().map(
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: arweave
description: ""
version: 3.8.0
version: 3.8.1
environment:
sdk: ">=3.0.0 <4.0.0"
publish_to: none
Expand Down
18 changes: 18 additions & 0 deletions test/wallets_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,22 @@ void main() {
'browser': Skip('TODO: implement this test for browser'),
});
});

test('loading wallets with missing dp, dq, and qi will have values generated',
() async {
final wallet =
await Wallet.createWalletFromMnemonic(testArweaveAppWalletMnemonic);

final jwk = wallet.toJwk();

jwk.remove('dp');
jwk.remove('dq');
jwk.remove('qi');

final wallet2 = Wallet.fromJwk(jwk);

expect(wallet.toJwk(), equals(wallet2.toJwk()));
}, onPlatform: {
'browser': Skip('TODO: implement this test for browser'),
});
}

0 comments on commit 6629b72

Please sign in to comment.