@@ -23,10 +23,10 @@ class _Bip32KeySerializer {
2323 static List <int > serializeBytes (
2424 List <int > keyBytes,
2525 Bip32KeyData keyData,
26- List <int > keyNetVerBytes,
26+ List <int >? keyNetVerBytes,
2727 ) {
2828 return [
29- ...keyNetVerBytes,
29+ ...keyNetVerBytes ?? [] ,
3030 ...keyData.depth.toBytes (),
3131 ...keyData.fingerPrint.toBytes (),
3232 ...keyData.index.toBytes (),
@@ -64,16 +64,17 @@ class Bip32PrivateKeySerializer {
6464 );
6565 }
6666
67- static List <int > serializeBytes (
68- IPrivateKey privKey,
69- Bip32KeyData keyData, [
67+ static List <int > serializeBytes ({
68+ required IPrivateKey privKey,
69+ required Bip32KeyData keyData,
7070 Bip32KeyNetVersions ? keyNetVer,
71- ]) {
71+ bool withPrefix = true ,
72+ }) {
7273 keyNetVer ?? = Bip32Const .mainNetKeyNetVersions;
7374 return _Bip32KeySerializer .serializeBytes (
7475 [0x00 , ...privKey.raw],
7576 keyData,
76- keyNetVer.private,
77+ withPrefix ? keyNetVer.private : null ,
7778 );
7879 }
7980}
@@ -95,16 +96,17 @@ class Bip32PublicKeySerializer {
9596 );
9697 }
9798
98- static List <int > serializeBytes (
99- IPublicKey pubKey,
100- Bip32KeyData keyData, [
101- Bip32KeyNetVersions ? keyNetVer,
102- ]) {
99+ static List <int > serializeBytes ({
100+ required IPublicKey pubKey,
101+ required Bip32KeyData keyData,
102+ required Bip32KeyNetVersions ? keyNetVer,
103+ bool withPrefix = true ,
104+ }) {
103105 keyNetVer ?? = Bip32Const .mainNetKeyNetVersions;
104106 return _Bip32KeySerializer .serializeBytes (
105107 pubKey.compressed,
106108 keyData,
107- keyNetVer.private,
109+ withPrefix ? keyNetVer.private : null ,
108110 );
109111 }
110112}
@@ -160,6 +162,30 @@ class Bip32KeyDeserializer {
160162 return Bip32DeserializedKey (keyParts.$1, keyParts.$2, isPublic);
161163 }
162164
165+ static Bip32DeserializedKey deserializeKeyBytesWithoutPrefix (
166+ List <int > serKeyBytes, {
167+ bool isPublic = false ,
168+ }) {
169+ // Validate length
170+ if (isPublic &&
171+ serKeyBytes.length !=
172+ (Bip32KeySerConst .serializedPubKeyByteLen -
173+ Bip32KeyNetVersionsConst .keyNetVersionByteLen)) {
174+ throw Bip32KeyError ('Invalid extended public key.' );
175+ }
176+ if (! isPublic &&
177+ ! Bip32KeySerConst .serializedPrivKeyByteLen.contains (
178+ serKeyBytes.length + Bip32KeyNetVersionsConst .keyNetVersionByteLen,
179+ )) {
180+ throw Bip32KeyError ('Invalid extended private key.' );
181+ }
182+
183+ // Get parts back
184+ final keyParts = _getPartsFromBytes (serKeyBytes, isPublic, offset: 0 );
185+
186+ return Bip32DeserializedKey (keyParts.$1, keyParts.$2, isPublic);
187+ }
188+
163189 /// Deserialize a key.
164190 static Bip32DeserializedKey deserializeKey (
165191 String serKeyStr, {
@@ -191,9 +217,10 @@ class Bip32KeyDeserializer {
191217 /// Get back key parts from serialized key bytes.
192218 static (List <int >, Bip32KeyData ) _getPartsFromBytes (
193219 List <int > serKeyBytes,
194- bool isPublic,
195- ) {
196- final depthIdx = Bip32KeyNetVersions .length;
220+ bool isPublic, {
221+ int offset = Bip32KeyNetVersionsConst .keyNetVersionByteLen,
222+ }) {
223+ final depthIdx = offset;
197224 final fprintIdx = depthIdx + Bip32Depth .fixedLength ();
198225 final keyIndexIdx = fprintIdx + Bip32FingerPrint .fixedLength ();
199226 final chainCodeIdx = keyIndexIdx + Bip32KeyIndex .fixedLength ();
0 commit comments