How to use the @polkadot/util/bn/toU8a function in @polkadot/util

To help you get started, we’ve selected a few @polkadot/util examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github polkadot-js / api / packages / type-params / src / encode / type / index.ts View on Github external
return u8aConcat(
            bnToU8a(u8a.length, 32, true),
            u8a
          );
        })();

      case 'Timestamp':
        if (value instanceof Date) {
          value = Math.ceil(value.getTime() / 1000);
        }

        return bnToU8a(value, 64, true);

      case 'u128':
        return bnToU8a(value, 128, true);

      // TODO enums?
      case 'VoteThreshold':
        return bnToU8a(value || 0, 8, true);

      default:
        // tslint:disable-next-line
        (type as never);
        throw new Error(`No formatter for ${type}`);
    }
  } catch (error) {
    console.error('Failed encoding', type, 'with', value, error);

    throw error;
  }
}
github polkadot-js / api / packages / type-params / src / encode / type / index.ts View on Github external
);
        })();

      case 'Timestamp':
        if (value instanceof Date) {
          value = Math.ceil(value.getTime() / 1000);
        }

        return bnToU8a(value, 64, true);

      case 'u128':
        return bnToU8a(value, 128, true);

      // TODO enums?
      case 'VoteThreshold':
        return bnToU8a(value || 0, 8, true);

      default:
        // tslint:disable-next-line
        (type as never);
        throw new Error(`No formatter for ${type}`);
    }
  } catch (error) {
    console.error('Failed encoding', type, 'with', value, error);

    throw error;
  }
}
github polkadot-js / api / packages / type-params / src / key / params.ts View on Github external
return bnToU8a((value as BN), sizes.AccountIndex, true);

        case 'ParachainId':
        case 'PropIndex':
        case 'ReferendumIndex':
        case 'SessionKey':
        case 'VoteIndex':
        case 'VoteThreshold':
        case 'u32':
          return bnToU8a((value as BN), 32, true);

        case 'String':
          return u8aFromString((value as string));

        case 'Timestamp':
          return bnToU8a((value as Date).getTime(), 64, true);

        default:
          // tslint:disable-next-line
          (type as never);
          throw new Error('Unable to find handler');
      }
    } catch (error) {
      console.error('formatParams', value, index, paramTypes[index], error);

      throw error;
    }
  });
}
github polkadot-js / common / packages / type-params / src / encode / type / index.ts View on Github external
? addrPrefixes.none
            : addrPrefixes.publicKey,
          addressDecode(value)
        );

      case 'Balance':
        return bnToU8a(value, sizes.Balance.get(version) || defaultSizes.Balance, true);

      case 'BlockNumber':
      case 'Gas':
      case 'SessionKey':
      case 'u64':
        return bnToU8a(value, 64, true);

      case 'bool':
        return bnToU8a(value ? 1 : 0, 8, true);

      case 'Bytes':
      case 'Code':
        return u8aToU8a(value);

      // FIXME Here we should pass through the actual objects
      case 'Call':
      case 'CandidateReceipt':
      case 'Digest':
      case 'Header':
      case 'MisbehaviorReport':
      case 'Proposal':
        return u8aToU8a(value);

      // TODO Here we should do actual length conversions, i.e. 256/512
      case 'Hash':
github polkadot-js / api / packages / type-params / src / encode / type / index.ts View on Github external
// FIXME Here we should pass through the actual objects
      case 'Call':
      case 'CandidateReceipt':
      case 'Digest':
      case 'Header':
      case 'MisbehaviorReport':
      case 'Proposal':
        return u8aToU8a(value);

      // TODO Here we should do actual length conversions, i.e. 256/512
      case 'Hash':
      case 'Signature':
        return u8aToU8a(value);

      case 'AccountIndex':
        return bnToU8a(value, sizes.AccountIndex.get(version) || defaultSizes.Balance, true);

      case 'KeyValue':
      case 'StorageKeyValue':
        return keyValue(value);

      case 'ParachainId':
      case 'PropIndex':
      case 'ReferendumIndex':
      case 'VoteIndex':
      case 'u32':
        return bnToU8a(value, 32, true);

      case 'StorageKey':
        return storageKey(value);

      case 'String':
github polkadot-js / apps / packages / ui-signer / src / sign.ts View on Github external
export default function signMessage (publicKey: Uint8Array, nonce: BN | number, value: Uint8Array, apiSupport: EncodingVersions): Signed {
  const message = encodeCall(publicKey, nonce, value, apiSupport);
  const signature = keyring.getPair(publicKey).sign(message);
  const data = u8aConcat(
    prefixes.publicKey,
    message,
    signature
  );

  console.log(`signMessage (${apiSupport}) :   message :: ${u8aToHex(message)}`);
  console.log(`signMessage (${apiSupport}) : signature :: ${u8aToHex(signature)}`);
  console.log(`signMessage (${apiSupport}) :      data :: ${u8aToHex(data)}`);

  return {
    data: u8aConcat(
      bnToU8a(data.length, 32, true),
      data
    ),
    message,
    signature
  };
}
github polkadot-js / api / packages / type-extrinsics / src / codec / encode / length.ts View on Github external
export default function encodeLength (...values: Array): Uint8Array {
  const length = values.reduce((length, u8a) => {
    return length + u8a.length;
  }, 0);

  return u8aConcat(
    bnToU8a(length, 32, true),
    ...values
  );
}
github polkadot-js / api / packages / type-params / src / method.ts View on Github external
({ description, key = '', params, type, isDeprecated = false, isHidden = false, isSigned = false, isUnhashed = false, subscribe }: CreateItemOptions): SectionItem => ({
      description,
      index: u8aConcat(sectionIndex, bnToU8a(index, 8, true)),
      isDeprecated,
      isHidden,
      isSigned,
      isSubscription: !isUndefined(subscribe),
      isUnhashed,
      key,
      name,
      params,
      section,
      subscribe: subscribe || ['', ''],
      type
    });
}
github polkadot-js / api / packages / type-params / src / encode / type / index.ts View on Github external
return (() => {
          const u8a = u8aFromString(value);

          return u8aConcat(
            bnToU8a(u8a.length, 32, true),
            u8a
          );
        })();
github polkadot-js / common / packages / type-storage / src / key / params.ts View on Github external
const type = paramTypes[index];

      switch (type) {
        case 'AccountId':
          return addressDecode((value as Uint8Array));

        case 'Balance':
          return bnToU8a((value as BN), sizes.Balance, true);

        case 'u128':
          return bnToU8a((value as BN), 128, true);

        case 'BlockNumber':
        case 'Gas':
        case 'u64':
          return bnToU8a((value as BN), 64, true);

        case 'bool':
          return new Uint8Array([value ? 1 : 0]);

        case 'Bytes':
        case 'Call':
        case 'CandidateReceipt':
        case 'Code':
        case 'Digest':
        case 'Hash':
        case 'Header':
        case 'KeyValue':
        case 'KeyValueStorage':
        case 'MisbehaviorReport':
        case 'Proposal':
        case 'Signature':